Class: TorgMailruApi::Listing

Inherits:
Object
  • Object
show all
Includes:
Enumerable, TorgMailruApi
Defined in:
lib/torg_mailru_api.rb

Constant Summary

Constants included from TorgMailruApi

API_VERSION, ENDPOINT_URI, VERSION

Instance Attribute Summary

Attributes included from Configuration

#access_token, #adapter, #cache, #logger, #proxy

Instance Method Summary collapse

Methods included from Configuration

#configure

Methods included from Connection

#connection, #get

Methods included from APIRequest

#decamelize_hash_keys, #get, #listing

Methods included from APIMethods

#categories, #category, #category_children, #category_filter, #category_hits, #category_models, #category_newmodels, #category_offers, #category_parameters, #model, #model_offers, #model_outlets, #model_parameters, #offer, #region, #region_children, #region_suggest, #regions, #search, #seller, #seller_outlets, #seller_reviews, #vendor, #vendors

Constructor Details

#initialize(resource, params = nil) ⇒ Listing

Returns a new instance of Listing.



129
130
131
132
133
134
135
# File 'lib/torg_mailru_api.rb', line 129

def initialize resource, params=nil
    @resource = resource
    @params = params.nil? ? {} : params
    @params[:page] = @params.fetch(:page, 1)
    @items = []
    @more = true
end

Instance Method Details

#eachObject



137
138
139
140
141
142
143
# File 'lib/torg_mailru_api.rb', line 137

def each
  return to_enum(:each) unless block_given?
  while true
    break unless fetch_next_page if @items.empty?
    yield @items.shift
  end
end

#fetch_next_pageObject



145
146
147
148
149
150
151
152
153
# File 'lib/torg_mailru_api.rb', line 145

def fetch_next_page
  if @more
    r = TorgMailruApi.get @resource, @params
    @more = (r.results_total-r.results_per_page*r.page) > 0
    @items = r.listing
    @params[:page] += 1
    true
  end
end