Hyrule-Compendium 1.0.0 → 3.2.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.
Files changed (3) hide show
  1. checksums.yaml +5 -5
  2. data/lib/hyrule_compendium.rb +124 -11
  3. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c9bdd0411c8aa33e9cc7b3c233a3cfcc2b967b46
4
- data.tar.gz: 45012d54951d67b33e285a39d95e7503efbd3897
2
+ SHA256:
3
+ metadata.gz: 5b2d9bbbe32c561ccf1c94fd12ade17722d271a33099bddd1a12fa322a4cd571
4
+ data.tar.gz: 2e5cee56a637f9cd13683f71d2d879809d64b67c57f44a0e8670153c10c45b21
5
5
  SHA512:
6
- metadata.gz: 668543423094d61e980e28f3cc3c7f435f2259cf421535db477b8418e484f6d937a8dfebc1ea801eb772d89adf46ef95901ab824ed6bd97ca19b3f5efacbfe0b
7
- data.tar.gz: e4e4b0aad79927a573324eb36363821bf53111cddbbf3e00f2297ecdb4341f043b276efd3ae46a066238531f2d2e90f9124c9586264006055bd43226fd513f2f
6
+ metadata.gz: 06d95e42274481077bf1ca024d7b309b7a3a1898111fdf456504c3bf0673afc9169bde5fe8602d3208fde48d028772a8bb8bbe1320545c0c8a99456dd01425a0
7
+ data.tar.gz: e507fb881f8edda640373e5eaf41af0d0eaf926bb3c0d3e33d1450e02e5f90417c10f26ba66b29abfeb53edacdd82d1154eea71e6831d93d8c896788f9d1913f
@@ -1,29 +1,142 @@
1
1
  require "json"
2
2
  require "uri"
3
3
  require "net/http"
4
+ require "open-uri"
4
5
 
5
- def make_req url
6
+ class NoEntryError < StandardError
7
+ # Raised when a given entry does not exist in the compendium
8
+ #
9
+ # Parameters:
10
+ # * `target_entry`: Non-existant input entry that causes error.
11
+ # - type: str, int
12
+
13
+ def initialize(target_entry)
14
+ if target_entry.is_a? String
15
+ super "Entry with name '#{target_entry}' not in compendium."
16
+ elsif target_entry.is_a? Integer
17
+ super "Entry with ID '#{target_entry}' not in compendium."
18
+ end
19
+ end
20
+ end
21
+
22
+ class NoCategoryError < StandardError
23
+ # Raised when a given category does not exist in the compendium
24
+ #
25
+ # Parameters:
26
+ # * `target_category`: Non-existant input category that causes error.
27
+ # - type: str
28
+
29
+ def initialize(target_category)
30
+ super "Category '#{target_category}' not in compendium. Categories are 'creatures', 'equipment', 'materials', 'monsters', and 'treaure'."
31
+ end
32
+ end
33
+
34
+ def make_req url, timeout
6
35
  uri = URI url
7
- res = Net::HTTP.start uri.host, uri.port do |http|
36
+ res = Net::HTTP.start uri.host, uri.port, read_timeout: timeout do |http|
8
37
  req = Net::HTTP::Get.new uri
9
38
  http.request req
10
39
  end
11
- return JSON.parse res.body
40
+ return (JSON.parse res.body)["data"]
12
41
  end
13
42
 
14
- module Hyrule_Compendium
43
+ class Hyrule_Compendium
44
+ # Base class for the Hyrule-Compendium
45
+ #
46
+ # Parameters:
47
+ # * `url`: The base URL of the API server
48
+ # - type: str
49
+ # - default: "http://botw-compendium.herokuapp.com/api/v2"
50
+ # * `default_timeout`: Default seconds to wait for response for all API calling functions until raising `Net::ReadTimeout`.
51
+ # - type: float, int
52
+ # - default: `nil` (no timeout)
53
+ # - notes: If a API calling function has a parameter `timeout`, it will overide this.
15
54
 
