Class: RakutenWebService::SearchResult
- Inherits:
-
Object
- Object
- RakutenWebService::SearchResult
- Includes:
- Enumerable
- Defined in:
- lib/rakuten_web_service/search_result.rb
Instance Method Summary collapse
- #each ⇒ Object
- #fetch_result ⇒ Object
- #has_next_page? ⇒ Boolean
-
#initialize(params, resource_class) ⇒ SearchResult
constructor
A new instance of SearchResult.
- #order(options) ⇒ Object
- #params ⇒ Object
Constructor Details
#initialize(params, resource_class) ⇒ SearchResult
Returns a new instance of SearchResult.
5 6 7 8 9 |
# File 'lib/rakuten_web_service/search_result.rb', line 5 def initialize(params, resource_class) @params = params.dup @resource_class = resource_class @client = RakutenWebService::Client.new(@resource_class.endpoint) end |
Instance Method Details
#each ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rakuten_web_service/search_result.rb', line 16 def each params = @params response = query begin resources = @resource_class.parse_response(response.body) resources.each do |resource| yield resource end break unless has_next_page? response = query(params.merge('page' => response.body['page'] + 1)) end while(response) end |
#fetch_result ⇒ Object
11 12 13 14 |
# File 'lib/rakuten_web_service/search_result.rb', line 11 def fetch_result response = query @resource_class.parse_response(response.body) end |
#has_next_page? ⇒ Boolean
35 36 37 |
# File 'lib/rakuten_web_service/search_result.rb', line 35 def has_next_page? @response.body['page'] && @response.body['page'] < @response.body['pageCount'] end |
#order(options) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rakuten_web_service/search_result.rb', line 39 def order() new_params = @params.dup if .is_a? Hash key, sort_order = *(.to_a.last) key = camelize(key.to_s) new_params[:sort] = case sort_order.to_s.downcase when 'desc' "-#{key}" when 'asc' "+#{key}" end elsif .to_s == 'standard' new_params[:sort] = 'standard' else raise ArgumentError, "Invalid Sort Option: #{.inspect}" end self.class.new(new_params, @resource_class) end |
#params ⇒ Object
30 31 32 33 |
# File 'lib/rakuten_web_service/search_result.rb', line 30 def params return {} if @params.nil? @params.dup end |