Module: BackchatClient::HttpClient

Includes:
BackchatLogger
Included in:
Channel, Stream, User
Defined in:
lib/backchat_client/http_client.rb

Overview

This module sends HTTP requests to Backchat including the specific header authenticating the sender It’s just a mixin, some features must be pre-loaded in the entity that uses the mixin @endpoint: Backchat API URI: api.backchat.io @api_key: application api key: kljdfjrwerwlkdfjsldkf URI_PATH: entity/model specific: streams, channels, etc.

Instance Method Summary collapse

Methods included from BackchatLogger

#debug, #error, included, #logger

Instance Method Details

#delete(path, params = {}, headers = {}) ⇒ Object

HTTP DELETE



49
50
51
52
53
54
55
# File 'lib/backchat_client/http_client.rb', line 49

def delete(path, params = {}, headers = {})
  headers.merge!({:Authorization => "Backchat #{@api_key}", :accept => :json})
  uri = set_path(path)
  uri = "#{uri}?".concat(params.collect { |k, v| "#{k}=#{v.to_s}" }.join("&"))
  debug("delete request to uri #{uri}")
  RestClient.delete(uri, headers)
end

#get(path, params = {}, headers = {}) ⇒ Object

HTTP GET



19
20
21
22
23
24
25
26
# File 'lib/backchat_client/http_client.rb', line 19

def get(path, params = {}, headers = {})
  headers.merge!({:Authorization => "Backchat #{@api_key}", :Accept => "application/json"})

  uri = set_path(path)
  uri = "#{uri}?".concat(params.collect { |k, v| "#{k}=#{v.to_s}" }.join("&"))
  debug("get request to uri #{uri}")
  RestClient.get(uri, headers)
end

#post(path, body = {}, headers = {}) ⇒ Object

HTTP POST



29
30
31
32
33
34
35
36
# File 'lib/backchat_client/http_client.rb', line 29

def post(path, body = {}, headers = {})
  headers.merge!({:Authorization => "Backchat #{@api_key}", :content_type => :json, :accept => :json})
  body = ActiveSupport::JSON.encode(body)
  uri = set_path(path)
  debug("post request to uri #{uri}")
  debug("post body: <#{body}>")
  RestClient.post("#{uri}", body, headers)
end

#put(path, body = {}, headers = {}) ⇒ Object

HTTP PUT



39
40
41
42
43
44
45
46
# File 'lib/backchat_client/http_client.rb', line 39

def put(path, body = {}, headers = {})
  headers.merge!({:Authorization => "Backchat #{@api_key}", :content_type => :json, :accept => :json})
  body = ActiveSupport::JSON.encode(body)
  uri = set_path(path)
  debug("put request to uri #{uri}")
  debug("put body: <#{body}>")
  RestClient.put("#{uri}", body, headers)
end