module GameOfThronesApi
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.
Constants
- BASE_ENDPOINT
- VERSION
- WORD_PARTICLES
Public Class Methods
find_book(name)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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_books()
click to toggle source
# 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_characters()
click to toggle source
# 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_houses()
click to toggle source
# 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
Public Instance Methods
get_all_records(category, records, total_pages, page = 1)
click to toggle source
The API results are paginated, just looping through the pages to collect all the records associated with the category.
# 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_page_count(response)
click to toggle source
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.
# 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)
click to toggle source
# File lib/game_of_thrones_api.rb, line 72 def name_query(filter) "/?name=#{uri_escape(filter)}" end
titleize_query(query)
click to toggle source
# 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)
click to toggle source
# File lib/game_of_thrones_api.rb, line 76 def uri_escape(term) term.gsub(' ', '%20') end