Class: RakutenWebService::SearchResult

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rakuten_web_service/search_result.rb

Instance Method Summary collapse

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

#eachObject



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_resultObject



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

Returns:

  • (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(options)
  new_params = @params.dup
  if options.is_a? Hash
    key, sort_order = *(options.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 options.to_s == 'standard'
    new_params[:sort] = 'standard' 
  else 
    raise ArgumentError, "Invalid Sort Option: #{options.inspect}"
  end
  self.class.new(new_params, @resource_class)
end

#paramsObject



30
31
32
33
# File 'lib/rakuten_web_service/search_result.rb', line 30

def params
  return {} if @params.nil?
  @params.dup 
end