firecord 0.2.6
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 +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.rubocop.yml +33 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +127 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/examples/address.rb +16 -0
- data/firecord.gemspec +33 -0
- data/lib/firecord.rb +15 -0
- data/lib/firecord/configuration.rb +27 -0
- data/lib/firecord/credentials.rb +42 -0
- data/lib/firecord/error.rb +5 -0
- data/lib/firecord/model.rb +64 -0
- data/lib/firecord/patch/as.rb +5 -0
- data/lib/firecord/record.rb +138 -0
- data/lib/firecord/record/deserializer.rb +26 -0
- data/lib/firecord/record/serializer.rb +34 -0
- data/lib/firecord/repository/firebase.rb +72 -0
- data/lib/firecord/repository/response.rb +31 -0
- data/lib/firecord/version.rb +3 -0
- metadata +168 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: aba51eb2c600ac98aa93e4eb53a1cd40e6042377
|
4
|
+
data.tar.gz: 2b5649e36fbf359f32dc8bf7ef195b5e746399ac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fc5ea20745dbe78cc0b508d192a0c67958e2abfab17b87fc403ae5bf80b9ce2678c62a3c0d45c5dbec0a03430956f0abac7cb738aeee9f0b524e347f24f8c072
|
7
|
+
data.tar.gz: e272026f4b9a055acfdcf595968bd79c02e426895fb238337633531560ceb4afdc85da9c0c7681bfb39466078f9c84d05da915a35c2b6f3367ae89c30076f08a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
Style/EmptyLinesAroundClassBody:
|
2
|
+
Enabled: false
|
3
|
+
|
4
|
+
Style/EmptyLinesAroundModuleBody:
|
5
|
+
Enabled: false
|
6
|
+
|
7
|
+
Style/FrozenStringLiteralComment:
|
8
|
+
Enabled: true
|
9
|
+
|
10
|
+
Style/MultilineMethodCallIndentation:
|
11
|
+
EnforcedStyle: indented_relative_to_receiver
|
12
|
+
|
13
|
+
Style/NumericPredicate:
|
14
|
+
EnforcedStyle: comparison
|
15
|
+
|
16
|
+
Style/Lambda:
|
17
|
+
EnforcedStyle: lambda
|
18
|
+
|
19
|
+
Style/BlockDelimiters:
|
20
|
+
EnforcedStyle: semantic
|
21
|
+
|
22
|
+
Style/FrozenStringLiteralComment:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
Metrics/LineLength:
|
26
|
+
Max: 80
|
27
|
+
|
28
|
+
AllCops:
|
29
|
+
TargetRubyVersion: 2.4
|
30
|
+
DisplayCopNames: true
|
31
|
+
|
32
|
+
Documentation:
|
33
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at tomas@stepnivlk.net. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Tomas Koutsky
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
# Firecord
|
2
|
+
|
3
|
+

