Module: GameOfThronesApi

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

Overview

There are 3 ‘get’ methods, each method returns an Array of all the records for that specific category.

Currently there are 12 Books, 2134 Characters and 444 Family Houses.

Each ‘get’ method is memoized for performance and to reduce API calls.

Each ‘find’ method takes a ‘term’ and searches the name column of that category for any matches.

Constant Summary collapse

BASE_ENDPOINT =
"http://anapioficeandfire.com/api".freeze
WORD_PARTICLES =
%w(and or the over to the a but of for with).freeze
VERSION =
"0.4.0"

Class Method Summary collapse

Class Method Details

.find_book(name) ⇒ Object



31
32
33
34
35
36
# File 'lib/game_of_thrones_api.rb', line 31

def self.find_book(name)
  books ||= GameOfThronesApi.get_books
  query   = titleize_query(name)

  books.select { |book| book['name'].include?(query) }
end

.find_character(name) ⇒ Object



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

def self.find_character(name)
  characters ||= GameOfThronesApi.get_characters
  query        = titleize_query(name)

  characters.select { |character| character['name'].include?(query) }
end

.find_house(name) ⇒ Object



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

def self.find_house(name)
  houses ||= GameOfThronesApi.get_houses
  query    = titleize_query(name)

  houses.select { |house| house['name'].include?(query) }
end

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

The API results are paginated, just looping through the pages to collect all the records associated with the category.



104
105
106
107
108
109
110
111
112
# File 'lib/game_of_thrones_api.rb', line 104

def get_all_records(category, records, total_pages, page = 1)
  if total_pages >= page
    page    += 1
    records += get("#{BASE_ENDPOINT}/#{category}?page=#{page}&pageSize=50").parsed_response
    get_all_records(category, records, total_pages, page)
  else
    records
  end
end

.get_booksObject



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

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

    get_all_records('books', response.parsed_response, total_pages)
  end
end

.get_charactersObject



38
39
40
41
42
43
44
45
# File 'lib/game_of_thrones_api.rb', line 38

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

    get_all_records('characters', response.parsed_response, total_pages)
  end
end

.get_housesObject



54
55
56
57
58
59
60
61
# File 'lib/game_of_thrones_api.rb', line 54

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

    get_all_records('houses', response.parsed_response, total_pages)
  end
end

.get_page_count(response) ⇒ Object

The pagination information is in the Headers We retrieve the links from the Headers using a regex There will always be 3 links, the last link, redirects to the last page. We take the page number from the last link as our page count.



84
85
86
87
# File 'lib/game_of_thrones_api.rb', line 84

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



72
73
74
# File 'lib/game_of_thrones_api.rb', line 72

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

.titleize_query(query) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/game_of_thrones_api.rb', line 89

def titleize_query(query)
  split_words =
    query.split.map.with_index do |word, index|
      if !WORD_PARTICLES.include?(word) || index.zero?
        word.capitalize
      else
        word
      end
    end

  split_words.join(' ')
end

.uri_escape(term) ⇒ Object



76
77
78
# File 'lib/game_of_thrones_api.rb', line 76

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