Class: JAPI::Trebek

Inherits:
Object
  • Object
show all
Defined in:
lib/japi/trebek.rb

Overview

Interface for creating and requesting Clues & Categories

Author:

  • Dean Silfen

Class Method Summary collapse

Class Method Details

.categories(options = {}) ⇒ Array<Category>

Get a list of categories from the service

Parameters:

  • options (Hash{String, Symbol => Fixnum}) (defaults to: {})

    hash of query params for clue endpoint

Options Hash (options):

  • :count (Fixnum)

    amount of categories to return, limited to 100 at a time

  • :offset (Fixnum)

    offsets the starting id of categories returned. Useful in pagination.

Returns:

  • (Array<Category>)

    A list of clues that fit the query params

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/japi/trebek.rb', line 57

def categories(options = {})
  allowed_keys = ['count', 'offset']
  options.keys.each do |key|
    unless allowed_keys.include?(key.to_s)
      message = "#{key} is not allowed, please only use the following options: #{allowed_keys.join(', ')}"
      raise InvalidParamError.new(message)
    end
  end
  query = URI.encode_www_form(options)
  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

Parameters:

  • id (Fixnum)

    id of desired category

Returns:

  • (Category)

    A list of clues that fit the query params



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

Parameters:

  • options (Hash{String, Symbol => Fixnum, DateTime}) (defaults to: {})

    hash of query params for clue endpoint

Options Hash (options):

  • :value (Fixnum)

    the value of the clue in dollars

  • :category (Fixnum)

    the id of the category you want to return

  • :min_date (DateTime)

    earliest date to show, based on original air date

  • :max_date (DateTime)

    latest date to show, based on original air date

  • :offset (Fixnum)

    offsets the returned clues. Useful in pagination

Returns:

  • (Array<Clue>)

    A list of clues that fit the query params

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/japi/trebek.rb', line 33

def clues(options = {})
  allowed_keys = ['value', 'category', 'max_date', 'min_date', 'offset']
  options.keys.each do |key|
    unless allowed_keys.include?(key.to_s)
      message = "#{key} is not allowed, please only use the following options: #{allowed_keys.join(', ')}"
      raise InvalidParamError.new(message)
    end
  end
  query = URI.encode_www_form(options)
  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

Parameters:

  • count (Fixnum) (defaults to: 1)

    amount of clues to return, limited to 100 at a time

Returns:

  • (Array<Clue>)

    A list of random clues



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