Hyrule-Compendium 2.0.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/hyrule_compendium.rb +93 -5
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8fd41a9a00958a4157972be44116bdfd842b18f5b846a14f35dff39dfb94ff83
4
- data.tar.gz: 9bdabdbd60ff6b8ffa5998d3f65512f4a0c43e1508339bcccf10393e1272b8fa
3
+ metadata.gz: 5e726e2598a53077e75749f3845280b41386921ae08d9d58785dedfc1d390e90
4
+ data.tar.gz: 335d8686b52ed78c8ea2a9595c7ef4729b82eafc636b9a3a53fc6df76936012b
5
5
  SHA512:
6
- metadata.gz: a16f4bf3472aeef0970317c3ecfc73db80731df34f1e8a8829345dec497eb158aae91108886ecf080148f2a0572a85f52ddb265b5e5dfd64c5b611dc29bc0157
7
- data.tar.gz: 6269f5479f726b0cc2f14a64c4deb7bfb1524744b54ecc6de40d1e4dd50eb3f015bacadcc2f4090ee41e8c2ea7da683a6c343a55a3858d3b821b41693baeb400
6
+ metadata.gz: b81886b6ea83a022362d92752d55ff962dda29abf66cab07d24c083e2d33c76b1e960a340ea2dc02d651a24337f47fece34c6c1ca1f5380e1734d8bee5b5b891
7
+ data.tar.gz: 599a5d2797a58970e66a67935468014951a40145f9867f23c018df5f172bc0773adb0ef99345d52c08f84bdfce36b076a1176fa76ccbe78fab65536b4bde89da
@@ -1,6 +1,35 @@
1
1
  require "json"
2
2
  require "uri"
3
3
  require "net/http"
4
+ require "open-uri"
5
+
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
4
33
 
5
34
  def make_req url
6
35
  uri = URI url
@@ -8,23 +37,82 @@ def make_req url
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
43
  class Hyrule_Compendium
15
- def initialize url="http://botw-compendium.herokuapp.com"
16
- @base = url + "/api/v1"
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
+
51
+ def initialize url="http://botw-compendium.herokuapp.com/api/v2"
52
+ @base = url
17
53
  end
18
54
 
19
55
  def get_entry entry
20
- return make_req("#{@base}/entry/#{entry.to_s.gsub " ", "%20"}")["data"]
56
+ # Gets an entry from the compendium.
57
+ #
58
+ # Parameters:
59
+ # * `entry`: The ID or name of the entry to be retrieved.
60
+ # - type: str, int
61
+ #
62
+ # Returns: The entry's metadata.
63
+ # type: hash
64
+ #
65
+ # Raises:
66
+ # * `NoEntryError` when the entry is not found.
67
+
68
+ res = make_req "#{@base}/entry/#{entry.to_s.gsub " ", "%20"}"
69
+ if res == {}
70
+ raise NoEntryError.new entry
71
+ end
72
+ return res
21
73
  end
22
74
 
23
75
  def get_category category
24
- return make_req("#{@base}/category/#{category}")["data"]
76
+ # Gets all entries from a category in the compendium.
77
+ #
78
+ # Parameters:
79
+ # * `category`: The name of the category to be retrieved. Must be one of the compendium categories.
80
+ # - type: string
81
+ # - notes:
82
+ # * must be "creatures", "equipment", "materials", "monsters", or "treasure"
83
+ # * the category "creatures" has two sub-categories, as keys: "food" and "non_food"
84
+ #
85
+ # Raises:
86
+ # * `NoCategoryError` when the category is not found.
87
+
88
+ if !(["creatures", "equipment", "materials", "monsters", "treasure"].include? category)
89
+ raise NoCategoryError.new category
90
+ end
91
+ return make_req "#{@base}/category/#{category}"
25
92
  end
26
93
 
27
94
  def get_all
95
+ # Gets all entries from the compendium.
96
+ #
97
+ # Returns: all items in the compendium with their metadata, nested in categories.
98
+ # type: hash
99
+
28
100
  return make_req @base
29
101
  end
102
+
103
+ def download_entry_image entry, output_file
104
+ # Downloads the image of a compendium entry.
105
+ #
106
+ # Parameters:
107
+ # * `entry`: The ID or name of the entry of the image to be downloaded.
108
+ # - type: str, int
109
+ # * `output_file`: The output file's path.
110
+ # - type: str
111
+
112
+ open (get_entry entry)["image"] do |image|
113
+ File.open output_file, "wb" do |file|
114
+ file.write image.read
115
+ end
116
+ end
117
+ end
30
118
  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: 2.0.1
4
+ version: 3.0.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: []