Class: HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, protocol = 'https') ⇒ HttpClient

Returns a new instance of HttpClient.



10
11
12
13
14
# File 'lib/http_client.rb', line 10

def initialize(host, protocol = 'https')
  @host = host
  @protocol = protocol
  @headers = {}
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



8
9
10
# File 'lib/http_client.rb', line 8

def headers
  @headers
end

#hostObject

Returns the value of attribute host.



8
9
10
# File 'lib/http_client.rb', line 8

def host
  @host
end

#protocolObject

Returns the value of attribute protocol.



8
9
10
# File 'lib/http_client.rb', line 8

def protocol
  @protocol
end

Instance Method Details

#get(path) ⇒ Object



50
51
52
# File 'lib/http_client.rb', line 50

def get(path)
  RestClient::Request.execute(:method => :get, :url => path, :headers => @headers, :verify_ssl => false)
end

#post(path, data) ⇒ Object



54
55
56
# File 'lib/http_client.rb', line 54

def post(path, data)
  RestClient::Request.execute(:method => :post, :url => path, :payload => data, :headers => @headers)
end

#request(method, path, request_params = {}, payload = {}, extra_options = {}) ⇒ Object



20
21
22
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
# File 'lib/http_client.rb', line 20

def request(method, path, request_params = {}, payload = {}, extra_options = {})
  response = nil
  path = url(method, path, request_params)
  begin
    case method
    when 'get'
      response = self.get(path)
    when 'post'
      payload = build_query(payload) if extra_options[:multipart] != true
      response = self.post(path, payload)
    end
  rescue RestClient::BadRequest => e
    raise BadRequest.new(JSON.parse(e.http_body)["statusMessage"])								
  rescue RestClient::ResourceNotFound => e
    raise ResourceNotFound.new(e.message)  
  rescue RestClient::Unauthorized => e
    raise UnAuthorizedException.new(e.message)
  rescue Exception => e
    raise e
  end

  unless extra_options[:stream] == true
    unless response.empty?
      response = ::ActiveSupport::JSON.decode(response)
      response = to_hashie(response)
    end
  end
  response
end

#set_headers(hash) ⇒ Object



16
17
18
# File 'lib/http_client.rb', line 16

def set_headers(hash)
  @headers = @headers.merge(hash)
end