game_of_thrones_api 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a078977179fabc795d32224b634241e1626bdf99
4
- data.tar.gz: 9fb064b4100378430cf6bb61ec2a43d68bbce462
3
+ metadata.gz: a183c141181b06da9971102948278966afc87107
4
+ data.tar.gz: f71d6fb00c936bf1384c9dc9cf4f348b6107d3d1
5
5
  SHA512:
6
- metadata.gz: 5002c3711cba6322a1fa72f19322b0a89c8c42cb85dcf430eb830c7e251f1a55328ca50e01f7b7c9ae3b88d1648c59112e202e3f957ec274c4283991b0b5701f
7
- data.tar.gz: b38bfb700b90b3ac0f6ee66b78e708ec534a87ce624f76d485943100e336598f7828f22b4d6c82f93c2f45abf1f7ce730988c86e1bc9d82e8da88949c3d25451
6
+ metadata.gz: 7009e890f25fc8619098ede5ad2212d541680db6d9d38bd33f7f25a75fca3d7f9942772e1a50f0da4fb82d1c916cf46adcfbbb14aa53b8dd92b2619d08be3ac6
7
+ data.tar.gz: b7e9aee365b8195b9d60e563a62a15e2d1d6d074a7ff3f3e512fcf2e7c95132d836838d953bbe956baf5fa58efee03affffe4e39d60c0a96e329221fa6d5e6ab
data/README.md CHANGED
@@ -1,12 +1,12 @@
1
1
  # GameOfThronesApi
2
2
 
3
- Something for *Game of Thrones* & *A Song of Fire and Ice* fans.
3
+ Something for **Game of Thrones** & **A Song of Ice and Fire** fans.
4
4
 
