Class: AcnhCritterpedia::API
- Inherits:
-
Object
- Object
- AcnhCritterpedia::API
- Defined in:
- lib/acnh_critterpedia/api.rb
Instance Attribute Summary collapse
-
#hemisphere ⇒ Object
Returns the value of attribute hemisphere.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #critters_available_now(critter_type) ⇒ Object
-
#initialize(hemisphere) ⇒ API
constructor
A new instance of API.
- #list_critter_names(critter_list) ⇒ Object
- #search_critter_by_name(name, critter_type) ⇒ Object
- #search_critters_by_month(month, critter_type) ⇒ Object
Constructor Details
#initialize(hemisphere) ⇒ API
Returns a new instance of API.
4 5 6 7 |
# File 'lib/acnh_critterpedia/api.rb', line 4 def initialize(hemisphere) @url = "https://acnhapi.com/v1a" @hemisphere = hemisphere end |
Instance Attribute Details
#hemisphere ⇒ Object
Returns the value of attribute hemisphere.
2 3 4 |
# File 'lib/acnh_critterpedia/api.rb', line 2 def hemisphere @hemisphere end |
#url ⇒ Object
Returns the value of attribute url.
2 3 4 |
# File 'lib/acnh_critterpedia/api.rb', line 2 def url @url end |
Instance Method Details
#critters_available_now(critter_type) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/acnh_critterpedia/api.rb', line 41 def critters_available_now(critter_type) req_url = "#{url}/#{critter_type}/" data = HTTParty.get(req_url) critter_list = data.select do |critter| critter["availability"]["month-array-#{hemisphere}"].include?(Time.now.month) && critter["availability"]["time-array"].include?(Time.now.hour) end list_critter_names(critter_list) end |
#list_critter_names(critter_list) ⇒ Object
52 53 54 |
# File 'lib/acnh_critterpedia/api.rb', line 52 def list_critter_names(critter_list) critter_list.map {|critter| critter["name"]["name-USen"]} end |
#search_critter_by_name(name, critter_type) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/acnh_critterpedia/api.rb', line 9 def search_critter_by_name(name, critter_type) req_url = "#{url}/#{critter_type}/#{name}" data = HTTParty.get(req_url) if data. == "Not Found" "error" else critter_hash = { name: data["name"]["name-USen"], location: data["availability"]["location"], catch_phrase: data["catch-phrase"], month_range: data["availability"]["month-#{hemisphere}"], is_all_year: data["availability"]["isAllYear"], time_range: data["availability"]["time"], is_all_day: data["availability"]["isAllDay"] } critter = AcnhCritterpedia::Critter.new(critter_hash) end end |
#search_critters_by_month(month, critter_type) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/acnh_critterpedia/api.rb', line 30 def search_critters_by_month(month, critter_type) req_url = "#{url}/#{critter_type}/" data = HTTParty.get(req_url) critter_list = data.select do |critter| critter["availability"]["month-array-#{hemisphere}"].include?(month) end list_critter_names(critter_list) end |