Class: Pandarus::APIBase
- Inherits:
-
Footrest::Client
- Object
- Footrest::Client
- Pandarus::APIBase
- Defined in:
- lib/pandarus/api_base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#initialize(*args) ⇒ APIBase
constructor
A new instance of APIBase.
-
#mixed_request(method, path, query_params, form_params, headers) ⇒ Object
Swagger allows for each param to be a query param or a form param, which is a bit unusual because in the 99% case, all params will be of one type or the other for a single Canvas request.
- #page_params_load(method, path) ⇒ Object
- #page_params_store(method, path, response = @response.env) ⇒ Object
- #parse_page_params!(url) ⇒ Object
- #remember_key(method, path) ⇒ Object
- #underscored_merge_opts(opts, base) ⇒ Object
- #was_last_page?(method, path) ⇒ Boolean
Constructor Details
#initialize(*args) ⇒ APIBase
Returns a new instance of APIBase.
9 10 11 12 |
# File 'lib/pandarus/api_base.rb', line 9 def initialize(*args) @pagination_params = {} super end |
Instance Attribute Details
#response ⇒ Object (readonly)
Returns the value of attribute response.
7 8 9 |
# File 'lib/pandarus/api_base.rb', line 7 def response @response end |
Instance Method Details
#mixed_request(method, path, query_params, form_params, headers) ⇒ Object
Swagger allows for each param to be a query param or a form param, which is a bit unusual because in the 99% case, all params will be of one type or the other for a single Canvas request. In order to accommodate the 1% case, however, we allow for mixed requests in general.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/pandarus/api_base.rb', line 18 def mixed_request(method, path, query_params, form_params, headers) @response = connection.send(method) do |r| if query_params.empty? r.path = fullpath(path) else r.url(fullpath(path), query_params) end r.body = form_params unless form_params.empty? r.headers = headers if headers end @response.body end |
#page_params_load(method, path) ⇒ Object
51 52 53 |
# File 'lib/pandarus/api_base.rb', line 51 def page_params_load(method, path) @pagination_params[remember_key(method, path)] end |
#page_params_store(method, path, response = @response.env) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/pandarus/api_base.rb', line 43 def page_params_store(method, path, response=@response.env) @pagination_params[remember_key(method, path)] = { next: parse_page_params!(response[:next_page]), current: parse_page_params!(response[:current_page]), last: parse_page_params!(response[:last_page]) } end |
#parse_page_params!(url) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/pandarus/api_base.rb', line 35 def parse_page_params!(url) return nil if url.nil? uri = URI.parse url Hash[URI.decode_www_form(uri.query)] rescue URI::InvalidURIError nil end |
#remember_key(method, path) ⇒ Object
31 32 33 |
# File 'lib/pandarus/api_base.rb', line 31 def remember_key(method, path) "#{method}:#{path}" end |
#underscored_merge_opts(opts, base) ⇒ Object
61 62 63 |
# File 'lib/pandarus/api_base.rb', line 61 def underscored_merge_opts(opts, base) base.merge(opts).merge(underscored_flatten_hash(opts)) end |
#was_last_page?(method, path) ⇒ Boolean
55 56 57 58 59 |
# File 'lib/pandarus/api_base.rb', line 55 def was_last_page?(method, path) params = @pagination_params[remember_key(method, path)] return false if params.nil? || params[:current].nil? || params[:last].nil? return (params[:current] == params[:last]) end |