Class: JAPI::Trebek
- Inherits:
-
Object
- Object
- JAPI::Trebek
- Defined in:
- lib/japi/trebek.rb
Overview
Interface for creating and requesting Clues & Categories
Class Method Summary collapse
-
.categories(options = {}) ⇒ Array<Category>
Get a list of categories from the service.
-
.category(id) ⇒ Category
Get clues of a single category from the service.
-
.clues(options = {}) ⇒ Array<Clue>
Get a list of clues from the service.
-
.random(count = 1) ⇒ Array<Clue>
Get a random clue from the service.
Class Method Details
.categories(options = {}) ⇒ Array<Category>
Get a list of categories from the service
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/japi/trebek.rb', line 57 def categories( = {}) allowed_keys = ['count', 'offset'] .keys.each do |key| unless allowed_keys.include?(key.to_s) = "#{key} is not allowed, please only use the following options: #{allowed_keys.join(', ')}" raise InvalidParamError.new() end end query = URI.encode_www_form() response = JSON.parse(open(base_url + 'categories/?' + query).read) response.map do |category| Category.new(category) end end |
.category(id) ⇒ Category
Get clues of a single category from the service
78 79 80 81 82 |
# File 'lib/japi/trebek.rb', line 78 def category(id) query = URI.encode_www_form(id: id) response = JSON.parse(open(base_url + 'category/?' + query).read) Category.new(response) end |
.clues(options = {}) ⇒ Array<Clue>
Get a list of clues from the service
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/japi/trebek.rb', line 33 def clues( = {}) allowed_keys = ['value', 'category', 'max_date', 'min_date', 'offset'] .keys.each do |key| unless allowed_keys.include?(key.to_s) = "#{key} is not allowed, please only use the following options: #{allowed_keys.join(', ')}" raise InvalidParamError.new() end end query = URI.encode_www_form() response = JSON.parse(open(base_url + 'clues/?' + query).read) response.map do |clue| Clue.new(clue) end end |
.random(count = 1) ⇒ Array<Clue>
Get a random clue from the service
13 14 15 16 17 18 19 20 |
# File 'lib/japi/trebek.rb', line 13 def random(count = 1) url = base_url + "random/?" + URI.encode_www_form({count: count}) response = JSON.parse(open(url).read) response.map do |clue| Clue.new(clue) end end |