16
- $base = "http://botw-compendium.herokuapp.com/api/v1"
55
+ def initialize url: "http://botw-compendium.herokuapp.com/api/v2", default_timeout: nil
56
+ @base = url
57
+ @default_timeout = default_timeout
58
+ end
59
+
60
+ def get_entry entry, timeout=@default_timeout
61
+ # Gets an entry from the compendium.
62
+ #
63
+ # Parameters:
64
+ # * `entry`: The ID or name of the entry to be retrieved.
65
+ # - type: str, int
66
+ # * `timeout`: Seconds to wait for response until raising `Net::ReadTimeout`.
67
+ # - type: float, int
68
+ # - default: `@default_timeout`
69
+ #
70
+ # Returns: The entry's metadata.
71
+ # - type: hash
72
+ #
73
+ # Raises:
74
+ # * `NoEntryError` when the entry is not found.
17
75
 
18
- def Hyrule_Compendium.get_entry entry
19
- return make_req("#{$base}/entry/#{entry.to_s.gsub " ", "%20"}")["data"]
76
+ res = make_req("#{@base}/entry/#{entry.to_s.gsub " ", "%20"}", timeout=timeout)
77
+ if res == {}
78
+ raise NoEntryError.new entry
79
+ end
80
+ return res
20
81
  end
21
82
 
22
- def Hyrule_Compendium.get_category category
23
- return make_req("#{$base}/category/#{category}")["data"]
83
+ def get_category category, timeout=@default_timeout
84
+ # Gets all entries from a category in the compendium.
85
+ #
86
+ # Parameters:
87
+ # * `category`: The name of the category to be retrieved. Must be one of the compendium categories.
88
+ # - type: string
89
+ # - notes:
90
+ # * must be "creatures", "equipment", "materials", "monsters", or "treasure"
91
+ # * the category "creatures" has two sub-categories, as keys: "food" and "non_food"
92
+ # * `timeout`: Seconds to wait for response until raising `Net::ReadTimeout`.
93
+ # - type: float, int
94
+ # - default: `@default_timeout`
95
+ #
96
+ # Returns: All entries in the category.
97
+ # - type: array, hash (for creatures)
98
+ #
99
+ # Raises:
100
+ # * `NoCategoryError` when the category is not found.
101
+
102
+ if !(["creatures", "equipment", "materials", "monsters", "treasure"].include? category)
103
+ raise NoCategoryError.new category
104
+ end
105
+ return make_req "#{@base}/category/#{category}", timeout=timeout
24
106
  end
25
107
 
26
- def Hyrule_Compendium.get_all
27
- return make_req $base
108
+ def get_all timeout=@default_timeout
109
+ # Gets all entries from the compendium.
110
+ #
111
+ # Parameters:
112
+ # * `timeout`: Seconds to wait for response until raising `Net::ReadTimeout`.
113
+ # - type: float, int
114
+ # - default: `@default_timeout`
115
+ #
116
+ # Returns: all items in the compendium with their metadata, nested in categories.
117
+ # - type: hash
118
+
119
+ return make_req @base, timeout
120
+ end
121
+
122
+ def download_entry_image entry, output_file=nil, timeout=@default_timeout
123
+ # Downloads the image of a compendium entry.
124
+ #
125
+ # Parameters:
126
+ # * `entry`: The ID or name of the entry of the image to be downloaded.
127
+ # - type: str, int
128
+ # * `output_file`: The output file's path.
129
+ # - type: str
130
+ # - default: entry's name with a ".png" extension with spaces replaced with underscores
131
+ # * `timeout`: Seconds to wait for server response until raising `Net::ReadTimeout`.
132
+ # - type: float, int
133
+ # - default: `@default_timeout`
134
+
135
+ entry_data = get_entry entry, timeout
136
+ open entry_data["image"] do |image|
137
+ File.open output_file || (entry_data["name"] + ".png").gsub(" ", "_"), "wb" do |file|
138
+ file.write image.read
139
+ end
140
+ end
28
141
  end
29
142
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Hyrule-Compendium
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aarav Borthakur
@@ -10,7 +10,7 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2021-01-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Ruby wrapper for the Hyrule Compendium API
13
+ description: The official ruby wrapper for the Hyrule Compendium API
14
14
  email: gadhaguy13@gmail.com
15
15
  executables: []
16
16
  extensions: []
@@ -36,8 +36,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
36
36
  - !ruby/object:Gem::Version
37
37
  version: '0'
38
38
  requirements: []
39
- rubyforge_project:
40
- rubygems_version: 2.5.2.3
39
+ rubygems_version: 3.0.3
41
40
  signing_key:
42
41
  specification_version: 4
43
42
  summary: Hyrule Compendium API