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



100
101
102
103
104
105
106
107
108
# File 'lib/backchat_client/http_client.rb', line 100

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}=#{CGI::escape(v.to_s)}" }.join("&"))
  debug("delete request to uri #{uri}")
  RestClient.delete(uri, headers) { |response, request, result, &block|
    response_handler(response, request, result, &block)
  }
end

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

HTTP GET



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/backchat_client/http_client.rb', line 62

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}=#{CGI::escape(v.to_s)}" }.join("&"))
  debug("get request to uri #{uri}")
  RestClient.get(uri, headers) { |response, request, result, &block|
    response_handler(response, request, result, &block)
  }
  
  
end

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

HTTP POST



76
77
78
79
80
81
82
83
84
85
# File 'lib/backchat_client/http_client.rb', line 76

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) { |response, request, result, &block|
    response_handler(response, request, result, &block)
  }
end

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

HTTP PUT



88
89
90
91
92
93
94
95
96
97
# File 'lib/backchat_client/http_client.rb', line 88

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) { |response, request, result, &block|
    response_handler(response, request, result, &block)
  }
end

#response_handler(response, request, result, &block) ⇒ Object

This method is used to handle the HTTP response response HTTP response request HTTP request result



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/backchat_client/http_client.rb', line 23

def response_handler(response, request, result, &block)
  case response.code
  when 200..207
    # If there is a :location header, return it
    response.return!(request, result, &block)
  when 301..307
    response.follow_redirection(request, result, &block)
  when 400
    logger.warn("Bad request")
    logger.warn("#{result.code} => #{result.message}")
    raise BackchatClient::Error::ClientError.new(result)
  when 401
    logger.warn("Error while accessing Backchat. Authentication failed")
    logger.warn("#{result.code} => #{result.message}")
    raise BackchatClient::Error::ClientError.new(result)
  when 404
    logger.warn("Error while accessing Backchat. Resource not found")
    logger.warn("#{result.code} => #{result.message}")
    raise BackchatClient::Error::ClientError.new(result)
  when 409
    logger.warn("Error while accessing Backchat. Conflict")
    logger.warn("#{result.code} => #{result.message}")
    raise BackchatClient::Error::ConflictError.new(result)
  when 422
    logger.warn("Error while accessing Backchat. Unprocessable entity")
    logger.warn("#{result.code} => #{result.message}")
    raise BackchatClient::Error::UnprocessableError.new(result)
  when 402..408,410..421,423..499
    logger.warn("Error while accessing Backchat")
    logger.warn("#{result.code} => #{result.message}")
    response.return!(request, result, &block)
  else
    logger.warn("Error while accessing Backchat")
    logger.warn("#{result.code} => #{result.message}")
    response.return!(request, result, &block)
  end
end