beryl 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3b009e4e353f9efecf2cb768b1e71126f5367edaf534559c0c6ef1b9d02312d1
4
+ data.tar.gz: 4e0fa2a870b6bec3f12f5b5d5e6b840571fff39e3181e8965f0c48e6d7032f4d
5
+ SHA512:
6
+ metadata.gz: a3834be4e596b8260b5a8d246b97e58ecfb771c74a8d0b5c6c49e8598a8cd9a5d483b7f1a5341e41a3238678c756f0ddbc23b1ae7cde8ea8af312102e214c60f
7
+ data.tar.gz: 3c0ca7a52f50ba852be97f031e89cd80df1ccd056ac8e4af4b9cbcff71c86efb32fe28a8d8c5980cf2f04514f4c1db343893e3ec9d15413eb2328a44ad21fd9b
@@ -0,0 +1,54 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
51
+
52
+ .idea/
53
+ /sandbox.rb
54
+ /.yardoc
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 1.16.5
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in beryl.gemspec
6
+ gemspec
@@ -0,0 +1,29 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ ast (2.4.0)
5
+ bowser (1.0.4)
6
+ opal (>= 0.7.0, < 0.12.0)
7
+ hike (1.2.3)
8
+ opal (0.11.3)
9
+ ast (>= 2.3.0)
10
+ hike (~> 1.2)
11
+ parser (= 2.3.3.1)
12
+ sourcemap (~> 0.1.0)
13
+ parser (2.3.3.1)
14
+ ast (~> 2.2)
15
+ puma (3.12.0)
16
+ rack (2.0.5)
17
+ sourcemap (0.1.1)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ bowser!
24
+ opal!
25
+ puma!
26
+ rack!
27
+
28
+ BUNDLED WITH
29
+ 1.16.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Bart Blast
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,39 @@
1
+ # Beryl
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/beryl`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'beryl'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install beryl
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/beryl.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,27 @@
1
+ require 'opal'
2
+ require 'rake/testtask'
3
+ require 'bowser'
4
+ require 'bundler/gem_tasks'
5
+
6
+ desc 'Build the app to build/app.js'
7
+ task :build do
8
+ Opal.append_path 'app'
9
+ Opal.append_path 'lib'
10
+ Dir.mkdir('build') unless File.exist?('build')
11
+ File.binwrite 'build/app.js', Opal::Builder.build('frontend_app').to_s
12
+ end
13
+
14
+ desc 'Build and run the app'
15
+ task :run do
16
+ Rake::Task['build'].invoke
17
+ sh 'bundle exec rackup --port 3000 --host 0.0.0.0'
18
+ end
19
+
20
+ desc 'Test the app'
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.libs << 'lib'
24
+ t.test_files = FileList['test/**/*_test.rb']
25
+ end
26
+
27
+ task :default => :test
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org' do
2
+ gem 'bowser'
3
+ gem 'opal'
4
+ gem 'puma'
5
+ gem 'rack'
6
+ end
@@ -0,0 +1,73 @@
1
+ require 'opal'
2
+ require 'native'
3
+ require 'event_loop'
4
+ require 'serializer'
5
+ require 'virtual_dom'
6
+
7
+ puts 'Wow, running opal!'
8
+
9
+ def div(props = {}, &children)
10
+ node('div', props, children ? children.call : [])
11
+ end
12
+
13
+ def input(props = {}, &children)
14
+ node('input', props, children ? children.call : [])
15
+ end
16
+
17
+ def link(text, props = {}, &children)
18
+ node('a', props, [text(text)])
19
+ end
20
+
21
+ def node(type, props = {}, children)
22
+ {
23
+ type: type,
24
+ props: props,
25
+ children: children
26
+ }
27
+ end
28
+
29
+ def span(props = {}, &children)
30
+ node('span', props, children ? children.call : [])
31
+ end
32
+
33
+ def text(value, props = {}, &children)
34
+ node('text', props.merge(nodeValue: value), children ? children.call : [])
35
+ end
36
+
37
+ def onload(&block)
38
+ `window.onload = block;`
39
+ end
40
+
41
+ def element(state)
42
+ div(id: 'container') {[
43
+ input(value: 'foo', type: 'text'),
44
+ span() {[
45
+ text(' Foo ' + state[:counter].to_s + ' ')
46
+ ]},
47
+ link(' Increment ', onClick: [:IncrementClicked, key_1: 1, key_2: 2]),
48
+ link(' Load ', onClick: [:LoadClicked, key_1: 1, key_2: 2]),
49
+ div {[
50
+ text(state[:content])
51
+ ]}
52
+ ]}
53
+ end
54
+
55
+ class Interval
56
+ def initialize(time = 0, &block)
57
+ @interval = `setInterval(function(){#{block.call}}, time)`
58
+ end
59
+
60
+ def stop
61
+ `clearInterval(#@interval)`
62
+ end
63
+ end
64
+
65
+ onload do
66
+ document = Native(`window.document`)
67
+ parentDom = document.getElementById('root')
68
+
69
+ state = { counter: 0, content: 'here we will load something' }
70
+ event_loop = EventLoop.new(parentDom, state)
71
+ event_loop.process
72
+ event_loop.render
73
+ end
@@ -0,0 +1,38 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "beryl/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "beryl"
8
+ spec.version = Beryl::VERSION
9
+ spec.authors = ["Bart Blast"]
10
+ spec.email = ["bart@bartblast.com"]
11
+
12
+ spec.summary = %q{Web framework}
13
+ spec.description = %q{Web framework}
14
+ spec.homepage = "https://github.com/bartblast/beryl"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.16"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "minitest", "~> 5.0"
38
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "beryl"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ require 'rack'
2
+ require_relative 'lib/app'
3
+
4
+ use Rack::Static, :urls => ['/build']
5
+ run App.new
@@ -0,0 +1,33 @@
1
+ require 'json'
2
+ require_relative 'command_handler'
3
+ require_relative 'serializer'
4
+
5
+ class App
6
+ HTML = <<~HEREDOC
7
+ <!DOCTYPE html>
8
+ <html>
9
+ <head>
10
+ <script src='build/app.js'></script>
11
+ </head>
12
+ <body>
13
+ <div id="root"></div>
14
+ </body>
15
+ </html>
16
+ HEREDOC
17
+
18
+ def call (env)
19
+ req = Rack::Request.new(env)
20
+ case req.path_info
21
+ when '/rock/command'
22
+ [200, { 'Content-Type' => 'application/json; charset=utf-8' }, [run_command(req)]]
23
+ else
24
+ [200, { 'Content-Type' => 'text/html; charset=utf-8' }, [HTML]]
25
+ end
26
+ end
27
+
28
+ def run_command(req)
29
+ json = JSON.parse(req.body.read)
30
+ result = CommandHandler.new.handle(json['type'].to_sym, json['payload'])
31
+ Serializer.serialize(result)
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ require "beryl/version"
2
+
3
+ module Beryl
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,3 @@
1
+ module Beryl
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'net/http'
2
+
3
+ class CommandHandler
4
+ def handle(type, payload)
5
+ case type
6
+ when :FetchData
7
+ uri = URI('https://www.onet.pl/')
8
+ [:DataFetched, data: Net::HTTP.get(uri).force_encoding("UTF-8")]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,53 @@
1
+ require 'virtual_dom'
2
+ require 'task'
3
+ require 'bowser/http'
4
+ require 'serializer'
5
+
6
+ class EventLoop
7
+ def initialize(root, state)
8
+ @messages = []
9
+ @root = root
10
+ @state = state
11
+ end
12
+
13
+ def push(message)
14
+ puts 'adding message...'
15
+ @messages << message
16
+ end
17
+
18
+ def render
19
+ VirtualDOM.new.render(self, element(@state), @root)
20
+ end
21
+
22
+ def process
23
+ while @messages.any?
24
+ message = @messages.shift
25
+ result = transition(message.first, message.last)
26
+ @state = result.is_a?(Array) ? result.first : result
27
+ command = result.is_a?(Array) ? result[1] : nil
28
+ run_command(result[1], result[2]) if command
29
+ render
30
+ end
31
+ end
32
+
33
+ def run_command(type, payload)
34
+ puts 'running command'
35
+ Task.new do
36
+ Bowser::HTTP.fetch('/rock/command', method: :post, data: { type: type, payload: Serializer.serialize(payload) })
37
+ .then(&:json) # JSONify the response
38
+ .then { |response| puts response }
39
+ .catch { |exception| warn exception.message }
40
+ end
41
+ end
42
+
43
+ def transition(type, payload)
44
+ case type
45
+ when :IncrementClicked
46
+ @state.merge(counter: @state[:counter] + 1)
47
+ when :LoadClicked
48
+ [@state, :FetchData, key_1: 1, key_2: 2]
49
+ when :LoadSuccess
50
+ @state.merge(content: payload[:data])
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,36 @@
1
+ require 'json'
2
+
3
+ module Serializer
4
+ extend self
5
+
6
+ def serialize(obj, json = true)
7
+ result = obj.is_a?(Array) ? array(obj) : item(obj)
8
+ json ? result.to_json : result
9
+ end
10
+
11
+ private
12
+
13
+ def array(obj)
14
+ obj.each_with_object([]) { |item, result| result << serialize(item, false) }
15
+ end
16
+
17
+ def hash(obj)
18
+ obj.each_with_object({}) { |(k, v), result| result[k] = serialize(v, false) }
19
+ end
20
+
21
+ def item(obj)
22
+ { class: obj.class.to_s, value: value(obj) }
23
+ end
24
+
25
+ def properties(obj)
26
+ obj.instance_variables.each_with_object({}) do |var, result|
27
+ result[var] = serialize(obj.instance_variable_get(var), false)
28
+ end
29
+ end
30
+
31
+ def value(obj)
32
+ return obj.to_s if %w(Float Integer Number String Symbol).include?(obj.class.to_s)
33
+ return hash(obj) if obj.is_a?(Hash)
34
+ properties(obj)
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ class Task
2
+ def initialize(&block)
3
+ `setTimeout(function(){#{block.call}}, 0)`
4
+ end
5
+ end
@@ -0,0 +1,45 @@
1
+ require 'native'
2
+ require 'event_loop'
3
+
4
+ class VirtualDOM
5
+ def render(event_loop, element, parentDom, replace = true)
6
+ document = Native(`window.document`)
7
+ dom = element[:type] == 'text' ? document.createTextNode('') : document.createElement(element[:type])
8
+
9
+ add_event_listeners(element, dom, event_loop)
10
+ set_attributes(element, dom)
11
+
12
+ childElements = element[:children] || [];
13
+ childElements.each { |child| render(event_loop, child, dom, false) }
14
+
15
+ update_dom(parentDom, dom, replace)
16
+ end
17
+
18
+ private
19
+
20
+ def add_event_listeners(element, dom, event_loop)
21
+ listener_props = element[:props].select { |key, _value| listener?(key) }
22
+ listener_props.each do |key, value|
23
+ event_type = key.downcase[2..-1]
24
+ dom.addEventListener(event_type, lambda { event_loop.push(value); event_loop.process })
25
+ end
26
+ end
27
+
28
+ def listener?(key)
29
+ key.start_with?('on')
30
+ end
31
+
32
+ def set_attributes(element, dom)
33
+ attribute_props = element[:props].reject { |key, _value| listener?(key) }
34
+ attribute_props.each { |key, value| dom[key] = value }
35
+ end
36
+
37
+ def update_dom(parent_dom, dom, replace)
38
+ if replace
39
+ while (parent_dom.firstChild) do
40
+ parent_dom.removeChild(parent_dom.firstChild)
41
+ end
42
+ end
43
+ parent_dom.appendChild(dom)
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: beryl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bart Blast
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: Web framework
56
+ email:
57
+ - bart@bartblast.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - app-Gemfile
70
+ - app/frontend_app.rb
71
+ - beryl.gemspec
72
+ - bin/console
73
+ - bin/setup
74
+ - config.ru
75
+ - lib/app.rb
76
+ - lib/beryl.rb
77
+ - lib/beryl/version.rb
78
+ - lib/command_handler.rb
79
+ - lib/event_loop.rb
80
+ - lib/serializer.rb
81
+ - lib/task.rb
82
+ - lib/virtual_dom.rb
83
+ homepage: https://github.com/bartblast/beryl
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.7.6
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Web framework
107
+ test_files: []