Class: Hyrule_Compendium

Inherits:
Object
  • Object
show all
Defined in:
lib/hyrule_compendium.rb

Instance Method Summary collapse

Constructor Details

#initialize(url: "http://botw-compendium.herokuapp.com/api/v2", default_timeout: nil) ⇒ Hyrule_Compendium

Base class for the Hyrule-Compendium

Parameters:

* `url`: The base URL of the API server
    - type: str
    - default: "http://botw-compendium.herokuapp.com/api/v2"
* `default_timeout`: Default seconds to wait for response for all API calling functions until raising `Net::ReadTimeout`.
    - type: float, int
    - default: `nil` (no timeout)
    - notes: If a API calling function has a parameter `timeout`, it will overide this.


55
56
57
58
# File 'lib/hyrule_compendium.rb', line 55

def initialize url: "http://botw-compendium.herokuapp.com/api/v2", default_timeout: nil
    @base = url
    @default_timeout = default_timeout
end

Instance Method Details

#download_entry_image(entry, output_file = nil, timeout = @default_timeout) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/hyrule_compendium.rb', line 122

def download_entry_image entry, output_file=nil, timeout=@default_timeout
    # Downloads the image of a compendium entry.
    # 
    # Parameters:
    #   * `entry`: The ID or name of the entry of the image to be downloaded.
    #       - type: str, int
    #   * `output_file`: The output file's path.
    #       - type: str
    #       - default: entry's name with a ".png" extension with spaces replaced with underscores
    #   * `timeout`: Seconds to wait for server response until raising `Net::ReadTimeout`.
    #       - type: float, int
    #       - default: `@default_timeout`

    entry_data = get_entry entry, timeout
    open entry_data["image"] do |image|
        File.open output_file || (entry_data["name"] + ".png").gsub(" ", "_"), "wb" do |file|
            file.write image.read
        end
    end
end

#get_all(timeout = @default_timeout) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/hyrule_compendium.rb', line 108

def get_all timeout=@default_timeout
    # Gets all entries from the compendium.
    #
    # Parameters:
    #   * `timeout`: Seconds to wait for response until raising `Net::ReadTimeout`.
    #       - type: float, int
    #       - default: `@default_timeout`
    #
    # Returns: all items in the compendium with their metadata, nested in categories.
    #   - type: hash

    return make_req @base, timeout
end

#get_category(category, timeout = @default_timeout) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/hyrule_compendium.rb', line 83

def get_category category, timeout=@default_timeout
    # Gets all entries from a category in the compendium.
    # 
    # Parameters:
    #   * `category`: The name of the category to be retrieved. Must be one of the compendium categories.
    #       - type: string
    #       - notes: 
    #           * must be "creatures", "equipment", "materials", "monsters", or "treasure"
    #           * the category "creatures" has two sub-categories, as keys: "food" and "non_food"
    #   * `timeout`: Seconds to wait for response until raising `Net::ReadTimeout`.
    #       - type: float, int
    #       - default: `@default_timeout`
    #
    # Returns: All entries in the category.
    #   - type: array, hash (for creatures)
    #
    # Raises:
    #   * `NoCategoryError` when the category is not found.

    if !(["creatures", "equipment", "materials", "monsters", "treasure"].include? category)
        raise NoCategoryError.new category
    end
    return make_req "#{@base}/category/#{category}", timeout=timeout
end

#get_entry(entry, timeout = @default_timeout) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hyrule_compendium.rb', line 60

def get_entry entry, timeout=@default_timeout
    # Gets an entry from the compendium.
    #
    # Parameters:
    #   * `entry`: The ID or name of the entry to be retrieved.
    #       - type: str, int
    #   * `timeout`: Seconds to wait for response until raising `Net::ReadTimeout`.
    #       - type: float, int
    #       - default: `@default_timeout`
    #
    # Returns: The entry's metadata.
    #   - type: hash
    #
    # Raises:
    #   * `NoEntryError` when the entry is not found.

    res = make_req("#{@base}/entry/#{entry.to_s.gsub " ", "%20"}", timeout=timeout)
    if res == {}
        raise NoEntryError.new entry
    end
    return res
end