5
- API Wrapper using [An API Of Ice And Fire](https://anapioficeandfire.com/)
5
+ A Ruby API Wrapper using [An API Of Ice And Fire](https://anapioficeandfire.com/). All credit goes to **Joakim Skoog** the creator of the API.
6
6
 
7
7
  Still In developement. Basic Functionality. Find Books, Characters and Houses.
8
8
 
9
- ##### TODO: Better filtering, fuzzy find for houses, complete API functionality and of course tests.
9
+ ##### TODO: Implement better filtering, complete API functionality and of course tests.
10
10
 
11
11
  ## Installation
12
12
 
@@ -26,36 +26,44 @@ Or install it yourself as:
26
26
 
27
27
  ## Usage
28
28
 
29
- #### An API Of Ice And Fire is Public, no authentication required
29
+ #### [An API Of Ice And Fire](https://anapioficeandfire.com/) is public, no authentication required
30
30
 
31
- #### Retrieve Books
32
- ```
31
+ #### Retrieve all Books
32
+ ```ruby
33
33
  GameOfThronesApi.get_books
34
34
  ```
35
35
 
36
- #### Find a single book
37
- ```
36
+ #### Find a Book by Title or Partial Title.
37
+ ```ruby
38
38
  GameOfThronesApi.find_book('A Dance with Dragons')
39
- ```
40
39
 
41
- #### Retrieve Characters
40
+ or
41
+
42
+ GameOfThronesApi.find_book('dragon')
42
43
  ```
44
+
45
+ #### Retrieve all Characters
46
+ ```ruby
43
47
  GameOfThronesApi.get_characters
44
48
  ```
45
49
 
46
- #### Find a character
47
- ```
48
- GameOfThronesApi.find_book('Jon Snow')
49
- ```
50
+ #### Find a Character by Name or Partial Name
51
+ ```ruby
52
+ GameOfThronesApi.find_character('Jon Snow')
53
+
54
+ or
50
55
 
51
- #### Retrieve House
56
+ GameOfThronesApi.find_character('baratheon')
52
57
  ```
58
+
59
+ #### Retrieve all Houses
60
+ ```ruby
53
61
  GameOfThronesApi.get_houses
54
62
  ```
55
63
 
56
- #### Find a specific House, must be complete house name. Will implement a fuzzy search in the future.
57
- ```
58
- GameOfThronesApi.find_house("House Targaryen of King's Landing")
64
+ #### Find a House by Name
65
+ ```ruby
66
+ GameOfThronesApi.find_house('targaryen')
59
67
  ```
60
68
 
61
69
  ## Contributing
@@ -1,46 +1,70 @@
1
- require "game_of_thrones_api/version"
2
- require "httparty"
1
+ require 'game_of_thrones_api/version'
2
+ require 'httparty'
3
+
4
+ # There are 3 'get' methods, each method returns an Array of all the records
5
+ # for that specific category.
6
+ #
7
+ # Currently there are 12 Books, 2134 Characters and 444 Family Houses.
8
+ #
9
+ # Each 'get' method is memoized for performance and to reduce API calls.
10
+ #
11
+ # Each 'find' method takes a 'term' and searches the name column of that
12
+ # category for any matches.
3
13
 
4
14
  module GameOfThronesApi
5
15
  include HTTParty
6
16
  format :json
7
17
 
8
- BASE_ENDPOINT = "http://anapioficeandfire.com/api"
18
+ BASE_ENDPOINT = "http://anapioficeandfire.com/api".freeze
19
+
20
+ WORD_PARTICLES = %w(and or the over to the a but of for with).freeze
9
21
 
10
22
  def self.get_books
11
- response = get("#{BASE_ENDPOINT}/books?page=1&pageSize=50")
12
- total_pages = get_page_count(response)
23
+ @get_books ||= begin
24
+ response = get("#{BASE_ENDPOINT}/books?page=1&pageSize=50")
25
+ total_pages = get_page_count(response)
13
26
 
14
- get_all_records('books', response, total_pages)
27
+ get_all_records('books', response, total_pages)
28
+ end
15
29
  end
16
30
 
17
31
  def self.find_book(name)
18
- name = name_query(name)
19
- get("#{BASE_ENDPOINT}/books#{name}").parsed_response
32
+ books ||= GameOfThronesApi.get_books
33
+ query = titleize_query(name)
34
+
35
+ books.select { |book| book['name'].include?(query) }
20
36
  end
21
37
 
22
38
  def self.get_characters
23
- response = get("#{BASE_ENDPOINT}/characters?page=1&pageSize=50")
24
- total_pages = get_page_count(response)
39
+ @get_characters ||= begin
40
+ response = get("#{BASE_ENDPOINT}/characters?page=1&pageSize=50")
41
+ total_pages = get_page_count(response)
25
42
 
26
- get_all_records('characters', response, total_pages)
43
+ get_all_records('characters', response, total_pages)
44
+ end
27
45
  end
28
46
 
29
47
  def self.find_character(name)
30
- name = name_query(name)
31
- get("#{BASE_ENDPOINT}/characters#{name}").parsed_response
48
+ characters ||= GameOfThronesApi.get_characters
49
+ query = titleize_query(name)
50
+
51
+ characters.select { |character| character['name'].include?(query) }
32
52
  end
33
53
 
34
54
  def self.get_houses
35
- response = get("#{BASE_ENDPOINT}/houses?page=1&pageSize=50")
36
- total_pages = get_page_count(response)
55
+ @get_houses ||= begin
56
+ response = get("#{BASE_ENDPOINT}/houses?page=1&pageSize=50")
57
+ total_pages = get_page_count(response)
37
58
 
38
- get_all_records('houses', response, total_pages)
59
+ get_all_records('houses', response, total_pages)
60
+ end
39
61
  end
40
62
 
41
63
  def self.find_house(name)
42
- name = name_query(name)
43
- get("#{BASE_ENDPOINT}/houses#{name}").parsed_response
64
+ houses ||= GameOfThronesApi.get_houses
65
+ query = titleize_query(name)
66
+
67
+ houses.select { |house| house['name'].include?(query) }
44
68
  end
45
69
 
46
70
  module_function
@@ -53,11 +77,30 @@ module GameOfThronesApi
53
77
  term.gsub(' ', '%20')
54
78
  end
55
79
 
80
+ # The pagination information is in the Headers
81
+ # We retrieve the links from the Headers using a regex
82
+ # There will always be 3 links, the last link, redirects to the last page.
83
+ # We take the page number from the last link as our page count.
56
84
  def get_page_count(response)
57
85
  page_links = response.headers['link'].scan(/<(\S+)>/).flatten
58
86
  /\?page\=(\d+)\&/.match(page_links.last)[1].to_i
59
87
  end
60
88
 
89
+ def titleize_query(query)
90
+ split_words =
91
+ query.split.map.with_index do |word, index|
92
+ if WORD_PARTICLES.exclude?(word) || index.zero?
93
+ word.capitalize
94
+ else
95
+ word
96
+ end
97
+ end
98
+
99
+ split_words.join(' ')
100
+ end
101
+
102
+ # The API results are paginated, just looping through the pages to collect
103
+ # all the records associated with the category.
61
104
  def get_all_records(category, response, total_pages, page = 1)
62
105
  characters = response.parsed_response
63
106
 
@@ -1,3 +1,3 @@
1
1
  module GameOfThronesApi
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: game_of_thrones_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Tam
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-06 00:00:00.000000000 Z
11
+ date: 2016-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty