Class: HttpClient
- Inherits:
-
Object
- Object
- HttpClient
- Defined in:
- lib/http_client.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#host ⇒ Object
Returns the value of attribute host.
-
#protocol ⇒ Object
Returns the value of attribute protocol.
Instance Method Summary collapse
- #get(path) ⇒ Object
-
#initialize(host, protocol = 'https') ⇒ HttpClient
constructor
A new instance of HttpClient.
- #post(path, data) ⇒ Object
- #request(method, path, request_params = {}, payload = {}, extra_options = {}) ⇒ Object
- #set_headers(hash) ⇒ Object
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
#headers ⇒ Object
Returns the value of attribute headers.
8 9 10 |
# File 'lib/http_client.rb', line 8 def headers @headers end |
#host ⇒ Object
Returns the value of attribute host.
8 9 10 |
# File 'lib/http_client.rb', line 8 def host @host end |
#protocol ⇒ Object
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 = {}, = {}) response = nil path = url(method, path, request_params) begin case method when 'get' response = self.get(path) when 'post' payload = build_query(payload) if [: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.) rescue RestClient::Unauthorized => e raise UnAuthorizedException.new(e.) rescue Exception => e raise e end unless [: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 |