Class: JAPI::Trebek

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

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/japi/trebek.rb', line 52

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



73
74
75
76
77
# File 'lib/japi/trebek.rb', line 73

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/japi/trebek.rb', line 29

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



10
11
12
13
14
15
16
17
# File 'lib/japi/trebek.rb', line 10

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