-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #277 from scarpe-team/shoes_api_self
Use a single Scarpe::App instance as self nearly everywhere
- Loading branch information
Showing
21 changed files
with
285 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
def printish(msg) | ||
#$stderr.puts msg | ||
File.open("/tmp/shoesy_stuff.txt", "a") do |f| | ||
f.write(msg + "\n") | ||
end | ||
end | ||
|
||
Shoes.app do | ||
printish "Self top: #{self.inspect}" # It's an instance of Shoes::App, yes | ||
stack do | ||
printish "Self in stack: #{self.inspect}" # Yup, here too | ||
end | ||
button("Clickity") do | ||
alert("You clicked me!") | ||
printish "Self in button handler: #{self.inspect}" # And here too | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
class Scarpe | ||
class DocumentRoot < Scarpe::Widget | ||
include Scarpe::Background | ||
|
||
class DocumentRoot < Scarpe::Slot | ||
def initialize | ||
super | ||
|
||
create_display_widget | ||
end | ||
|
||
# This can be absolutely huge in console output, and it's frequently printed. | ||
# The default inspect string can be absolutely huge in console output, and it's frequently printed. | ||
def inspect | ||
"<Scarpe::DocumentRoot>" | ||
end | ||
|
||
alias_method :info, :puts | ||
|
||
def all_widgets | ||
out = [] | ||
|
||
to_add = self.children | ||
until to_add.empty? | ||
out.concat(to_add) | ||
to_add = to_add.flat_map(&:children).compact | ||
end | ||
|
||
out | ||
end | ||
|
||
# We can add various ways to find widgets here. | ||
# These are sort of like Shoes selectors, used for testing. | ||
def find_widgets_by(*specs) | ||
widgets = all_widgets | ||
app = Scarpe::App.instance | ||
specs.each do |spec| | ||
if spec.is_a?(Class) | ||
widgets.select! { |w| spec === w } | ||
elsif spec.is_a?(Symbol) | ||
s = spec.to_s | ||
case s[0] | ||
when "$" | ||
begin | ||
# I'm not finding a global_variable_get or similar... | ||
global_value = eval s | ||
widgets = [global_value] | ||
rescue | ||
raise "Error getting global variable: #{spec.inspect}" | ||
end | ||
when "@" | ||
if app.instance_variables.include?(spec) | ||
widgets = [app.instance_variable_get(spec)] | ||
else | ||
raise "Can't find top-level instance variable: #{spec.inspect}!" | ||
end | ||
else | ||
end | ||
else | ||
raise("Don't know how to find widgets by #{spec.inspect}!") | ||
end | ||
end | ||
widgets | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
require "net/http" | ||
require "openssl" | ||
require "nokogiri" | ||
|
||
class Scarpe::Widget | ||
def download(url) | ||
Thread.new do | ||
begin | ||
uri = URI(url) | ||
http = Net::HTTP.new(uri.host, uri.port) | ||
http.use_ssl = true | ||
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | ||
|
||
request = Net::HTTP::Get.new(uri.request_uri) | ||
|
||
http.request(request) do |response| | ||
case response | ||
when Net::HTTPSuccess | ||
# content = response.body | ||
|
||
# headers = response.header | ||
|
||
html_get_title(content) | ||
else | ||
Scarpe.error("Failed to download content. Response code: #{response.code}") | ||
end | ||
end | ||
rescue StandardError => e | ||
Scarpe.error("Error occurred while downloading: #{e.message}") | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def html_get_title(content) | ||
doc = Nokogiri::HTML(content) | ||
|
||
title = doc.at_css("title")&.text&.strip || "" | ||
|
||
# headings = doc.css("h1").map(&:text) | ||
|
||
title | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
class Scarpe::Slot < Scarpe::Widget | ||
include Scarpe::Background | ||
include Scarpe::Border | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.