Class: Bestsellers::List

Inherits:
Object
  • Object
show all
Includes:
Configuration, HTTParty
Defined in:
lib/nytimes_bestsellers/client.rb

Constant Summary collapse

BOOKS_URL =
"http://api.nytimes.com/svc/books/v2/lists"

Instance Attribute Summary

Attributes included from Configuration

#api_key

Instance Method Summary collapse

Methods included from Configuration

#configure, #reset

Constructor Details

#initializeList

Returns a new instance of List.



15
16
17
# File 'lib/nytimes_bestsellers/client.rb', line 15

def initialize
  reset
end

Instance Method Details

#age_groupsObject



88
89
90
# File 'lib/nytimes_bestsellers/client.rb', line 88

def age_groups
  HTTParty.get(BOOKS_URL + "/age-groups?api-key=#{api_key}")
end

#bestseller_lists_overview(o = {}) ⇒ Object



63
64
65
66
67
# File 'lib/nytimes_bestsellers/client.rb', line 63

def bestseller_lists_overview(o = {})
  date = (Date.parse o[:date] || Date.today).strftime('%Y-%m-%e')

  HTTParty.get(BOOKS_URL + "/overview?published_date=#{date}&api-key=#{api_key}")
end

#find_list_namesObject



84
85
86
# File 'lib/nytimes_bestsellers/client.rb', line 84

def find_list_names
  HTTParty.get(BOOKS_URL + "/names?api-key=#{api_key}")
end

#get_list(list_name, o = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/nytimes_bestsellers/client.rb', line 24

def get_list(list_name, o = {})
  url = BOOKS_URL.clone

  if o[:date]
    date = o[:date]
    url << "/#{date}"
  end
  
  url << "/#{list_name}?"

  [:offset, :sort_by, :sort_order].each do |thing|
    set_urlparam(url, thing, o)
  end

  url << "&api-key=#{api_key}"

  HTTParty.get(url)
end

#search_list(list_name, o = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/nytimes_bestsellers/client.rb', line 43

def search_list(list_name, o = {})

  url = BOOKS_URL.clone + "?list-name=#{list_name}"

  date = if o[:date]
    Date.parse(o[:date]) 
  else
    Date.today
  end.strftime('%Y-%m-%e')
  url << "&date=#{date}"

  [:isbn, :published_date, :rank, :rank_last_week, :weeks_on_list].each do |thing|
    set_urlparam(url, thing, o)
  end

  url << "&api-key=#{api_key}"

 HTTParty.get(url)
end

#set_urlparam(url, name, options) ⇒ Object



19
20
21
22
# File 'lib/nytimes_bestsellers/client.rb', line 19

def set_urlparam(url, name, options)
 return unless options[name]
 url << "&#{name.to_s.gsub('_','-')}=#{options[name]}"
end

#single_history(o = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/nytimes_bestsellers/client.rb', line 69

def single_history(o = {})

  url = BOOKS_URL.clone + "/best-sellers/history"

  url << "?"

  [:author, :publisher, :title, :age_group, :contributor, :isbn, :price].each do |thing|
    set_urlparam(url, thing, o)
  end

  url << "&api-key=#{api_key}"

  HTTParty.get(url)
end