Class: Lolitado::API
Direct Known Subclasses
Class Method Summary collapse
-
.add_headers(value) ⇒ Object
define a method to set http headers.
-
.format_response(response, msecs) ⇒ Object
format api response.
-
.new_headers ⇒ Object
define a method to get http headers.
-
.request(method, endpoint, body = false) ⇒ Object
define a customize http request method for rest api, forward this method to call HTTParty.get or HTTParty.post or HTTParty.put or HTTParty.delete method.
Class Method Details
.add_headers(value) ⇒ Object
define a method to set http headers
49 50 51 52 53 |
# File 'lib/lolitado/api.rb', line 49 def self.add_headers value @new_headers ||= {} @new_headers = @new_headers.merge(value.to_hash) return @new_headers end |
.format_response(response, msecs) ⇒ Object
format api response
12 13 14 |
# File 'lib/lolitado/api.rb', line 12 def self.format_response response, msecs return {:response => response, :message => response.parsed_response, :status => response.code, :duration => msecs} end |
.new_headers ⇒ Object
define a method to get http headers
58 59 60 |
# File 'lib/lolitado/api.rb', line 58 def self.new_headers return @new_headers end |
.request(method, endpoint, body = false) ⇒ Object
define a customize http request method for rest api, forward this method to call HTTParty.get or HTTParty.post or HTTParty.put or HTTParty.delete method
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/lolitado/api.rb', line 24 def self.request method, endpoint, body = false start = Time.now case method.to_s when "get" response = self.get(endpoint, :body => body, :headers => API.new_headers, :verify => false) when "post" response = self.post(endpoint, :body => body, :headers => API.new_headers, :verify => false) when "put" response = self.put(endpoint, :body => body, :headers => API.new_headers, :verify => false) when "delete" response = self.delete(endpoint, :headers => API.new_headers, :verify => false) else warn "#{method} is invalid http method." end finish = Time.now msecs = (finish - start) * 1000.0 # puts "RESPONSE - #{msecs}" return self.format_response(response, msecs) end |