class AcnhInformations::Api
Scrape informations from 'acnhapi' api
Some tools for the project
Constants
- BASE_URL
The base URL to scrape
- CATEGORIES
All available categories
Public Class Methods
get_by_name(category, name)
click to toggle source
Get a thing by an approximative name @param [String] category The category to get the object @param [String] name the approximative name @return [Hash, Boolean] False if the program didn't find anything, or the thing hash
# File lib/acnh_informations/tools.rb, line 13 def self.get_by_name(category, name) result = false return false unless valid?(category.to_s) scrape(category.to_s).each do |element| element[1][:name].each_value do |language_name| result = element[1] if language_name.match(/#{name.to_s}/) break if result end end result end
scrape(category, id = nil)
click to toggle source
Scrape informations from API
@param [Symbol, String] category The category to find the object @param id [Symbol, String, NilClass] The id to get @return [Object] The result as a Hash @see Api#valid?
# File lib/acnh_informations/api.rb, line 36 def self.scrape(category, id = nil) return false unless valid?(category.to_s, id.to_s) JSON.parse(RestClient.get("#{BASE_URL}/v1/#{category.to_s}/#{id ? id.to_s : ""}"), :symbolize_names => true) end
valid?(category, id = nil)
click to toggle source
Verify if the object exists
@param [Symbol, String] category The category to find the object @param id [Symbol, String, NilClass] The id to get @return [Boolean] True or False, if the wanted research exists
# File lib/acnh_informations/api.rb, line 20 def self.valid?(category, id = nil) return false unless id.to_s.ascii_only? RestClient.get("#{BASE_URL}/v1/#{category.to_s}/#{id ? id.to_s : ""}") true rescue RestClient::NotFound false end