class Hyrule_Compendium

Public Class Methods

new(url: "http://botw-compendium.herokuapp.com/api/v2", default_timeout: nil) click to toggle source

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.
# 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

Public Instance Methods

download_entry_image(entry, output_file=nil, timeout=@default_timeout) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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