Can load some basic business info from YELP
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
require "json"
|
||||
|
||||
require "../templates/businessrow.cr"
|
||||
require "../api/yelp.cr"
|
||||
|
||||
module Wince
|
||||
@@main_window_id = 0_u32
|
||||
@@business_rows = [] of BusinessRow
|
||||
|
||||
def activate(app : Adw::Application)
|
||||
main_window = APP.window_by_id(@@main_window_id)
|
||||
@@ -12,14 +18,49 @@ module Wince
|
||||
|
||||
SEARCH_BUTTON.clicked_signal.connect do
|
||||
handle_search
|
||||
end
|
||||
end
|
||||
|
||||
window.present
|
||||
end
|
||||
|
||||
def yelp_response_to_business_rows(response : String)
|
||||
businesses = JSON.parse(response)["businesses"].as_a
|
||||
|
||||
businesses.map do |business|
|
||||
name = business["name"].as_s? || ""
|
||||
rating = business["rating"].as_f.to_s
|
||||
hours = business["is_closed"].as_bool.to_s
|
||||
website = business["url"].as_s? || ""
|
||||
|
||||
BusinessRow.new(name, rating, hours, website)
|
||||
end
|
||||
end
|
||||
|
||||
def clear_business_rows
|
||||
@@business_rows.each { |row| BUSINESS_LIST.remove(row) }
|
||||
end
|
||||
|
||||
def handle_search
|
||||
puts "searched for " + SEARCH_ENTRY.buffer.text
|
||||
puts "location for " + LOCATION_ENTRY.buffer.text
|
||||
search = SEARCH_ENTRY.buffer.text
|
||||
location = LOCATION_ENTRY.buffer.text
|
||||
|
||||
if !search || !location
|
||||
return
|
||||
end
|
||||
|
||||
response = Yelp.search_businesses(search, location)
|
||||
|
||||
if response.status_code != 200
|
||||
puts "api call error"
|
||||
puts response.body
|
||||
return #TODO: show a toast here
|
||||
end
|
||||
|
||||
clear_business_rows()
|
||||
@@business_rows = yelp_response_to_business_rows(response.body)
|
||||
@@business_rows.each do |row|
|
||||
BUSINESS_LIST.append(row)
|
||||
end
|
||||
end
|
||||
|
||||
APP.activate_signal.connect(->activate(Adw::Application))
|
||||
|
||||
Reference in New Issue
Block a user