Skip to content

Commit

Permalink
Move http() method into Robot, and delegate in other classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tombell committed May 26, 2012
1 parent 90ffb1e commit dd8805e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 51 deletions.
26 changes: 1 addition & 25 deletions src/adapter.coffee
@@ -1,12 +1,10 @@
{EventEmitter} = require 'events'
HttpClient = require 'scoped-http-client'

class Adapter extends EventEmitter
# An adapter is a specific interface to a chat source for robots.
#
# robot - A Robot instance.
constructor: (@robot) ->
@httpClient = HttpClient

# Public: Raw method for sending data back to the chat source. Extend this.
#
Expand Down Expand Up @@ -71,30 +69,8 @@ class Adapter extends EventEmitter
# Once your request is assembled, you can call `get()`/`post()`/etc to
# send the request.
#
# url - String URL to access.
#
# Examples:
#
# res.http("http://example.com")
# # set a single header
# .header('Authorization', 'bearer abcdef')
#
# # set multiple headers
# .headers(Authorization: 'bearer abcdef', Accept: 'application/json')
#
# # add URI query parameters
# .query(a: 1, b: 'foo & bar')
#
# # make the actual request
# .get() (err, res, body) ->
# console.log body
#
# # or, you can POST data
# .post(data) (err, res, body) ->
# console.log body
#
# Returns a ScopedClient instance.
http: (url) ->
@httpClient.create(url)
@robot.http(url)

module.exports = Adapter
27 changes: 1 addition & 26 deletions src/response.coffee
@@ -1,5 +1,3 @@
HttpClient = require 'scoped-http-client'

class Response
# Public: Responses are sent to matching listeners. Messages know about the
# content and user that made the original message, and how to reply back to
Expand All @@ -9,7 +7,6 @@ class Response
# message - The current Message instance.
# match - The Match object from the successful Regex match.
constructor: (@robot, @message, @match) ->
@httpClient = HttpClient

# Public: Posts a message back to the chat source
#
Expand Down Expand Up @@ -57,30 +54,8 @@ class Response
# Once your request is assembled, you can call `get()`/`post()`/etc to
# send the request.
#
# url - String URL to access.
#
# Examples:
#
# res.http("http://example.com")
# # set a single header
# .header('Authorization', 'bearer abcdef')
#
# # set multiple headers
# .headers(Authorization: 'bearer abcdef', Accept: 'application/json')
#
# # add URI query parameters
# .query(a: 1, b: 'foo & bar')
#
# # make the actual request
# .get() (err, res, body) ->
# console.log body
#
# # or, you can POST data
# .post(data) (err, res, body) ->
# console.log body
#
# Returns a ScopedClient instance.
http: (url) ->
@httpClient.create(url)
@robot.http(url)

module.exports = Response
30 changes: 30 additions & 0 deletions src/robot.coffee
Expand Up @@ -340,5 +340,35 @@ class Robot
content = JSON.parse data
@version = content.version

# Public: Creates a scoped http client with chainable methods for
# modifying the request. This doesn't actually make a request though.
# Once your request is assembled, you can call `get()`/`post()`/etc to
# send the request.
#
# url - String URL to access.
#
# Examples:
#
# res.http("http://example.com")
# # set a single header
# .header('Authorization', 'bearer abcdef')
#
# # set multiple headers
# .headers(Authorization: 'bearer abcdef', Accept: 'application/json')
#
# # add URI query parameters
# .query(a: 1, b: 'foo & bar')
#
# # make the actual request
# .get() (err, res, body) ->
# console.log body
#
# # or, you can POST data
# .post(data) (err, res, body) ->
# console.log body
#
# Returns a ScopedClient instance.
http: (url) ->
HttpClient.create(url)

module.exports = Robot

0 comments on commit dd8805e

Please sign in to comment.