Class: CatalogAPI::Request
- Inherits:
-
Object
- Object
- CatalogAPI::Request
- Defined in:
- lib/catalogapi/request.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#data ⇒ Object
Returns the value of attribute data.
-
#json ⇒ Object
Returns the value of attribute json.
-
#method_name ⇒ Object
Returns the value of attribute method_name.
-
#path ⇒ Object
Returns the value of attribute path.
-
#response ⇒ Object
Returns the value of attribute response.
Instance Method Summary collapse
- #base_url ⇒ Object
- #each(&block) ⇒ Object
- #first ⇒ Object
- #get(params = {}) ⇒ Object
-
#initialize(method_name, path: nil) ⇒ Request
constructor
A new instance of Request.
- #json_response ⇒ Object
- #map(&block) ⇒ Object
- #next_page ⇒ Object
- #post(params = {}) ⇒ Object
- #required_params(prepend = 'creds_') ⇒ Object
Constructor Details
#initialize(method_name, path: nil) ⇒ Request
Returns a new instance of Request.
11 12 13 14 15 |
# File 'lib/catalogapi/request.rb', line 11 def initialize(method_name, path: nil) @data = [] @method_name = method_name @path = path || method_name end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
10 11 12 |
# File 'lib/catalogapi/request.rb', line 10 def body @body end |
#data ⇒ Object
Returns the value of attribute data.
10 11 12 |
# File 'lib/catalogapi/request.rb', line 10 def data @data end |
#json ⇒ Object
Returns the value of attribute json.
10 11 12 |
# File 'lib/catalogapi/request.rb', line 10 def json @json end |
#method_name ⇒ Object
Returns the value of attribute method_name.
10 11 12 |
# File 'lib/catalogapi/request.rb', line 10 def method_name @method_name end |
#path ⇒ Object
Returns the value of attribute path.
10 11 12 |
# File 'lib/catalogapi/request.rb', line 10 def path @path end |
#response ⇒ Object
Returns the value of attribute response.
10 11 12 |
# File 'lib/catalogapi/request.rb', line 10 def response @response end |
Instance Method Details
#base_url ⇒ Object
17 18 19 20 21 |
# File 'lib/catalogapi/request.rb', line 17 def base_url env = CatalogAPI.production? ? 'prod' : 'dev' username = CatalogAPI.username "https://#{username}.#{env}.catalogapi.com/v1" end |
#each(&block) ⇒ Object
58 59 60 |
# File 'lib/catalogapi/request.rb', line 58 def each(&block) data.each(&block) end |
#first ⇒ Object
54 55 56 |
# File 'lib/catalogapi/request.rb', line 54 def first data.first end |
#get(params = {}) ⇒ Object
23 24 25 26 27 |
# File 'lib/catalogapi/request.rb', line 23 def get(params = {}) url = "#{base_url}/rest/#{path}" self.response = HTTP.get(url, { params: params.merge(required_params) }) json_response end |
#json_response ⇒ Object
35 36 37 38 39 |
# File 'lib/catalogapi/request.rb', line 35 def json_response check_status! self.json = JSON.parse(response.body.to_s, symbolize_names: true) self end |
#map(&block) ⇒ Object
62 63 64 |
# File 'lib/catalogapi/request.rb', line 62 def map(&block) data.map(&block) end |
#next_page ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/catalogapi/request.rb', line 66 def next_page page_info = json.dig( "#{method_name}_response".to_sym, "#{method_name}_result".to_sym, :pager ).to_h return nil unless page_info[:has_next].to_i == 1 page_info[:page] + 1 end |
#post(params = {}) ⇒ Object
29 30 31 32 33 |
# File 'lib/catalogapi/request.rb', line 29 def post(params = {}) url = "#{base_url}/json/#{path}" self.response = HTTP.post(url, { json: params }) json_response end |
#required_params(prepend = 'creds_') ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/catalogapi/request.rb', line 75 def required_params(prepend = 'creds_') { "#{prepend}datetime".to_sym => time, "#{prepend}uuid".to_sym => uuid, "#{prepend}checksum".to_sym => checksum, "#{prepend}method".to_sym => method_name.to_s } end |