Class: Opera::MobileStoreSDK::APIObjectList
- Inherits:
-
Object
- Object
- Opera::MobileStoreSDK::APIObjectList
- Includes:
- Enumerable
- Defined in:
- lib/opera/mobile_store_sdk/api_object_list.rb
Overview
Our version of Array… which will request each page upon iteration… see stackoverflow.com/questions/1571349/can-the-array-be-reinvented-in-ruby
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #[](n) ⇒ Object
- #api_call_duration ⇒ Object
- #api_response_datetime ⇒ Object
- #api_response_parsing_duration ⇒ Object
- #each ⇒ Object
- #found_rows ⇒ Object (also: #total_count)
- #includes(included_fields = []) ⇒ Object
-
#initialize(klass, options = {}) ⇒ APIObjectList
constructor
A new instance of APIObjectList.
- #length ⇒ Object
- #offset(given_offset) ⇒ Object
- #page(given_page) ⇒ Object
- #per(given_item_count) ⇒ Object
- #sdk_tms ⇒ Object
- #timestamp ⇒ Object
- #to_a ⇒ Object
- #updated_at ⇒ Object
-
#where(given_options = {}) ⇒ Object
Chainable Methods ——————————————————.
Constructor Details
#initialize(klass, options = {}) ⇒ APIObjectList
Returns a new instance of APIObjectList.
15 16 17 18 |
# File 'lib/opera/mobile_store_sdk/api_object_list.rb', line 15 def initialize(klass, = {}) @klass = klass = .with_indifferent_access end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/opera/mobile_store_sdk/api_object_list.rb', line 13 def end |
Instance Method Details
#[](n) ⇒ Object
56 57 58 |
# File 'lib/opera/mobile_store_sdk/api_object_list.rb', line 56 def [](n) response.body[n] end |
#api_call_duration ⇒ Object
36 37 38 |
# File 'lib/opera/mobile_store_sdk/api_object_list.rb', line 36 def api_call_duration sdk_tms[:api_calling].real end |
#api_response_datetime ⇒ Object
44 45 46 |
# File 'lib/opera/mobile_store_sdk/api_object_list.rb', line 44 def api_response_datetime response.env[:opera_api_response_datetime] end |
#api_response_parsing_duration ⇒ Object
40 41 42 |
# File 'lib/opera/mobile_store_sdk/api_object_list.rb', line 40 def api_response_parsing_duration sdk_tms[:api_response_parsing].real end |
#each ⇒ Object
60 61 62 |
# File 'lib/opera/mobile_store_sdk/api_object_list.rb', line 60 def each 0.upto(length - 1) { |idx| yield self[idx] } end |
#found_rows ⇒ Object Also known as: total_count
24 25 26 |
# File 'lib/opera/mobile_store_sdk/api_object_list.rb', line 24 def found_rows response.env[:found_rows] end |
#includes(included_fields = []) ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/opera/mobile_store_sdk/api_object_list.rb', line 125 def includes(included_fields = []) params = included_fields.map(&:to_s).map(&:downcase) .select { |x| %w(original_images billing eula adult subsites compatibility profit rating).include? x } .map { |x| x == "billing" ? "show_billing" : x } .inject({}) { |hash, param_name| hash[param_name] = "1"; hash } self.class.new @klass, .merge(params) end |
#length ⇒ Object
20 21 22 |
# File 'lib/opera/mobile_store_sdk/api_object_list.rb', line 20 def length response.body.count end |
#offset(given_offset) ⇒ Object
117 118 119 |
# File 'lib/opera/mobile_store_sdk/api_object_list.rb', line 117 def offset(given_offset) where .merge(offset: given_offset) end |
#page(given_page) ⇒ Object
113 114 115 |
# File 'lib/opera/mobile_store_sdk/api_object_list.rb', line 113 def page(given_page) where .merge(page: given_page) end |
#per(given_item_count) ⇒ Object
121 122 123 |
# File 'lib/opera/mobile_store_sdk/api_object_list.rb', line 121 def per(given_item_count) where .merge(items: given_item_count) end |
#sdk_tms ⇒ Object
29 30 31 32 33 34 |
# File 'lib/opera/mobile_store_sdk/api_object_list.rb', line 29 def sdk_tms { api_calling: response.env[:opera_api_calling_tms], api_response_parsing: response.env[:opera_api_response_parsing_tms], } end |
#timestamp ⇒ Object
48 49 50 |
# File 'lib/opera/mobile_store_sdk/api_object_list.rb', line 48 def response.env[:timestamp] end |
#to_a ⇒ Object
64 65 66 |
# File 'lib/opera/mobile_store_sdk/api_object_list.rb', line 64 def to_a response.body end |
#updated_at ⇒ Object
52 53 54 |
# File 'lib/opera/mobile_store_sdk/api_object_list.rb', line 52 def updated_at Time.at(page.).to_datetime end |
#where(given_options = {}) ⇒ Object
Chainable Methods ——————————————————
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/opera/mobile_store_sdk/api_object_list.rb', line 69 def where( = {}) = .with_indifferent_access ######################################################################## # Validate (& filter out conflicting) options: # Product list API can use added_after/updated_after parameters: if .include? :updated_after if [:updated_after].is_a? String begin [:updated_after] = DateTime.parse([:updated_after]) rescue ArgumentError => e raise Opera::MobileStoreSDK::Errors::APIParamsError, "Parameter 'updated_after' is not a date (#{given_options[:updated_after]})" end elsif ![:updated_after].respond_to? :to_date raise Opera::MobileStoreSDK::Errors::APIParamsError, "Parameter 'updated_after' is not a date (#{given_options[:updated_after]})" end end if .key? :added_after if [:added_after].is_a? String begin [:added_after] = DateTime.parse([:added_after]) rescue ArgumentError => e raise Opera::MobileStoreSDK::Errors::APIParamsError, "Parameter 'added_after' is not a date (#{given_options[:added_after]})" end elsif ![:added_after].respond_to? :to_date raise Opera::MobileStoreSDK::Errors::APIParamsError, "Parameter 'added_after' is not a date (#{given_options[:added_after]})" end # Delete conflicting parameters: .delete :updated_after end ######################################################################## self.class.new @klass, .merge() end |