Class: YahooShoppingSearch::Responses::Item

Inherits:
Base
  • Object
show all
Defined in:
lib/yahoo_shopping_search/responses/item.rb

Constant Summary collapse

MAX_RESULTS_COUNT =

start + results sum’s mixium is 1000

1000
DEFAULT_RESULTS_COUNT =
20

Instance Method Summary collapse

Methods inherited from Base

#[], #body, #each, #items, #last, #params

Constructor Details

#initialize(body, params = {}) ⇒ Item

Returns a new instance of Item.

Parameters:

  • body (String)
  • params (Hash) (defaults to: {})


13
14
15
16
# File 'lib/yahoo_shopping_search/responses/item.rb', line 13

def initialize(body, params = {})
  @body = JSON.parse(body)
  @params = params
end

Instance Method Details

#client_classClassName

Returns:

  • (ClassName)


75
76
77
# File 'lib/yahoo_shopping_search/responses/item.rb', line 75

def client_class
  ::YahooShoppingSearch::Clients::Item
end

#has_next_page?Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
# File 'lib/yahoo_shopping_search/responses/item.rb', line 19

def has_next_page?
  if total_results_count >= MAX_RESULTS_COUNT
    MAX_RESULTS_COUNT >= start_position + results_count
  else
    total_results_count - start_position - results_count > 0
  end
end

#next_page!Array<YahooShoppingSearch::Resources::Item>



28
29
30
31
32
33
34
# File 'lib/yahoo_shopping_search/responses/item.rb', line 28

def next_page!
  raise NextPageNotFound.new('Next page not found') unless has_next_page?
  @body = client_class.search(next_search_condition).body
  @params = next_search_condition

  items
end

#next_search_conditionHash

Returns:

  • (Hash)


57
58
59
60
61
62
# File 'lib/yahoo_shopping_search/responses/item.rb', line 57

def next_search_condition
  condition = search_condition
  condition['start'] = start_position + (condition['results'] || DEFAULT_RESULTS_COUNT)

  condition
end

#resource_classClassName

Returns:

  • (ClassName)


70
71
72
# File 'lib/yahoo_shopping_search/responses/item.rb', line 70

def resource_class
  ::YahooShoppingSearch::Resources::Item
end

#results_countInteger

Returns:

  • (Integer)


42
43
44
# File 'lib/yahoo_shopping_search/responses/item.rb', line 42

def results_count
  @body['totalResultsReturned']
end

#search_conditionHash

Returns:

  • (Hash)


52
53
54
# File 'lib/yahoo_shopping_search/responses/item.rb', line 52

def search_condition
  params
end

#sourcesArray<Hash>

Returns:

  • (Array<Hash>)


65
66
67
# File 'lib/yahoo_shopping_search/responses/item.rb', line 65

def sources
  body['hits']
end

#start_positionInteger

Returns:

  • (Integer)


37
38
39
# File 'lib/yahoo_shopping_search/responses/item.rb', line 37

def start_position
  @body['firstResultsPosition']
end

#total_results_countInteger

Returns:

  • (Integer)


47
48
49
# File 'lib/yahoo_shopping_search/responses/item.rb', line 47

def total_results_count
  @body['totalResultsAvailable']
end