Module: GameOfThronesApi

Includes:
HTTParty
Defined in:
lib/game_of_thrones_api.rb,
lib/game_of_thrones_api/version.rb

Constant Summary collapse

BASE_ENDPOINT =
"http://anapioficeandfire.com/api"
VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.find_book(name) ⇒ Object



17
18
19
20
# File 'lib/game_of_thrones_api.rb', line 17

def self.find_book(name)
  name = name_query(name)
  get("#{BASE_ENDPOINT}/books#{name}").parsed_response
end

.find_character(name) ⇒ Object



29
30
31
32
# File 'lib/game_of_thrones_api.rb', line 29

def self.find_character(name)
  name = name_query(name)
  get("#{BASE_ENDPOINT}/characters#{name}").parsed_response
end

.find_house(name) ⇒ Object



41
42
43
44
# File 'lib/game_of_thrones_api.rb', line 41

def self.find_house(name)
  name = name_query(name)
  get("#{BASE_ENDPOINT}/houses#{name}").parsed_response
end

.get_all_records(category, response, total_pages, page = 1) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/game_of_thrones_api.rb', line 61

def get_all_records(category, response, total_pages, page = 1)
  characters  = response.parsed_response

  while total_pages >= page
    page       += 1
    characters += get("#{BASE_ENDPOINT}/#{category}?page=#{page}&pageSize=50").parsed_response
  end

  characters
end

.get_booksObject



10
11
12
13
14
15
# File 'lib/game_of_thrones_api.rb', line 10

def self.get_books
  response    = get("#{BASE_ENDPOINT}/books?page=1&pageSize=50")
  total_pages = get_page_count(response)

  get_all_records('books', response, total_pages)
end

.get_charactersObject



22
23
24
25
26
27
# File 'lib/game_of_thrones_api.rb', line 22

def self.get_characters
  response    = get("#{BASE_ENDPOINT}/characters?page=1&pageSize=50")
  total_pages = get_page_count(response)

  get_all_records('characters', response, total_pages)
end

.get_housesObject



34
35
36
37
38
39
# File 'lib/game_of_thrones_api.rb', line 34

def self.get_houses
  response    = get("#{BASE_ENDPOINT}/houses?page=1&pageSize=50")
  total_pages = get_page_count(response)

  get_all_records('houses', response, total_pages)
end

.get_page_count(response) ⇒ Object



56
57
58
59
# File 'lib/game_of_thrones_api.rb', line 56

def get_page_count(response)
  page_links = response.headers['link'].scan(/<(\S+)>/).flatten
  /\?page\=(\d+)\&/.match(page_links.last)[1].to_i
end

.name_query(filter) ⇒ Object



48
49
50
# File 'lib/game_of_thrones_api.rb', line 48

def name_query(filter)
  "/?name=#{uri_escape(filter)}"
end

.uri_escape(term) ⇒ Object



52
53
54
# File 'lib/game_of_thrones_api.rb', line 52

def uri_escape(term)
  term.gsub(' ', '%20')
end