Can load some basic business info from YELP

This commit is contained in:
2022-12-18 00:34:47 -08:00
parent 00814dd7e4
commit 5e2e9e2752
11 changed files with 151 additions and 8 deletions

29
src/modules/api/yelp.cr Normal file
View File

@@ -0,0 +1,29 @@
require "http/client"
require "io"
module Wince::Yelp
extend self
@@token : String = {{ read_file("./api_key") }}
def search_businesses(search : String, location : String)
params = URI::Params.encode({
location: location,
term: search,
sort_by: "best_match"
})
uri = URI.new(
scheme: "https",
host: "api.yelp.com",
path: "/v3/businesses/search",
query: params
)
headers = HTTP::Headers{ "Authorization" => "Bearer " + @@token }
response = HTTP::Client.get(uri, headers)
end
end
#response = Wince::Yelp.search_businesses("miss saigon", "94610")
#puts response.status_code
#puts response.body

View File

@@ -0,0 +1,26 @@
module Wince
@[Gtk::UiTemplate(resource: "/wince/ui/compiled/templates.ui", children: %w(businessName businessRating businessHours businessWebsite))]
class BusinessRow < Gtk::ListBoxRow
include Gtk::WidgetTemplate
@name : Gtk::Label
@rating : Gtk::Label
@hours : Gtk::Label
@website : Gtk::LinkButton
def initialize(name : String, rating : String, hours : String, website : String)
super()
@name = Gtk::Label.cast(template_child("businessName"))
@rating = Gtk::Label.cast(template_child("businessRating"))
@hours = Gtk::Label.cast(template_child("businessHours"))
@website = Gtk::LinkButton.cast(template_child("businessWebsite"))
@name.text = name
@rating.text = rating
@hours.text = hours
@website.uri = website
@website.label = "view on yelp"
end
end
end

View File

@@ -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))

View File

@@ -9,6 +9,7 @@ module Wince
SEARCH_ENTRY = Gtk::Entry.cast(B_UI["searchEntry"])
LOCATION_ENTRY = Gtk::Entry.cast(B_UI["locationEntry"])
SEARCH_BUTTON = Gtk::Button.cast(B_UI["searchButton"])
BUSINESS_LIST = Gtk::ListBox.cast(B_UI["searchResults"])
APP = Adw::Application.new("dev.wince", Gio::ApplicationFlags::None)
end