rest-firebase 0.9.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: e5623f3812406296167b73e1b26510a060043d0b
4
+ data.tar.gz: 45ddc73ba69941f7d74cdb183b8af4d6784f59f0
5
+ SHA512:
6
+ metadata.gz: ffac8d5aabfb1468a32a7410a495ca0cb4dc0f8b9055c8b498dc5b209d2bc2e5a33027b45b65a7bd6c3f400fc9c9086036d1f727cbf06dc4b458f53e13d7c8e3
7
+ data.tar.gz: 11e4cf1a5b78c289e8ce5af68a47c90f19877c7fc9b04a566d28fe87e78e0a26068fcf54b38c35d3c128ad9c940d72ae824805fa1ad845afd148adc2b98407a3
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ /pkg/
2
+ Gemfile.lock
data/.gitmodules ADDED
@@ -0,0 +1,6 @@
1
+ [submodule "rest-core"]
2
+ path = rest-core
3
+ url = git://github.com/godfat/rest-core.git
4
+ [submodule "task"]
5
+ path = task
6
+ url = git://github.com/godfat/gemgem.git
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ before_install: 'git submodule update --init'
2
+ script: 'ruby -r bundler/setup -S rake test'
3
+
4
+ rvm:
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - ruby
8
+ - rbx-2
9
+ - jruby
data/CHANGES.md ADDED
@@ -0,0 +1,5 @@
1
+ # CHANGES
2
+
3
+ ## rest-firebase 0.9.0 -- 2014-05-13
4
+
5
+ * Birthday!
data/Gemfile ADDED
@@ -0,0 +1,38 @@
1
+
2
+ source 'https://rubygems.org/'
3
+
4
+ gemspec
5
+
6
+ # this is for travis-ci
7
+ gem 'rest-core', :path => 'rest-core' if
8
+ File.exist?("#{File.dirname(File.expand_path(__FILE__))}/rest-core/Gemfile")
9
+
10
+ gem 'rake'
11
+ gem 'bacon'
12
+ gem 'muack'
13
+ gem 'webmock'
14
+
15
+ gem 'json'
16
+ gem 'json_pure'
17
+ gem 'multi_json'
18
+
19
+ gem 'rack'
20
+
21
+ platforms :ruby do
22
+ gem 'yajl-ruby'
23
+ end
24
+
25
+ platforms :rbx do
26
+ gem 'rubysl-weakref' # used in rest-core
27
+ gem 'rubysl-singleton' # used in rake
28
+ gem 'rubysl-rexml' # used in crack used in webmock
29
+ gem 'rubysl-bigdecimal' # used in crack used in webmock
30
+ gem 'rubysl-test-unit' # used in activesupport
31
+ gem 'rubysl-enumerator' # used in activesupport
32
+ gem 'rubysl-benchmark' # used in activesupport
33
+ gem 'racc' # used in journey used in actionpack
34
+ end
35
+
36
+ platforms :jruby do
37
+ gem 'jruby-openssl'
38
+ end
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,211 @@
1
+ # rest-firebase [![Build Status](https://secure.travis-ci.org/CodementorIO/rest-firebase.png?branch=master)](http://travis-ci.org/CodementorIO/rest-firebase)
2
+
3
+ by [Codementor][]
4
+
5
+ [Codementor]: https://www.codementor.io/
6
+
7
+ ## LINKS:
8
+
9
+ * [github](https://github.com/CodementorIO/rest-firebase)
10
+ * [rubygems](https://rubygems.org/gems/rest-firebase)
11
+ * [rdoc](http://rdoc.info/projects/CodementorIO/rest-firebase)
12
+
13
+ ## DESCRIPTION:
14
+
15
+ Ruby Firebase REST API client built on top of [rest-core][].
16
+
17
+ [rest-core]: https://github.com/godfat/rest-core
18
+
19
+ ## FEATURES:
20
+
21
+ * Concurrent requests
22
+ * Streaming requests
23
+
24
+ ## REQUIREMENTS:
25
+
26
+ ### Mandatory:
27
+
28
+ * Tested with MRI (official CRuby), Rubinius and JRuby.
29
+ * gem [rest-core][]
30
+ * gem [httpclient][]
31
+ * gem [mime-types][]
32
+ * gem [timers][]
33
+
34
+ [httpclient]: https://github.com/nahi/httpclient
35
+ [mime-types]: https://github.com/halostatue/mime-types
36
+ [timers]: https://github.com/celluloid/timers
37
+
38
+ ### Optional:
39
+
40
+ * gem json or yajl-ruby, or multi_json
41
+
42
+ ## INSTALLATION:
43
+
44
+ ``` shell
45
+ gem install rest-firebase
46
+ ```
47
+
48
+ Or if you want development version, put this in Gemfile:
49
+
50
+ ``` ruby
51
+ gem 'rest-firebase', :git => 'git://github.com/CodementorIO/rest-firebase.git',
52
+ :submodules => true
53
+ ```
54
+
55
+ ## SYNOPSIS:
56
+
57
+ Check out Firebase's
58
+ [REST API documentation](https://www.firebase.com/docs/rest-api.html)
59
+ for a complete reference.
60
+
61
+ ``` ruby
62
+ require 'rest-firebase'
63
+
64
+ f = RestFirebase.new :site => 'https://SampleChat.firebaseIO-demo.com/',
65
+ :secret => 'secret',
66
+ :d => {:auth_data => 'something'},
67
+ :log_method => method(:puts),
68
+ :auth => false # Ignore auth for this example!
69
+
70
+ @reconnect = true
71
+
72
+ # Streaming over 'users/tom'
73
+ es = f.event_source('users/tom')
74
+ es.onopen { |sock| p sock } # Called when connected
75
+ es.onmessage{ |event, data, sock| p event, data } # Called for each message
76
+ es.onerror { |error, sock| p error } # Called whenever there's an error
77
+ # Extra: If we return true in onreconnect callback, it would automatically
78
+ # reconnect the node for us if disconnected.
79
+ es.onreconnect{ |error, sock| p error; @reconnect }
80
+
81
+ # Start making the request
82
+ es.start
83
+
84
+ # Try to close the connection and see it reconnects automatically
85
+ es.close
86
+
87
+ # Update users/tom.json
88
+ p f.put('users/tom', :some => 'data')
89
+ p f.post('users/tom', :some => 'other')
90
+ p f.get('users/tom')
91
+ p f.delete('users/tom')
92
+
93
+ # Need to tell onreconnect stops reconnecting, or even if we close
94
+ # the connection manually, it would still try to reconnect again.
95
+ @reconnect = false
96
+
97
+ # Close the connection to gracefully shut it down.
98
+ es.close
99
+
100
+ # Refresh the auth by resetting it
101
+ f.auth = nil
102
+ ```
103
+
104
+ ## Concurrent HTTP Requests:
105
+
106
+ Inherited from [rest-core][], you can do concurrent requests quite easily.
107
+ Here's a very quick example of making two API calls at the same time.
108
+
109
+ ``` ruby
110
+ require 'rest-firebase'
111
+ firebase = RestFirebase.new(:log_method => method(:puts))
112
+ puts "httpclient with threads doing concurrent requests"
113
+ a = [firebase.get('users/tom'), firebase.get('users/mom')]
114
+ puts "It's not blocking... but doing concurrent requests underneath"
115
+ p a.map{ |r| r['name'] } # here we want the values, so it blocks here
116
+ puts "DONE"
117
+ ```
118
+
119
+ If you prefer callback based solution, this would also work:
120
+
121
+ ``` ruby
122
+ require 'rest-firebase'
123
+ firebase = RestFirebase.new(:log_method => method(:puts))
124
+ puts "callback also works"
125
+ firebase.get('users/tom') do |r|
126
+ p r['name']
127
+ end
128
+ puts "It's not blocking... but doing concurrent requests underneath"
129
+ firebase.wait # we block here to wait for the request done
130
+ puts "DONE"
131
+ ```
132
+
133
+ For a detailed explanation, see:
134
+ [Advanced Concurrent HTTP Requests -- Embrace the Future][future]
135
+
136
+ [future]: https://github.com/godfat/rest-core#advanced-concurrent-http-requests----embrace-the-future
137
+
138
+ ### Thread Pool / Connection Pool
139
+
140
+ Underneath, rest-core would spawn a thread for each request, freeing you
141
+ from blocking. However, occasionally we would not want this behaviour,
142
+ giving that we might have limited resource and cannot maximize performance.
143
+
144
+ For example, maybe we could not afford so many threads running concurrently,
145
+ or the target server cannot accept so many concurrent connections. In those
146
+ cases, we would want to have limited concurrent threads or connections.
147
+
148
+ ``` ruby
149
+ RestFirebase.pool_size = 10
150
+ RestFirebase.pool_idle_time = 60
151
+ ```
152
+
153
+ This could set the thread pool size to 10, having a maximum of 10 threads
154
+ running together, growing from requests. Each threads idled more than 60
155
+ seconds would be shut down automatically.
156
+
157
+ Note that `pool_size` should at least be larger than 4, or it might be
158
+ very likely to have _deadlock_ if you're using nested callbacks and having
159
+ a large number of concurrent calls.
160
+
161
+ Also, setting `pool_size` to `-1` would mean we want to make blocking
162
+ requests, without spawning any threads. This might be useful for debugging.
163
+
164
+ ### Gracefully shutdown
165
+
166
+ To shutdown gracefully, consider shutdown the thread pool (if we're using it),
167
+ and wait for all requests for a given client. For example:
168
+
169
+ ``` ruby
170
+ RestFirebase.shutdown
171
+ ```
172
+
173
+ We could put them in `at_exit` callback like this:
174
+
175
+ ``` ruby
176
+ at_exit do
177
+ RestFirebase.shutdown
178
+ end
179
+ ```
180
+
181
+ If you're using unicorn, you probably want to put that in the config.
182
+
183
+ ## Powered sites:
184
+
185
+ * [Codementor][]
186
+
187
+ ## CHANGES:
188
+
189
+ * [CHANGES](CHANGES.md)
190
+
191
+ ## CONTRIBUTORS:
192
+
193
+ * Lin Jen-Shin (@godfat)
194
+
195
+ ## LICENSE:
196
+
197
+ Apache License 2.0
198
+
199
+ Copyright (c) 2014, Codementor
200
+
201
+ Licensed under the Apache License, Version 2.0 (the "License");
202
+ you may not use this file except in compliance with the License.
203
+ You may obtain a copy of the License at
204
+
205
+ <http://www.apache.org/licenses/LICENSE-2.0>
206
+
207
+ Unless required by applicable law or agreed to in writing, software
208
+ distributed under the License is distributed on an "AS IS" BASIS,
209
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
210
+ See the License for the specific language governing permissions and
211
+ limitations under the License.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+
2
+ begin
3
+ require "#{dir = File.dirname(__FILE__)}/task/gemgem"
4
+ rescue LoadError
5
+ sh 'git submodule update --init'
6
+ exec Gem.ruby, '-S', $PROGRAM_NAME, *ARGV
7
+ end
8
+
9
+ $LOAD_PATH.unshift(File.expand_path("#{dir}/rest-core/lib"))
10
+
11
+ Gemgem.init(dir) do |s|
12
+ s.name = 'rest-firebase'
13
+ s.version = '0.9.0'
14
+ s.homepage = 'https://github.com/CodementorIO/rest-firebase'
15
+
16
+ s.authors = ['Codementor', 'Lin Jen-Shin (godfat)']
17
+ s.email = ['help@codementor.io']
18
+
19
+ %w[rest-core].each{ |g| s.add_runtime_dependency(g, '>=3.1.1') }
20
+
21
+ # exclude rest-core
22
+ s.files.reject!{ |f| f.start_with?('rest-core/') }
23
+ end
data/TODO.md ADDED
@@ -0,0 +1,3 @@
1
+ # TODO
2
+
3
+ * auto-refresh the auth
@@ -0,0 +1,103 @@
1
+
2
+ require 'rest-core'
3
+
4
+ # https://www.firebase.com/docs/security/custom-login.html
5
+ # https://www.firebase.com/docs/rest-api.html
6
+ RestFirebase = RC::Builder.client(:d, :secret, :auth) do
7
+ use RC::Timeout , 10
8
+
9
+ use RC::DefaultSite , 'https://SampleChat.firebaseIO-demo.com/'
10
+ use RC::DefaultHeaders, {'Accept' => 'application/json'}
11
+ use RC::DefaultQuery , nil
12
+
13
+ use RC::FollowRedirect, 1
14
+ use RC::CommonLogger , nil
15
+ use RC::Cache , nil, 600 do
16
+ use RC::ErrorHandler, lambda{ |env| RestFirebase::Error.call(env) }
17
+ use RC::ErrorDetectorHttp
18
+ use RC::JsonResponse, true
19
+ end
20
+ end
21
+
22
+ class RestFirebase::Error < RestCore::Error
23
+ include RestCore
24
+ class ServerError < RestFirebase::Error; end
25
+ class ClientError < RestCore::Error; end
26
+
27
+ class BadRequest < RestFirebase::Error; end
28
+ class Unauthorized < RestFirebase::Error; end
29
+ class Forbidden < RestFirebase::Error; end
30
+ class NotFound < RestFirebase::Error; end
31
+ class NotAcceptable < RestFirebase::Error; end
32
+ class ExpectationFailed < RestFirebase::Error; end
33
+
34
+ class InternalServerError < RestFirebase::Error::ServerError; end
35
+ class BadGateway < RestFirebase::Error::ServerError; end
36
+ class ServiceUnavailable < RestFirebase::Error::ServerError; end
37
+
38
+ attr_reader :error, :code, :url
39
+ def initialize error, code, url=''
40
+ @error, @code, @url = error, code, url
41
+ super("[#{code}] #{error.inspect} from #{url}")
42
+ end
43
+
44
+ def self.call env
45
+ error, code, url = env[RESPONSE_BODY], env[RESPONSE_STATUS],
46
+ env[REQUEST_URI]
47
+ return new(error, code, url) unless error.kind_of?(Hash)
48
+ case code
49
+ when 400; BadRequest
50
+ when 401; Unauthorized
51
+ when 403; Forbidden
52
+ when 404; NotFound
53
+ when 406; NotAcceptable
54
+ when 417; ExpectationFailed
55
+ when 500; InternalServerError
56
+ when 502; BadGateway
57
+ when 503; ServiceUnavailable
58
+ else ; self
59
+ end.new(error, code, url)
60
+ end
61
+ end
62
+
63
+ module RestFirebase::Client
64
+ include RestCore
65
+
66
+ class EventSource < RestCore::EventSource
67
+ def onmessage event=nil, data=nil, sock=nil
68
+ if event
69
+ super(event, Json.decode(data), sock)
70
+ else
71
+ super
72
+ end
73
+ end
74
+ end
75
+
76
+ def request env, app=app
77
+ super(env.merge(REQUEST_PATH => "#{env[REQUEST_PATH]}.json",
78
+ REQUEST_PAYLOAD => Json.encode(env[REQUEST_PAYLOAD])),
79
+ app)
80
+ end
81
+
82
+ def generate_auth opts={}
83
+ raise RestFirebase::Error::ClientError.new(
84
+ "Please set your secret") unless secret
85
+
86
+ header = {:typ => 'JWT', :alg => 'HS256'}
87
+ claims = {:v => 0, :iat => Time.now.to_i, :d => d}.merge(opts)
88
+ # http://tools.ietf.org/html/draft-ietf-jose-json-web-signature-26
89
+ input = [header, claims].map{ |d| base64url(Json.encode(d)) }.join('.')
90
+ # http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-20
91
+ "#{input}.#{base64url(Hmac.sha256(secret, input))}"
92
+ end
93
+
94
+ private
95
+ def base64url str; [str].pack('m').tr('+/', '-_'); end
96
+ def default_query; {:auth => auth}; end
97
+ def default_auth ; generate_auth ; end
98
+ end
99
+
100
+ class RestFirebase
101
+ include RestFirebase::Client
102
+ self.event_source_class = EventSource
103
+ end
@@ -0,0 +1,48 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # stub: rest-firebase 0.9.0 ruby lib
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "rest-firebase"
6
+ s.version = "0.9.0"
7
+
8
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
+ s.require_paths = ["lib"]
10
+ s.authors = [
11
+ "Codementor",
12
+ "Lin Jen-Shin (godfat)"]
13
+ s.date = "2014-05-13"
14
+ s.description = "Ruby Firebase REST API client built on top of [rest-core][].\n\n[rest-core]: https://github.com/godfat/rest-core"
15
+ s.email = ["help@codementor.io"]
16
+ s.files = [
17
+ ".gitignore",
18
+ ".gitmodules",
19
+ ".travis.yml",
20
+ "CHANGES.md",
21
+ "Gemfile",
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "TODO.md",
26
+ "lib/rest-firebase.rb",
27
+ "rest-firebase.gemspec",
28
+ "task/README.md",
29
+ "task/gemgem.rb",
30
+ "test/test_api.rb"]
31
+ s.homepage = "https://github.com/CodementorIO/rest-firebase"
32
+ s.licenses = ["Apache License 2.0"]
33
+ s.rubygems_version = "2.2.2"
34
+ s.summary = "Ruby Firebase REST API client built on top of [rest-core][]."
35
+ s.test_files = ["test/test_api.rb"]
36
+
37
+ if s.respond_to? :specification_version then
38
+ s.specification_version = 4
39
+
40
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
41
+ s.add_runtime_dependency(%q<rest-core>, [">= 3.1.1"])
42
+ else
43
+ s.add_dependency(%q<rest-core>, [">= 3.1.1"])
44
+ end
45
+ else
46
+ s.add_dependency(%q<rest-core>, [">= 3.1.1"])
47
+ end
48
+ end
data/task/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # Gemgem
2
+
3
+ ## DESCRIPTION:
4
+
5
+ Provided tasks:
6
+
7
+ rake clean # Remove ignored files
8
+ rake gem:build # Build gem
9
+ rake gem:install # Install gem
10
+ rake gem:release # Release gem
11
+ rake gem:spec # Generate gemspec
12
+ rake test # Run tests in memory
13
+
14
+ ## REQUIREMENTS:
15
+
16
+ * Tested with MRI (official CRuby) 1.9.3, 2.0.0, Rubinius and JRuby.
17
+
18
+ ## INSTALLATION:
19
+
20
+ git submodule add git://github.com/godfat/gemgem.git task
21
+
22
+ And in Rakefile:
23
+
24
+ ``` ruby
25
+ begin
26
+ require "#{dir = File.dirname(__FILE__)}/task/gemgem"
27
+ rescue LoadError
28
+ sh 'git submodule update --init'
29
+ exec Gem.ruby, '-S', $PROGRAM_NAME, *ARGV
30
+ end
31
+
32
+ Gemgem.init(dir) do |s|
33
+ s.name = 'your-gem'
34
+ s.version = '0.1.0'
35
+ end
36
+ ```
37
+
38
+ ## LICENSE:
39
+
40
+ Apache License 2.0
41
+
42
+ Copyright (c) 2011-2013, Lin Jen-Shin (godfat)
43
+
44
+ Licensed under the Apache License, Version 2.0 (the "License");
45
+ you may not use this file except in compliance with the License.
46
+ You may obtain a copy of the License at
47
+
48
+ <http://www.apache.org/licenses/LICENSE-2.0>
49
+
50
+ Unless required by applicable law or agreed to in writing, software
51
+ distributed under the License is distributed on an "AS IS" BASIS,
52
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
53
+ See the License for the specific language governing permissions and
54
+ limitations under the License.
data/task/gemgem.rb ADDED
@@ -0,0 +1,262 @@
1
+
2
+ module Gemgem
3
+ class << self
4
+ attr_accessor :dir, :spec, :spec_create
5
+ end
6
+
7
+ module_function
8
+ def gem_tag ; "#{spec.name}-#{spec.version}" ; end
9
+ def gem_path ; "#{pkg_dir}/#{gem_tag}.gem" ; end
10
+ def spec_path ; "#{dir}/#{spec.name}.gemspec" ; end
11
+ def pkg_dir ; "#{dir}/pkg" ; end
12
+ def escaped_dir; @escaped_dir ||= Regexp.escape(dir); end
13
+
14
+ def init dir, &block
15
+ self.dir = dir
16
+ $LOAD_PATH.unshift("#{dir}/lib")
17
+ ENV['RUBYLIB'] = "#{dir}/lib:#{ENV['RUBYLIB']}"
18
+ ENV['PATH'] = "#{dir}/bin:#{ENV['PATH']}"
19
+ self.spec_create = block
20
+ end
21
+
22
+ def create
23
+ spec = Gem::Specification.new do |s|
24
+ s.authors = ['Lin Jen-Shin (godfat)']
25
+ s.email = ['godfat (XD) godfat.org']
26
+
27
+ s.description = description.join
28
+ s.summary = description.first
29
+ s.license = readme['LICENSE'].sub(/.+\n\n/, '').lines.first.strip
30
+
31
+ s.date = Time.now.strftime('%Y-%m-%d')
32
+ s.files = gem_files
33
+ s.test_files = test_files
34
+ s.executables = bin_files
35
+ end
36
+ spec_create.call(spec)
37
+ spec.homepage ||= "https://github.com/godfat/#{spec.name}"
38
+ self.spec = spec
39
+ end
40
+
41
+ def write
42
+ File.open(spec_path, 'w'){ |f| f << split_lines(spec.to_ruby) }
43
+ end
44
+
45
+ def split_lines ruby
46
+ ruby.gsub(/(.+?)\s*=\s*\[(.+?)\]/){ |s|
47
+ if $2.index(',')
48
+ "#{$1} = [\n #{$2.split(',').map(&:strip).join(",\n ")}]"
49
+ else
50
+ s
51
+ end
52
+ }
53
+ end
54
+
55
+ def strip_path path
56
+ strip_home_path(strip_cwd_path(path))
57
+ end
58
+
59
+ def strip_home_path path
60
+ path.sub(ENV['HOME'], '~')
61
+ end
62
+
63
+ def strip_cwd_path path
64
+ path.sub(Dir.pwd, '.')
65
+ end
66
+
67
+ def git *args
68
+ `git --git-dir=#{dir}/.git #{args.join(' ')}`
69
+ end
70
+
71
+ def sh_git *args
72
+ Rake.sh('git', "--git-dir=#{dir}/.git", *args)
73
+ end
74
+
75
+ def sh_gem *args
76
+ Rake.sh(Gem.ruby, '-S', 'gem', *args)
77
+ end
78
+
79
+ def glob path=dir
80
+ Dir.glob("#{path}/**/*", File::FNM_DOTMATCH)
81
+ end
82
+
83
+ def readme
84
+ @readme ||=
85
+ if (path = "#{Gemgem.dir}/README.md") && File.exist?(path)
86
+ ps = "##{File.read(path)}".
87
+ scan(/((#+)[^\n]+\n\n.+?(?=(\n\n\2[^#\n]+\n)|\Z))/m).map(&:first)
88
+ ps.inject('HEADER' => ps.first){ |r, s, i|
89
+ r[s[/\w+/]] = s
90
+ r
91
+ }
92
+ else
93
+ {}
94
+ end
95
+ end
96
+
97
+ def description
98
+ # JRuby String#lines is returning an enumerator
99
+ @description ||= (readme['DESCRIPTION']||'').sub(/.+\n\n/, '').lines.to_a
100
+ end
101
+
102
+ def all_files
103
+ @all_files ||= fold_files(glob).sort
104
+ end
105
+
106
+ def fold_files files
107
+ files.inject([]){ |r, path|
108
+ if File.file?(path) && path !~ %r{/\.git(/|$)} &&
109
+ (rpath = path[%r{^#{escaped_dir}/(.*$)}, 1])
110
+ r << rpath
111
+ elsif File.symlink?(path) # walk into symlinks...
112
+ r.concat(fold_files(glob(File.expand_path(path,
113
+ File.readlink(path)))))
114
+ else
115
+ r
116
+ end
117
+ }
118
+ end
119
+
120
+ def gem_files
121
+ @gem_files ||= all_files.reject{ |f|
122
+ f =~ ignored_pattern && !git_files.include?(f)
123
+ }
124
+ end
125
+
126
+ def test_files
127
+ @test_files ||= gem_files.grep(%r{^test/(.+?/)*test_.+?\.rb$})
128
+ end
129
+
130
+ def bin_files
131
+ @bin_files ||= gem_files.grep(%r{^bin/}).map{ |f| File.basename(f) }
132
+ end
133
+
134
+ def git_files
135
+ @git_files ||= if File.exist?("#{dir}/.git")
136
+ git('ls-files').split("\n")
137
+ else
138
+ []
139
+ end
140
+ end
141
+
142
+ def ignored_files
143
+ @ignored_files ||= all_files.grep(ignored_pattern)
144
+ end
145
+
146
+ def ignored_pattern
147
+ @ignored_pattern ||= if gitignore.empty?
148
+ /^$/
149
+ else
150
+ Regexp.new(expand_patterns(gitignore).join('|'))
151
+ end
152
+ end
153
+
154
+ def expand_patterns pathes
155
+ # http://git-scm.com/docs/gitignore
156
+ pathes.flat_map{ |path|
157
+ # we didn't implement negative pattern for now
158
+ Regexp.escape(path).sub(%r{^/}, '^').gsub(/\\\*/, '[^/]*')
159
+ }
160
+ end
161
+
162
+ def gitignore
163
+ @gitignore ||= if File.exist?(path = "#{dir}/.gitignore")
164
+ File.read(path).lines.
165
+ reject{ |l| l == /^\s*(#|\s+$)/ }.map(&:strip)
166
+ else
167
+ []
168
+ end
169
+ end
170
+ end
171
+
172
+ namespace :gem do
173
+
174
+ desc 'Install gem'
175
+ task :install => [:build] do
176
+ Gemgem.sh_gem('install', Gemgem.gem_path)
177
+ end
178
+
179
+ desc 'Build gem'
180
+ task :build => [:spec] do
181
+ require 'fileutils'
182
+ require 'rubygems/package'
183
+ gem = nil
184
+ Dir.chdir(Gemgem.dir) do
185
+ gem = Gem::Package.build(Gem::Specification.load(Gemgem.spec_path))
186
+ FileUtils.mkdir_p(Gemgem.pkg_dir)
187
+ FileUtils.mv(gem, Gemgem.pkg_dir) # gem is relative path, but might be ok
188
+ end
189
+ puts "\e[35mGem built: \e[33m" \
190
+ "#{Gemgem.strip_path("#{Gemgem.pkg_dir}/#{gem}")}\e[0m"
191
+ end
192
+
193
+ desc 'Generate gemspec'
194
+ task :spec do
195
+ Gemgem.create
196
+ Gemgem.write
197
+ end
198
+
199
+ desc 'Release gem'
200
+ task :release => [:spec, :check, :build] do
201
+ Gemgem.module_eval do
202
+ sh_git('tag', Gemgem.gem_tag)
203
+ sh_git('push')
204
+ sh_git('push', '--tags')
205
+ sh_gem('push', Gemgem.gem_path)
206
+ end
207
+ end
208
+
209
+ task :check do
210
+ ver = Gemgem.spec.version.to_s
211
+
212
+ if ENV['VERSION'].nil?
213
+ puts("\e[35mExpected " \
214
+ "\e[33mVERSION\e[35m=\e[33m#{ver}\e[0m")
215
+ exit(1)
216
+
217
+ elsif ENV['VERSION'] != ver
218
+ puts("\e[35mExpected \e[33mVERSION\e[35m=\e[33m#{ver} " \
219
+ "\e[35mbut got\n " \
220
+ "\e[33mVERSION\e[35m=\e[33m#{ENV['VERSION']}\e[0m")
221
+ exit(2)
222
+ end
223
+ end
224
+
225
+ end # of gem namespace
226
+
227
+ desc 'Run tests'
228
+ task :test do
229
+ next if Gemgem.test_files.empty?
230
+
231
+ require 'bacon'
232
+ Bacon.extend(Bacon::TestUnitOutput)
233
+ Bacon.summary_on_exit
234
+ Gemgem.test_files.each{ |file| require "#{Gemgem.dir}/#{file[0..-4]}" }
235
+ end
236
+
237
+ desc 'Trash ignored files'
238
+ task :clean => ['gem:spec'] do
239
+ next if Gemgem.ignored_files.empty?
240
+
241
+ require 'fileutils'
242
+ trash = File.expand_path("~/.Trash/#{Gemgem.spec.name}")
243
+ puts "Move the following files into:" \
244
+ " \e[35m#{Gemgem.strip_path(trash)}\e[33m"
245
+
246
+ Gemgem.ignored_files.each do |file|
247
+ from = "#{Gemgem.dir}/#{file}"
248
+ to = "#{trash}/#{File.dirname(file)}"
249
+ puts Gemgem.strip_path(from)
250
+
251
+ FileUtils.mkdir_p(to)
252
+ FileUtils.mv(from, to)
253
+ end
254
+
255
+ print "\e[0m"
256
+ end
257
+
258
+ task :default do
259
+ # Is there a reliable way to do this in the current process?
260
+ # It failed miserably before between Rake versions...
261
+ exec "#{Gem.ruby} -S #{$PROGRAM_NAME} -f #{Rake.application.rakefile} -T"
262
+ end
data/test/test_api.rb ADDED
@@ -0,0 +1,73 @@
1
+
2
+ require 'rest-firebase'
3
+ require 'rest-core/test'
4
+
5
+ describe RestFirebase do
6
+ before do
7
+ stub(Time).now{ Time.at(0) }
8
+ end
9
+
10
+ after do
11
+ WebMock.reset!
12
+ Muack.verify
13
+ end
14
+
15
+ path = 'https://a.json?auth=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9%0A.eyJ2IjowLCJpYXQiOjAsImQiOm51bGx9%0A.C9JtzZhiCrsClNdAQcE7Irngr2BZJCH4x1p-IHxfrAo%3D%0A'
16
+
17
+ def firebase
18
+ RestFirebase.new(:secret => 'nnf')
19
+ end
20
+
21
+ should 'get true' do
22
+ stub_request(:get, path).to_return(:body => 'true')
23
+ firebase.get('https://a').should.eq true
24
+ end
25
+
26
+ should 'put {"status":"ok"}' do
27
+ json = '{"status":"ok"}'
28
+ rbon = {'status' => 'ok'}
29
+ stub_request(:put, path).with(:body => json).to_return(:body => json)
30
+ firebase.put('https://a', rbon).should.eq rbon
31
+ end
32
+
33
+ should 'parse event source' do
34
+ stub_request(:get, path).to_return(:body => <<-SSE)
35
+ event: put
36
+ data: {}
37
+
38
+ event: keep-alive
39
+ data: null
40
+
41
+ event: invalid
42
+ data: invalid
43
+ SSE
44
+ m = [{'event' => 'put' , 'data' => {}},
45
+ {'event' => 'keep-alive', 'data' => nil}]
46
+ es = firebase.event_source('https://a')
47
+ es.should.kind_of RestFirebase::Client::EventSource
48
+ es.onmessage do |event, data|
49
+ {'event' => event, 'data' => data}.should.eq m.shift
50
+ end.onerror do |error|
51
+ error.should.kind_of RC::Json::ParseError
52
+ end.start.wait
53
+ m.should.empty
54
+ end
55
+
56
+ check = lambda do |status, klass|
57
+ stub_request(:delete, path).to_return(
58
+ :body => '{}', :status => status)
59
+
60
+ lambda{ firebase.delete('https://a').tap{} }.should.raise(klass)
61
+
62
+ WebMock.reset!
63
+ end
64
+
65
+ should 'raise exception when encountering error' do
66
+ [400, 401, 402, 403, 404, 406, 417].each do |status|
67
+ check[status, RestFirebase::Error]
68
+ end
69
+ [500, 502, 503].each do |status|
70
+ check[status, RestFirebase::Error::ServerError]
71
+ end
72
+ end
73
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rest-firebase
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Codementor
8
+ - Lin Jen-Shin (godfat)
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-05-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-core
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 3.1.1
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 3.1.1
28
+ description: |-
29
+ Ruby Firebase REST API client built on top of [rest-core][].
30
+
31
+ [rest-core]: https://github.com/godfat/rest-core
32
+ email:
33
+ - help@codementor.io
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - ".gitignore"
39
+ - ".gitmodules"
40
+ - ".travis.yml"
41
+ - CHANGES.md
42
+ - Gemfile
43
+ - LICENSE
44
+ - README.md
45
+ - Rakefile
46
+ - TODO.md
47
+ - lib/rest-firebase.rb
48
+ - rest-firebase.gemspec
49
+ - task/README.md
50
+ - task/gemgem.rb
51
+ - test/test_api.rb
52
+ homepage: https://github.com/CodementorIO/rest-firebase
53
+ licenses:
54
+ - Apache License 2.0
55
+ metadata: {}
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 2.2.2
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Ruby Firebase REST API client built on top of [rest-core][].
76
+ test_files:
77
+ - test/test_api.rb