bowser 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 959f43cc08e1a519036ff190c7758c8985c92b75
4
+ data.tar.gz: 65aed9101ecc212d109526e44c5633cae06af2d7
5
+ SHA512:
6
+ metadata.gz: 2be6103c2c7702df5b9930a69b9965960639bf7a4633d625523c122afcef328ec9fba8d52261c69d3d4c7ceb62251ff181365bb28a45216fb7b1a9e7bb38b917
7
+ data.tar.gz: ea3e17f6ee915df58f39e19d2d108080bc3649f79814bce31b815c4cb2bdf48a8fba1dc9cdd86d067b3daa10f8e0465038b80c5bb30a9da80ff8bb7a7d31f994
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,50 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This Code of Conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at [INSERT EMAIL ADDRESS]. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+
45
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
46
+ version 1.3.0, available at
47
+ [http://contributor-covenant.org/version/1/3/0/][version]
48
+
49
+ [homepage]: http://contributor-covenant.org
50
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bowser.gemspec
4
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,25 @@
1
+ License
2
+ =======
3
+
4
+ Copyright (c) 2015 Jamie Gaskins
5
+
6
+ MIT License
7
+
8
+ Permission is hereby granted, free of charge, to any person obtaining
9
+ a copy of this software and associated documentation files (the
10
+ "Software"), to deal in the Software without restriction, including
11
+ without limitation the rights to use, copy, modify, merge, publish,
12
+ distribute, sublicense, and/or sell copies of the Software, and to
13
+ permit persons to whom the Software is furnished to do so, subject to
14
+ the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be
17
+ included in all copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # Bowser
2
+
3
+ It's like `Browser`, but smaller. It provides minimal browser support for libraries and frameworks which don't need the full spectrum of support from [`opal-browser`](https://github.com/opal/opal-browser).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'bowser'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install bowser
20
+
21
+ ## Usage
22
+
23
+ Inside your front-end app:
24
+
25
+ ```ruby
26
+ require 'bowser'
27
+
28
+ Bowser.document # Handle to the current document
29
+ Bowser.window # Handle to the current window
30
+ ```
31
+
32
+ ### AJAX support
33
+
34
+ To make HTTP requests to your API, for example, you can do:
35
+
36
+ ```ruby
37
+ Bowser::HTTP.fetch('/api/things')
38
+ ```
39
+
40
+ It returns a `Promise`, which you can call `then`, `fail`, or `always` on in order to execute a block of code based on success, failure, or either one, respectively.
41
+
42
+ ```ruby
43
+ Bowser::HTTP.fetch(url)
44
+ .then { |response| do_something_with(response.json) }
45
+ .fail { |exception| warn exception.message }
46
+ .always { log "Fetched #{url}" }
47
+ ```
48
+
49
+ The current implementation uses the `Promise` class from the Opal standard library, but it is not fully A+-compliant, so we're in the process of implementing our own.
50
+
51
+ ## Contributing
52
+
53
+ 1. Fork it
54
+ 1. Branch it
55
+ 1. Hack it
56
+ 1. Save it
57
+ 1. Commit it
58
+ 1. Push it
59
+ 1. Pull-request it
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'opal/rspec/rake_task'
3
+ require 'bowser'
4
+
5
+ Opal::RSpec::RakeTask.new(:opal_spec)
6
+
7
+ task :default => :opal_spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bowser"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/bowser.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bowser/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bowser"
8
+ spec.version = Bowser::VERSION
9
+ spec.authors = ["Jamie Gaskins"]
10
+ spec.email = ["jgaskins@gmail.com"]
11
+
12
+ spec.summary = %q{Minimalist browser support for Opal apps}
13
+ spec.homepage = "https://github.com/clearwater-rb/bowser"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "opal", "~> 0.9.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "opal-rspec", "~> 0.5.0"
26
+ end
@@ -0,0 +1,3 @@
1
+ module Bowser
2
+ VERSION = "0.1.0"
3
+ end
data/lib/bowser.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "bowser/version"
2
+ require "opal"
3
+
4
+ module Bowser
5
+ end
6
+
7
+ Opal.append_path(File.expand_path(File.join("..", "..", "opal"), __FILE__).untaint)
@@ -0,0 +1,9 @@
1
+ module Bowser
2
+ module HTTP
3
+ class Event
4
+ def initialize native
5
+ @native = native
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,97 @@
1
+ require 'bowser/http/response'
2
+ require 'bowser/http/event'
3
+
4
+ module Bowser
5
+ module HTTP
6
+ class Request
7
+ attr_reader :method, :url, :data, :headers, :promise
8
+ attr_accessor :response
9
+
10
+ UNSENT = 0
11
+ OPENED = 1
12
+ HEADERS_RECEIVED = 2
13
+ LOADING = 3
14
+ DONE = 4
15
+
16
+ def initialize(method, url, native: `new XMLHttpRequest()`)
17
+ @native = native
18
+ @method = method
19
+ @url = url
20
+
21
+ @ready_state_callbacks = []
22
+ %x{
23
+ #@native.onreadystatechange = #{
24
+ proc do |event|
25
+ @ready_state_callbacks.each do |callback|
26
+ callback.call event
27
+ end
28
+ end
29
+ }
30
+ }
31
+
32
+ @response = Response.new(@native)
33
+ end
34
+
35
+ def on event_name, &block
36
+ handler = proc { |event| block.call(Event.new(event)) }
37
+ case event_name
38
+ when :readystatechange
39
+ @ready_state_callbacks << handler
40
+ when :success
41
+ @ready_state_callbacks << proc { |event|
42
+ block.call Event.new(event) if done? && response.success?
43
+ }
44
+ else
45
+ object = post? ? `#@native.upload` : @native
46
+ `object.addEventListener(event_name, handler)`
47
+ end
48
+
49
+ self
50
+ end
51
+
52
+ def send(data: {}, headers: {})
53
+ `#@native.open(#{method}, #{url})`
54
+ @data = data
55
+ @headers = headers
56
+ headers.each do |attr, value|
57
+ `#@native.setRequestHeader(attr, value)`
58
+ end
59
+ if method == :get || method == :delete
60
+ `#@native.send()`
61
+ else
62
+ `#@native.send(#{JSON.generate data})`
63
+ end
64
+
65
+ self
66
+ end
67
+
68
+ def post?
69
+ method == :post
70
+ end
71
+
72
+ def get?
73
+ method == :get
74
+ end
75
+
76
+ def ready_state
77
+ `#@native.readyState`
78
+ end
79
+
80
+ def sent?
81
+ ready_state >= OPENED
82
+ end
83
+
84
+ def headers_received?
85
+ ready_state >= HEADERS_RECEIVED
86
+ end
87
+
88
+ def loading?
89
+ ready_state == LOADING
90
+ end
91
+
92
+ def done?
93
+ ready_state >= DONE
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,32 @@
1
+ require 'json'
2
+
3
+ module Bowser
4
+ module HTTP
5
+ class Response
6
+ def initialize xhr
7
+ @xhr = xhr
8
+ end
9
+
10
+ def code
11
+ `#@xhr.status`
12
+ end
13
+
14
+ def body
15
+ `#@xhr.response`
16
+ end
17
+
18
+ def json
19
+ body = self.body
20
+ @json ||= JSON.parse(body) if `body !== undefined`
21
+ end
22
+
23
+ def success?
24
+ (200...400).cover? code
25
+ end
26
+
27
+ def fail?
28
+ !success?
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,40 @@
1
+ require 'native'
2
+ require 'promise'
3
+
4
+ require 'bowser/http/request'
5
+
6
+ module Bowser
7
+ module HTTP
8
+ module_function
9
+
10
+ def fetch(url)
11
+ promise = Promise.new
12
+ request = Request.new(:get, url)
13
+
14
+ request.on :success do
15
+ promise.resolve request.response
16
+ end
17
+ request.on :error do |event|
18
+ promise.reject Native(event)
19
+ end
20
+ request.send
21
+
22
+ promise
23
+ end
24
+
25
+ def upload(url, data, content_type: 'application/json')
26
+ promise = Promise.new
27
+ request = Request.new(:post, url)
28
+
29
+ request.on :success do
30
+ promise.resolve request.response
31
+ end
32
+ request.on :error do |event|
33
+ promise.reject Native(event)
34
+ end
35
+ request.send(data: data, headers: { 'Content-Type' => content_type })
36
+
37
+ promise
38
+ end
39
+ end
40
+ end
data/opal/bowser.rb ADDED
@@ -0,0 +1,194 @@
1
+ module Bowser
2
+ module_function
3
+
4
+ module EventTarget
5
+ def on event_name, &block
6
+ wrapper = proc { |event| block.call Event.new(event) }
7
+
8
+ if `#@native.addEventListener !== undefined`
9
+ `#@native.addEventListener(event_name, wrapper)`
10
+ elsif `#@native.addListener !== undefined`
11
+ `#@native.addListener(event_name, wrapper)`
12
+ else
13
+ warn "[Bowser] Not entirely sure how to add an event listener to #{self}"
14
+ end
15
+
16
+ wrapper
17
+ end
18
+
19
+ def off event_name, &block
20
+ if `#@native.removeEventListener !== undefined`
21
+ `#@native.removeEventListener(event_name, block)`
22
+ elsif `#@native.removeListener !== undefined`
23
+ `#@native.removeListener(event_name, block)`
24
+ else
25
+ warn "[Bowser] Not entirely sure how to remove an event listener from #{self}"
26
+ end
27
+
28
+ nil
29
+ end
30
+ end
31
+
32
+ module Window
33
+ extend EventTarget
34
+
35
+ @native = `window`
36
+
37
+ module_function
38
+
39
+ if `#@native.requestAnimationFrame !== undefined`
40
+ def animation_frame &block
41
+ `requestAnimationFrame(block)`
42
+ self
43
+ end
44
+ else
45
+ def animation_frame &block
46
+ delay(0.16, &block)
47
+ self
48
+ end
49
+ end
50
+
51
+ def delay duration, &block
52
+ `setTimeout(block, duration * 1000)`
53
+ self
54
+ end
55
+
56
+ def interval duration, &block
57
+ `setInterval(block, duration * 1000)`
58
+ self
59
+ end
60
+
61
+ def location
62
+ Location
63
+ end
64
+
65
+ module Location
66
+ module_function
67
+
68
+ def path
69
+ `window.location.pathname`
70
+ end
71
+ end
72
+ end
73
+ def window
74
+ Window
75
+ end
76
+
77
+ module Document
78
+ extend EventTarget
79
+
80
+ @native = `document`
81
+
82
+ module_function
83
+
84
+ def body
85
+ @body ||= Element.new(`#@native.body`)
86
+ end
87
+
88
+ def [] css
89
+ native = `#@native.querySelector(css)`
90
+ if `#{native} === undefined`
91
+ nil
92
+ else
93
+ Element.new(native)
94
+ end
95
+ end
96
+
97
+ def create_element type
98
+ Element.new(`document.createElement(type)`)
99
+ end
100
+ end
101
+ def document
102
+ Document
103
+ end
104
+
105
+ class Element
106
+ include EventTarget
107
+
108
+ def initialize native
109
+ @native = native
110
+ end
111
+
112
+ def inner_dom= node
113
+ clear
114
+ append node
115
+ end
116
+
117
+ def inner_html
118
+ `#@native.innerHTML`
119
+ end
120
+
121
+ def clear
122
+ %x{
123
+ var native = #@native;
124
+
125
+ if(native.nodeName === 'INPUT' || native.nodeName === 'TEXTAREA') {
126
+ native.value = null;
127
+ } else {
128
+ var children = native.children;
129
+ for(var i = 0; i < children.length; i++) {
130
+ children[i].remove();
131
+ }
132
+ }
133
+ }
134
+ self
135
+ end
136
+
137
+ def append node
138
+ `#@native.appendChild(node)`
139
+ self
140
+ end
141
+
142
+ # Form input methods
143
+ def checked?
144
+ `!!#@native.checked`
145
+ end
146
+
147
+ def value
148
+ `#@native.value`
149
+ end
150
+ end
151
+
152
+ class Event
153
+ def initialize native
154
+ @native = native
155
+ end
156
+
157
+ def prevent
158
+ `#@native.preventDefault()`
159
+ self
160
+ end
161
+
162
+ def prevented?
163
+ `#@native.defaultPrevented`
164
+ end
165
+
166
+ def meta?
167
+ `#@native.metaKey`
168
+ end
169
+
170
+ def shift?
171
+ `#@native.shiftKey`
172
+ end
173
+
174
+ def ctrl?
175
+ `#@native.ctrlKey`
176
+ end
177
+
178
+ def alt?
179
+ `#@native.altKey`
180
+ end
181
+
182
+ def button
183
+ `#@native.button`
184
+ end
185
+
186
+ def target
187
+ Element.new(`#@native.currentTarget`)
188
+ end
189
+
190
+ def code
191
+ `#@native.keyCode`
192
+ end
193
+ end
194
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bowser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jamie Gaskins
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: opal
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: opal-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.5.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.5.0
69
+ description:
70
+ email:
71
+ - jgaskins@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.md
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - bowser.gemspec
87
+ - lib/bowser.rb
88
+ - lib/bowser/version.rb
89
+ - opal/bowser.rb
90
+ - opal/bowser/http.rb
91
+ - opal/bowser/http/event.rb
92
+ - opal/bowser/http/request.rb
93
+ - opal/bowser/http/response.rb
94
+ homepage: https://github.com/clearwater-rb/bowser
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.4.5.1
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Minimalist browser support for Opal apps
118
+ test_files: []