Class: Rowling::Client
- Inherits:
-
Object
- Object
- Rowling::Client
- Defined in:
- lib/rowling/client.rb
Constant Summary collapse
- CONFIGURATION_KEYS =
[:api_key, :raw, :retries]
Instance Method Summary collapse
- #base_template ⇒ Object
- #build_uri(args) ⇒ Object
- #check_errors(response) ⇒ Object
- #find_book_by_isbn(isbn) ⇒ Object
- #get_booklists(args = {}) ⇒ Object
- #get_categories(args = {}) ⇒ Object
- #get_classes(args = {}) ⇒ Object
- #get_dates(args = {}) ⇒ Object
- #get_detailed_version(book) ⇒ Object
-
#initialize(args = {}) ⇒ Client
constructor
A new instance of Client.
- #make_request(args = {}, tries = 0) ⇒ Object
- #search_books(args = {}) ⇒ Object
- #set_defaults ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Client
Returns a new instance of Client.
6 7 8 9 10 11 |
# File 'lib/rowling/client.rb', line 6 def initialize(args={}) CONFIGURATION_KEYS.each do |key| send("#{key}=", args[key]) end set_defaults end |
Instance Method Details
#base_template ⇒ Object
20 21 22 |
# File 'lib/rowling/client.rb', line 20 def base_template template = Addressable::Template.new("http://api.usatoday.com/open/bestsellers/books{/segments*}{?query*}") end |
#build_uri(args) ⇒ Object
122 123 124 125 126 |
# File 'lib/rowling/client.rb', line 122 def build_uri(args) query = { "api_key" => self.api_key } query.merge!(args[:query]) if args[:query] base_template.({segments: args[:segments], query: query}).to_s end |
#check_errors(response) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/rowling/client.rb', line 128 def check_errors(response) if response.code == 503 raise Rowling::Response503Error elsif response.code == 400 raise Rowling::Response400Error elsif response.code == 403 raise Rowling::Response403Error elsif response.code != 200 raise Rowling::ResponseError, "Request Failed. Code #{response.code}." else response end end |
#find_book_by_isbn(isbn) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rowling/client.rb', line 55 def find_book_by_isbn(isbn) segments = ["titles", isbn] if self.raw make_request({ segments: segments }) else begin book_response = make_request({ segments: segments }) Rowling::Book.new(book_response["Title"]) rescue Rowling::Response503Error, Rowling::Response400Error => e return nil end end end |
#get_booklists(args = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rowling/client.rb', line 69 def get_booklists(args={}) date_segments = [:year, :month, :day].map do |segment| args.delete(segment) end segments = ["booklists"].concat(date_segments.compact) response = make_request({ segments: segments, args: args}) if self.raw response elsif lists = response["BookLists"] lists.map{ |list| BookList.new(list) } else {} end end |
#get_categories(args = {}) ⇒ Object
29 30 31 32 |
# File 'lib/rowling/client.rb', line 29 def get_categories(args={}) category_response = make_request({segments: "categories", query: args}) category_response["Categories"] end |
#get_classes(args = {}) ⇒ Object
24 25 26 27 |
# File 'lib/rowling/client.rb', line 24 def get_classes(args={}) class_response = make_request({segments: "classes", query: args}) class_response["Classes"] end |
#get_dates(args = {}) ⇒ Object
34 35 36 37 |
# File 'lib/rowling/client.rb', line 34 def get_dates(args={}) date_response = make_request({segments: "dates", query: args}) date_response["Dates"] end |
#get_detailed_version(book) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rowling/client.rb', line 84 def get_detailed_version(book) if book.title_api_url segments = book.title_api_url book_response = make_request({ segments: segments }) if self.raw book_response else Rowling::Book.new(book_response["Title"]) end else raise Rowling::ResponseError "Can't retrieve details for book without title api url set" end end |
#make_request(args = {}, tries = 0) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/rowling/client.rb', line 98 def make_request(args={}, tries=0) if self.api_key uri = build_uri(args) response = HTTParty.get(uri) if self.raw response else if tries < self.retries begin check_errors(response) rescue Rowling::Response403Error tries += 1 sleep(1) make_request(args, tries) end else check_errors(response) end end else raise Rowling::NoAPIKeyError end end |
#search_books(args = {}) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rowling/client.rb', line 39 def search_books(args={}) segments = "titles" book_response = make_request({segments: segments, query: args}) if self.raw book_response else if titles = book_response["Titles"] titles.map do |title| Rowling::Book.new(title) end else [] end end end |
#set_defaults ⇒ Object
15 16 17 18 |
# File 'lib/rowling/client.rb', line 15 def set_defaults @raw ||= false @retries ||= 0 end |