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

1
.gitignore vendored
View File

@@ -4,3 +4,4 @@
/.shards/
*.dwarf
/data/ui/compiled
api_key

View File

@@ -10,7 +10,9 @@ You gotta have blueprint installed
pacman -S blueprint-compiler crystal shards libadwaita
```
Then you can test run with
Then you gotta get an API key from YELP and put it in the file `api_key` at the root of the project
Then you can run the program with
```
crystal run src/gtktest.cr

View File

@@ -29,5 +29,15 @@ Adw.ApplicationWindow mainWindow {
icon-name: "edit-find";
}
}
Gtk.ScrolledWindow {
vexpand: true;
Gtk.ListBox searchResults {
margin-top: 14;
margin-start: 14;
margin-end: 14;
}
}
}
}
}

27
data/ui/templates.blp Normal file
View File

@@ -0,0 +1,27 @@
using Gtk 4.0;
using Adw 1;
template Wince-BusinessRow : Gtk.ListBoxRow {
styles ["card"]
margin-bottom: 14;
Gtk.Box {
orientation: vertical;
margin-start: 8;
margin-top: 8;
Gtk.Label businessName {
styles ["heading"]
xalign: 0;
}
Gtk.Label businessRating {
xalign: 0;
}
Gtk.Label businessHours {
xalign: 0;
}
Gtk.LinkButton businessWebsite {
halign: start;
}
}
}

View File

@@ -2,5 +2,6 @@
<gresources>
<gresource prefix="/wince">
<file compressed="true" preprocess="xml-stripblanks">ui/compiled/main.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/compiled/templates.ui</file>
</gresource>
</gresources>

View File

@@ -1,12 +1,13 @@
# NOTICE: This lockfile contains some overrides from shard.override.yml
version: 2.0
shards:
gi-crystal:
git: https://github.com/hugopl/gi-crystal.git
version: 0.14.0
version: 0.14.0+git.commit.569f8d4b77effe312c642fc60929000e3e9514e2
gtk4:
gtk4: # Overridden
git: https://github.com/hugopl/gtk4.cr.git
version: 0.12.0
version: 0.12.0+git.commit.ef2eb03b38b25ea2780e83cf50af3a6807ac6293
libadwaita:
git: https://github.com/geopjr/libadwaita.cr.git

4
shard.override.yml Normal file
View File

@@ -0,0 +1,4 @@
dependencies:
gtk4:
github: hugopl/gtk4.cr
branch: master

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