class Opera::MobileStoreSDK::APIObjectList

Our version of Array… which will request each page upon iteration… see stackoverflow.com/questions/1571349/can-the-array-be-reinvented-in-ruby

Attributes

options[R]

Public Class Methods

new(klass, options = {}) click to toggle source
# File lib/opera/mobile_store_sdk/api_object_list.rb, line 15
def initialize(klass, options = {})
  @klass = klass
  @options = options.with_indifferent_access
end

Public Instance Methods

[](n) click to toggle source
# File lib/opera/mobile_store_sdk/api_object_list.rb, line 56
def [](n)
  response.body[n]
end
api_call_duration() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() { |self| ... } click to toggle source
# 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() click to toggle source
# File lib/opera/mobile_store_sdk/api_object_list.rb, line 24
def found_rows
  response.env[:found_rows]
end
Also aliased as: total_count
includes(included_fields = []) click to toggle source
# 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, @options.merge(params)
end
length() click to toggle source
# File lib/opera/mobile_store_sdk/api_object_list.rb, line 20
def length
  response.body.count
end
offset(given_offset) click to toggle source
# File lib/opera/mobile_store_sdk/api_object_list.rb, line 117
def offset(given_offset)
  where @options.merge(offset: given_offset)
end
page(given_page) click to toggle source
# File lib/opera/mobile_store_sdk/api_object_list.rb, line 113
def page(given_page)
  where @options.merge(page: given_page)
end
per(given_item_count) click to toggle source
# File lib/opera/mobile_store_sdk/api_object_list.rb, line 121
def per(given_item_count)
  where @options.merge(items: given_item_count)
end
sdk_tms() click to toggle source
# 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() click to toggle source
# File lib/opera/mobile_store_sdk/api_object_list.rb, line 48
def timestamp
  response.env[:timestamp]
end
to_a() click to toggle source
# File lib/opera/mobile_store_sdk/api_object_list.rb, line 64
def to_a
  response.body
end
total_count()
Alias for: found_rows
updated_at() click to toggle source
# File lib/opera/mobile_store_sdk/api_object_list.rb, line 52
def updated_at
  Time.at(page.timestamp).to_datetime
end
where(given_options = {}) click to toggle source

Chainable Methods ——————————————————

# File lib/opera/mobile_store_sdk/api_object_list.rb, line 69
def where(given_options = {})

  given_options = given_options.with_indifferent_access

  ########################################################################
  # Validate (& filter out conflicting) options:

  # Product list API can use added_after/updated_after parameters:

  if given_options.include? :updated_after
    if given_options[:updated_after].is_a? String
      begin
        given_options[:updated_after] = DateTime.parse(given_options[:updated_after])
      rescue ArgumentError => e
        raise Opera::MobileStoreSDK::Errors::APIParamsError,
          "Parameter 'updated_after' is not a date (#{given_options[:updated_after]})"
      end
    elsif !given_options[: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 given_options.key? :added_after
    if given_options[:added_after].is_a? String
      begin
        given_options[:added_after] = DateTime.parse(given_options[:added_after])
      rescue ArgumentError => e
        raise Opera::MobileStoreSDK::Errors::APIParamsError,
          "Parameter 'added_after' is not a date (#{given_options[:added_after]})"
      end
    elsif !given_options[: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:
    given_options.delete :updated_after
  end
  ########################################################################

  self.class.new @klass, @options.merge(given_options)
end

Protected Instance Methods

action_param() click to toggle source
# File lib/opera/mobile_store_sdk/api_object_list.rb, line 165
def action_param
  @_action_param ||= case @klass
    when "Opera::MobileStore::Developer" then "full_developers"
    when "Opera::MobileStore::Compatibility" then "compatibility"
    when "Opera::MobileStore::DeviceModel" then "models"
    when "Opera::MobileStore::DevicePlatform" then "platforms"
    when "Opera::MobileStore::DevicePlatformFamily" then "platform_families"
    else @klass.demodulize.underscore.pluralize
  end
end
response() click to toggle source

# File lib/opera/mobile_store_sdk/api_object_list.rb, line 137
def response

  converted_options = @options.inject({}) do |hsh, keyval|
    key, val = keyval

    hsh[key.to_s] = case key.to_sym
      when :added_after, :updated_after then
        # byebug
        val.to_date.to_s
      else val.to_s
    end

    hsh
  end

  # puts "Options: #{@options.inspect}"
  # byebug

  @_response ||= Opera::MobileStoreSDK.connection.get do |request|
    request.url MobileStoreSDK.config.api_path, {
      action: action_param
    }.merge(converted_options)

    request.options.timeout = MobileStoreSDK.config.api_call_timeout
    request.options.open_timeout = MobileStoreSDK.config.api_call_open_timeout
  end
end