|
4
|
+
|
5
|
+
[](https://travis-ci.org/stepnivlk/firecord) [](https://ebertapp.io/github/stepnivlk/firecord)
|
6
|
+
|
7
|
+
Firecord is an ODM (Object-Document-Mapper) framework for Firebase in Ruby.
|
8
|
+
|
9
|
+
Connects to the Firebase REST API, (for now) it's totally synchronous and provides similar API to all those classical Ruby ORMs.
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
You need to download ```credentials.json``` for your Firebase project (Firebase console > Project settings > Service accounts > Generate new private key)
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
require 'firecord'
|
17
|
+
|
18
|
+
Firecord.configure do |config|
|
19
|
+
config.credentials_file = '/path/to/credentials.json'
|
20
|
+
end
|
21
|
+
|
22
|
+
class Address
|
23
|
+
include Firecord::Record
|
24
|
+
|
25
|
+
root_key 'addresses'
|
26
|
+
|
27
|
+
field :name, :string
|
28
|
+
field :location, :string
|
29
|
+
field :door_number, :integer
|
30
|
+
field :timestamps
|
31
|
+
end
|
32
|
+
|
33
|
+
address = Address.new(name: 'home', location: 'Prerov', door_number: 1)
|
34
|
+
# => #<Address id=nil name="home" location="Prerov" door_number=1 created_at=nil updated_at=nil>
|
35
|
+
|
36
|
+
address.save
|
37
|
+
# => #<Address id="-KdvwtldpM4yVWJfoQIg" name="home" location="Prerov" door_number=1 created_at="2017-02-26T19:44:32+00:00" updated_at=nil>
|
38
|
+
|
39
|
+
another_address = Address.find("-KdvwtldpM4yVWJfoQIg")
|
40
|
+
# => #<Address id="-KgBbL8yedmT88iLmbOE" name="not_home" location="Semice" door_number=2 created_at="2017-02-26T19:33:12+00:00" updated_at=nil>
|
41
|
+
|
42
|
+
another_address.door_number = 23
|
43
|
+
# => 23
|
44
|
+
|
45
|
+
another_address.save
|
46
|
+
# => #<Address id="-KgBbL8yedmT88iLmbOE" name="not_home" location="Semice" door_number=23 created_at="2017-02-26T19:33:12+00:00" updated_at="2017-02-26T19:47:22+00:00">
|
47
|
+
|
48
|
+
another_address.update(location: 'Prerov nad Labem')
|
49
|
+
# => #<Address id="-KgBbL8yedmT88iLmbOE" name="not_home" location="Prerov nad Labem" door_number=23 created_at="2017-02-26T19:33:12+00:00" updated_at="2017-02-26T19:47:22+00:00">
|
50
|
+
|
51
|
+
Address.where(name: "not_home", door_number: 23)
|
52
|
+
# => [#<Address id="-KgBbL8yedmT88iLmbOE" name="not_home" location="Prerov nad Labem" door_number=23 created_at="2017-02-26T19:33:12+00:00" updated_at="2017-02-26T19:47:22+00:00">]
|
53
|
+
|
54
|
+
Address.all
|
55
|
+
# => [#<Address id="-KdvwtldpM4yVWJfoQIg" name="home" location="Prerov" door_number=1 created_at="2017-02-26T19:44:32+00:00" updated_at=nil>, #<Address id="-KgBbL8yedmT88iLmbOE" name="not_home" location="Prerov nad Labem" door_number=23 created_at="2017-02-26T19:33:12+00:00" updated_at="2017-02-26T19:47:22+00:00">]
|
56
|
+
|
57
|
+
address.delete
|
58
|
+
# => true
|
59
|
+
|
60
|
+
Address.all
|
61
|
+
# => [#<Address id="-KgBbL8yedmT88iLmbOE" name="not_home" location="Prerov nad Labem" door_number=23 created_at="2017-02-26T19:33:12+00:00" updated_at="2017-02-26T19:47:22+00:00">]
|
62
|
+
```
|
63
|
+
|
64
|
+
## Current version: 0.2.6
|
65
|
+
This version adds:
|
66
|
+
* Basic retypying/checking of invalid assignments, eg: ```address.door_number = '23' #=> 23```.
|
67
|
+
* ```.where``` method which for now accepts hash and works only for equality, eg: ```Address.where(door_number: 23)```
|
68
|
+
* Adds proper ```DateTime``` object for ```created_at``` and ```updated_at``` fields
|
69
|
+
* Adds more 'integration' tests for dummy classes.
|
70
|
+
* Introduces ```Serializer``` and ```Deserializer``` classes.
|
71
|
+
|
72
|
+
### Roadmap to version 0.3.0
|
73
|
+
- [x] Working timestamps
|
74
|
+
- [ ] Error handling
|
75
|
+
- [x] `where` method
|
76
|
+
- [ ] Test everything
|
77
|
+
- [x] Better configuration
|
78
|
+
- [x] Documentation
|
79
|
+
- [x] Basic type validation
|
80
|
+
- [ ] Get rid of nil checks in firebase repo
|
81
|
+
- [ ] Add fields: boolean, datetime, time, date, json
|
82
|
+
- [ ] Add ```.delete_all``` method
|
83
|
+
- [ ] Add ```.create``` method
|
84
|
+
|
85
|
+
### Future versions
|
86
|
+
- [ ] Add proper validation
|
87
|
+
- [ ] Robust persistence state abstraction
|
88
|
+
- [ ] Some basic associations
|
89
|
+
- [ ] Define nested documents
|
90
|
+
|
91
|
+
#### Version log
|
92
|
+
##### 0.2.2 codename Spike
|
93
|
+
This release is not meant to be used by anyone in production. I'm trying to lay down the interface, experiment and learn basics of ORM/ODM by coding and producing something (maybe) usefull. I still need to finalize some functionality, do refactoring and add features.
|
94
|
+
|
95
|
+
|
96
|
+
## Installation
|
97
|
+
|
98
|
+
Add this line to your application's Gemfile:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
gem 'firecord'
|
102
|
+
```
|
103
|
+
|
104
|
+
And then execute:
|
105
|
+
|
106
|
+
$ bundle
|
107
|
+
|
108
|
+
Or install it yourself as:
|
109
|
+
|
110
|
+
$ gem install firecord
|
111
|
+
|
112
|
+
|
113
|
+
## Development
|
114
|
+
|
115
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
116
|
+
|
117
|
+
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).
|
118
|
+
|
119
|
+
## Contributing
|
120
|
+
|
121
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/stepnivlk/firecord. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
122
|
+
|
123
|
+
|
124
|
+
## License
|
125
|
+
|
126
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
127
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "firecord"
|
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__)
|
data/bin/setup
ADDED
data/examples/address.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require '../lib/firecord'
|
2
|
+
|
3
|
+
Firecord.configure do |config|
|
4
|
+
config.credentials_file = '../../credentials.json'
|
5
|
+
end
|
6
|
+
|
7
|
+
class Address
|
8
|
+
include Firecord::Record
|
9
|
+
|
10
|
+
root_key 'addresses'
|
11
|
+
|
12
|
+
field :name, :string
|
13
|
+
field :location, :string
|
14
|
+
field :door_number, :integer
|
15
|
+
field :timestamps
|
16
|
+
end
|
data/firecord.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'firecord/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'firecord'
|
8
|
+
spec.version = Firecord::VERSION
|
9
|
+
spec.authors = ['Tomas Koutsky']
|
10
|
+
spec.email = ['tomas@stepnivlk.net']
|
11
|
+
|
12
|
+
spec.summary = 'Firecord is an ODM framework for Firebase in Ruby.'
|
13
|
+
spec.description = 'Firecord is an ODM (Object-Document-Mapper) framework' \
|
14
|
+
' for Firebase in Ruby.'
|
15
|
+
spec.homepage = "http://stepnivlk.net"
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = 'exe'
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
|
25
|
+
spec.add_runtime_dependency 'jwt', '~> 1.5'
|
26
|
+
spec.add_runtime_dependency 'httparty', '~> 0.14'
|
27
|
+
|
28
|
+
spec.add_development_dependency 'bundler', '~> 1.14'
|
29
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
30
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
31
|
+
spec.add_development_dependency 'webmock', '~> 2.3'
|
32
|
+
spec.add_development_dependency 'vcr', '~> 3.0'
|
33
|
+
end
|
data/lib/firecord.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "firecord/version"
|
2
|
+
require 'firecord/configuration'
|
3
|
+
require 'firecord/credentials'
|
4
|
+
require 'firecord/repository/firebase'
|
5
|
+
require 'firecord/repository/response'
|
6
|
+
require 'firecord/record'
|
7
|
+
require 'firecord/record/serializer'
|
8
|
+
require 'firecord/record/deserializer'
|
9
|
+
require 'firecord/model'
|
10
|
+
require 'firecord/error'
|
11
|
+
require 'firecord/patch/as'
|
12
|
+
|
13
|
+
module Firecord
|
14
|
+
extend Configuration
|
15
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Firecord
|
2
|
+
module Configuration
|
3
|
+
CREDENTIALS_FILE = './credentials.json'.freeze
|
4
|
+
|
5
|
+
VALID_ACCESSORS = [:credentials_file].freeze
|
6
|
+
|
7
|
+
attr_accessor(*VALID_ACCESSORS)
|
8
|
+
|
9
|
+
def configure
|
10
|
+
yield self
|
11
|
+
end
|
12
|
+
|
13
|
+
def options
|
14
|
+
VALID_ACCESSORS.inject({}) do |accessor, key|
|
15
|
+
accessor.merge!(key => send(key))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.extended(mod)
|
20
|
+
mod.set_defaults
|
21
|
+
end
|
22
|
+
|
23
|
+
def set_defaults
|
24
|
+
self.credentials_file = CREDENTIALS_FILE
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'jwt'
|
2
|
+
|
3
|
+
module Firecord
|
4
|
+
class Credentials
|
5
|
+
def initialize(credentials_file = nil)
|
6
|
+
@credentials = \
|
7
|
+
JSON.parse(File.read(credentials_file || Firecord.credentials_file))
|
8
|
+
@private_key = OpenSSL::PKey::RSA.new @credentials['private_key']
|
9
|
+
end
|
10
|
+
|
11
|
+
def project_id
|
12
|
+
@credentials['project_id']
|
13
|
+
end
|
14
|
+
|
15
|
+
def generate_jwt_assertion
|
16
|
+
now_seconds = Time.now.to_i
|
17
|
+
payload = {
|
18
|
+
iss: @credentials['client_email'],
|
19
|
+
aud: @credentials['token_uri'],
|
20
|
+
iat: now_seconds,
|
21
|
+
exp: now_seconds + (60 * 60),
|
22
|
+
scope: 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/firebase.database'
|
23
|
+
}
|
24
|
+
JWT.encode payload, @private_key, 'RS256'
|
25
|
+
end
|
26
|
+
|
27
|
+
def generate_access_token
|
28
|
+
uri = URI.parse(@credentials['token_uri'])
|
29
|
+
https = Net::HTTP.new(uri.host, uri.port)
|
30
|
+
https.use_ssl = true
|
31
|
+
req = Net::HTTP::Post.new(uri.path)
|
32
|
+
req['Cache-Control'] = 'no-store'
|
33
|
+
req.set_form_data(
|
34
|
+
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
|
35
|
+
assertion: generate_jwt_assertion
|
36
|
+
)
|
37
|
+
|
38
|
+
resp = JSON.parse(https.request(req).body)
|
39
|
+
resp['access_token']
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module Firecord
|
2
|
+
module Model
|
3
|
+
def self.extended(model)
|
4
|
+
model.instance_variable_set(:@fields, [])
|
5
|
+
model.fields << OpenStruct.new(name: :id, type: :private_key)
|
6
|
+
end
|
7
|
+
|
8
|
+
def field(name, type = nil)
|
9
|
+
return timestamps if name == :timestamps
|
10
|
+
|
11
|
+
@fields << OpenStruct.new(name: name, type: type)
|
12
|
+
end
|
13
|
+
|
14
|
+
def timestamps
|
15
|
+
%i(created_at updated_at).each do |stamp|
|
16
|
+
@fields << OpenStruct.new(name: stamp, type: :datetime)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def all
|
21
|
+
repository
|
22
|
+
.all
|
23
|
+
.reject { |response| response.keys.include?(:error) }
|
24
|
+
.map { |response| new(response).persist }
|
25
|
+
end
|
26
|
+
|
27
|
+
def find(id)
|
28
|
+
response = repository.get(id)
|
29
|
+
response.nil? ? nil : new(response).persist
|
30
|
+
end
|
31
|
+
|
32
|
+
def where(query)
|
33
|
+
validate_query!(query)
|
34
|
+
|
35
|
+
all.select do |record|
|
36
|
+
result = query.map { |name, value| record.send(name) == value }.uniq
|
37
|
+
|
38
|
+
result.size == 1 && result[0] == true ? true : false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def root_key(root_name)
|
43
|
+
@root = root_name
|
44
|
+
end
|
45
|
+
|
46
|
+
def fields
|
47
|
+
@fields
|
48
|
+
end
|
49
|
+
|
50
|
+
def root
|
51
|
+
@root || name.downcase
|
52
|
+
end
|
53
|
+
|
54
|
+
def repository
|
55
|
+
@repository ||= Repository::Firebase.new(root)
|
56
|
+
end
|
57
|
+
|
58
|
+
def validate_query!(query)
|
59
|
+
result = query.keys - fields.map(&:name)
|
60
|
+
raise InvalidQuery, 'Your query contains invalid key(s)' \
|
61
|
+
unless result.empty?
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
module Firecord
|
2
|
+
module Record
|
3
|
+
def self.included(model)
|
4
|
+
model.extend(Model)
|
5
|
+
super
|
6
|
+
end
|
7
|
+
|
8
|
+
alias model class
|
9
|
+
|
10
|
+
def initialize(params = {})
|
11
|
+
fields.each do |field|
|
12
|
+
initialize_accessor(field, params)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def save
|
17
|
+
return _create if new?
|
18
|
+
|
19
|
+
_update
|
20
|
+
end
|
21
|
+
|
22
|
+
def update(attributes = {})
|
23
|
+
attributes.each do |name, value|
|
24
|
+
send("#{name}=", value)
|
25
|
+
end
|
26
|
+
|
27
|
+
tap { |record| repository.patch(record) }
|
28
|
+
end
|
29
|
+
|
30
|
+
def delete
|
31
|
+
repository.delete(id)
|
32
|
+
end
|
33
|
+
|
34
|
+
def inspect
|
35
|
+
attrs = fields.map { |field|
|
36
|
+
value = get_value(field.name)
|
37
|
+
formatted = value.is_a?(String) ? "\"#{value}\"" : value
|
38
|
+
|
39
|
+
"#{field.name}=#{formatted || 'nil'}"
|
40
|
+
}
|
41
|
+
|
42
|
+
"#<#{model.name} #{attrs.join(' ')}>"
|
43
|
+
end
|
44
|
+
|
45
|
+
def new?
|
46
|
+
persistence_state == :transient
|
47
|
+
end
|
48
|
+
|
49
|
+
def persist
|
50
|
+
@persistence_state = :persisted
|
51
|
+
|
52
|
+
self
|
53
|
+
end
|
54
|
+
|
55
|
+
def method_missing(method, *args, &block)
|
56
|
+
return super unless available_names.include?(method)
|
57
|
+
|
58
|
+
return set_value(method, args[0]) if method.to_s.end_with?('=')
|
59
|
+
get_value(method)
|
60
|
+
end
|
61
|
+
|
62
|
+
def respond_to_missing?(method, include_private = false)
|
63
|
+
available_names.include?(method) || super
|
64
|
+
end
|
65
|
+
|
66
|
+
def fields
|
67
|
+
model.fields
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
|
72
|
+
def set_value(name, value, type = nil)
|
73
|
+
type ||= field_for(name).type
|
74
|
+
sanitizer = Serializer.new(value, type)
|
75
|
+
|
76
|
+
send("_#{name}", sanitizer.value)
|
77
|
+
end
|
78
|
+
|
79
|
+
def get_value(name)
|
80
|
+
value = send("_#{name}")
|
81
|
+
|
82
|
+
return value if value.nil?
|
83
|
+
|
84
|
+
Deserializer.new(value, field_for(name).type).value
|
85
|
+
end
|
86
|
+
|
87
|
+
def available_names
|
88
|
+
fields.map(&:name) + fields.map { |field| :"#{field.name}=" }
|
89
|
+
end
|
90
|
+
|
91
|
+
def _create
|
92
|
+
tap do |record|
|
93
|
+
record.created_at = DateTime.now
|
94
|
+
record.id = repository.post(persist)[:name]
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def _update
|
99
|
+
tap do |record|
|
100
|
+
record.updated_at = DateTime.now
|
101
|
+
repository.patch(record)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def field_for(name)
|
106
|
+
sanitized_name = name.to_s.end_with?('=') ? :"#{name[0..-2]}" : name
|
107
|
+
fields.find { |field| field.name == sanitized_name }
|
108
|
+
end
|
109
|
+
|
110
|
+
def repository
|
111
|
+
model.repository
|
112
|
+
end
|
113
|
+
|
114
|
+
def persistence_state
|
115
|
+
@persistence_state ||= :transient
|
116
|
+
end
|
117
|
+
|
118
|
+
def initialize_accessor(field, params)
|
119
|
+
name = field.name
|
120
|
+
|
121
|
+
restrict_accessor(name)
|
122
|
+
|
123
|
+
value = params[name] || nil
|
124
|
+
set_value("#{name}=", value, field.type) if value
|
125
|
+
end
|
126
|
+
|
127
|
+
def restrict_accessor(name)
|
128
|
+
self.class.class_eval do
|
129
|
+
attr_accessor("_#{name}")
|
130
|
+
end
|
131
|
+
|
132
|
+
self.class.instance_eval do
|
133
|
+
private(:"_#{name}")
|
134
|
+
private(:"_#{name}=")
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Firecord
|
2
|
+
module Record
|
3
|
+
class Deserializer
|
4
|
+
def initialize(value, type)
|
5
|
+
@value = value
|
6
|
+
@type = type
|
7
|
+
end
|
8
|
+
|
9
|
+
def value
|
10
|
+
send(@type)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
%w(string integer float private_key).each do |type|
|
16
|
+
define_method(type) do
|
17
|
+
@value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def datetime
|
22
|
+
DateTime.parse(@value)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Firecord
|
2
|
+
module Record
|
3
|
+
class Serializer
|
4
|
+
def initialize(value, type)
|
5
|
+
@value = value
|
6
|
+
@type = type
|
7
|
+
end
|
8
|
+
|
9
|
+
def value
|
10
|
+
@value.nil? ? @value : send(@type)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
%w(string datetime).each do |type|
|
16
|
+
define_method(type) do
|
17
|
+
@value.to_s
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def private_key
|
22
|
+
@value
|
23
|
+
end
|
24
|
+
|
25
|
+
def integer
|
26
|
+
@value.respond_to?(:to_i) ? @value.to_i : nil
|
27
|
+
end
|
28
|
+
|
29
|
+
def float
|
30
|
+
@value.respond_to?(:to_f) ? @value.to_f : nil
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
|
3
|
+
module Firecord
|
4
|
+
module Repository
|
5
|
+
class Firebase
|
6
|
+
include HTTParty
|
7
|
+
|
8
|
+
def initialize(root)
|
9
|
+
@credentials = Credentials.new
|
10
|
+
@root = root
|
11
|
+
self.class.base_uri "https://#{@credentials.project_id}.firebaseio.com/"
|
12
|
+
end
|
13
|
+
|
14
|
+
def all
|
15
|
+
self
|
16
|
+
.class.get("/#{@root}.json", options)
|
17
|
+
.as { |response| Response.new(response).sanitize }
|
18
|
+
.map { |id, data| Response.new(data.merge(id: id)).sanitize }
|
19
|
+
end
|
20
|
+
|
21
|
+
def get(id)
|
22
|
+
self
|
23
|
+
.class.get("/#{@root}/#{id}.json", options)
|
24
|
+
.as { |response| Response.new(response, id: id).sanitize_with_nil }
|
25
|
+
end
|
26
|
+
|
27
|
+
def post(record)
|
28
|
+
self
|
29
|
+
.class.post("/#{@root}.json", payload(record))
|
30
|
+
.as { |response| Response.new(response).sanitize }
|
31
|
+
end
|
32
|
+
|
33
|
+
def patch(record)
|
34
|
+
self
|
35
|
+
.class.patch("/#{@root}/#{record.id}.json", payload(record))
|
36
|
+
.as { |response| Response.new(response).sanitize }
|
37
|
+
end
|
38
|
+
|
39
|
+
def delete(id)
|
40
|
+
self
|
41
|
+
.class.delete("/#{@root}/#{id}.json", options)
|
42
|
+
.as { |response| response ? false : true }
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def payload(record)
|
48
|
+
options.dup.tap do |data|
|
49
|
+
data[:body] = body(record).to_json
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def body(record)
|
54
|
+
record.fields.each_with_object({}) do |field, data|
|
55
|
+
next if field.type == :private_key
|
56
|
+
|
57
|
+
data[field.name] = record.send(field.name)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def options
|
62
|
+
{
|
63
|
+
headers: {
|
64
|
+
'Authorization' => "Bearer #{@credentials.generate_access_token}",
|
65
|
+
'Content-Type' => 'application/json',
|
66
|
+
'User-Agent' => 'X-FIREBASE-CLIENT'
|
67
|
+
}
|
68
|
+
}
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Firecord
|
2
|
+
module Repository
|
3
|
+
class Response
|
4
|
+
def initialize(response, default = {})
|
5
|
+
@response = response
|
6
|
+
@default = default
|
7
|
+
end
|
8
|
+
|
9
|
+
def sanitize
|
10
|
+
@response ? symbolize_keys : invalid_record
|
11
|
+
end
|
12
|
+
|
13
|
+
def sanitize_with_nil
|
14
|
+
@response ? symbolize_keys : nil
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def symbolize_keys
|
20
|
+
@response.each_with_object(@default) do |(key, value), result|
|
21
|
+
result[key.to_sym] = value
|
22
|
+
result
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def invalid_record
|
27
|
+
{ nil => { error: 'invalid response' } }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: firecord
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tomas Koutsky
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: jwt
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: httparty
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.14'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.14'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.14'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.14'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: webmock
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '2.3'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.3'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: vcr
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.0'
|
111
|
+
description: Firecord is an ODM (Object-Document-Mapper) framework for Firebase in
|
112
|
+
Ruby.
|
113
|
+
email:
|
114
|
+
- tomas@stepnivlk.net
|
115
|
+
executables: []
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- ".gitignore"
|
120
|
+
- ".rspec"
|
121
|
+
- ".rubocop.yml"
|
122
|
+
- ".travis.yml"
|
123
|
+
- CODE_OF_CONDUCT.md
|
124
|
+
- Gemfile
|
125
|
+
- LICENSE.txt
|
126
|
+
- README.md
|
127
|
+
- Rakefile
|
128
|
+
- bin/console
|
129
|
+
- bin/setup
|
130
|
+
- examples/address.rb
|
131
|
+
- firecord.gemspec
|
132
|
+
- lib/firecord.rb
|
133
|
+
- lib/firecord/configuration.rb
|
134
|
+
- lib/firecord/credentials.rb
|
135
|
+
- lib/firecord/error.rb
|
136
|
+
- lib/firecord/model.rb
|
137
|
+
- lib/firecord/patch/as.rb
|
138
|
+
- lib/firecord/record.rb
|
139
|
+
- lib/firecord/record/deserializer.rb
|
140
|
+
- lib/firecord/record/serializer.rb
|
141
|
+
- lib/firecord/repository/firebase.rb
|
142
|
+
- lib/firecord/repository/response.rb
|
143
|
+
- lib/firecord/version.rb
|
144
|
+
homepage: http://stepnivlk.net
|
145
|
+
licenses:
|
146
|
+
- MIT
|
147
|
+
metadata: {}
|
148
|
+
post_install_message:
|
149
|
+
rdoc_options: []
|
150
|
+
require_paths:
|
151
|
+
- lib
|
152
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
153
|
+
requirements:
|
154
|
+
- - ">="
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
158
|
+
requirements:
|
159
|
+
- - ">="
|
160
|
+
- !ruby/object:Gem::Version
|
161
|
+
version: '0'
|
162
|
+
requirements: []
|
163
|
+
rubyforge_project:
|
164
|
+
rubygems_version: 2.6.10
|
165
|
+
signing_key:
|
166
|
+
specification_version: 4
|
167
|
+
summary: Firecord is an ODM framework for Firebase in Ruby.
|
168
|
+
test_files: []
|