dayman 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +28 -0
- data/.gitignore +8 -0
- data/.rspec +2 -0
- data/.rubocop.yml +29 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +97 -0
- data/LICENSE.txt +21 -0
- data/README.md +70 -0
- data/Rakefile +7 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/circle.yml +3 -0
- data/dayman.gemspec +32 -0
- data/lib/dayman/parsers/base.rb +65 -0
- data/lib/dayman/parsers/collection_parser.rb +21 -0
- data/lib/dayman/parsers/member_parser.rb +20 -0
- data/lib/dayman/request/fields.rb +37 -0
- data/lib/dayman/request/filters.rb +24 -0
- data/lib/dayman/request/includes.rb +37 -0
- data/lib/dayman/request/sorting.rb +43 -0
- data/lib/dayman/request.rb +57 -0
- data/lib/dayman/resource.rb +61 -0
- data/lib/dayman/version.rb +4 -0
- data/lib/dayman.rb +9 -0
- metadata +209 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 23ccd4a07729e4665570159b15d4efcc36de7447
|
4
|
+
data.tar.gz: 4e2a406bd959744267e26a954b2a6bbbc8925a2a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0fd8ab23c5663b629f918f8e36e9f34c142da18b7981f169a22b2bf1f874c56099979d6f78be22218020a934b6ac3f9dc2c3884a41a4571b19879c02394b20f0
|
7
|
+
data.tar.gz: afc86fde581dc7b271a16160554bffc457169de043493d497020992d44cf93d0bb5734f35f82585f7f188bee93fd4ba1130e0e08fcdc7a447d14a5ff32fcc900
|
data/.codeclimate.yml
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
---
|
2
|
+
engines:
|
3
|
+
bundler-audit:
|
4
|
+
enabled: true
|
5
|
+
duplication:
|
6
|
+
enabled: true
|
7
|
+
config:
|
8
|
+
languages:
|
9
|
+
- ruby
|
10
|
+
- javascript
|
11
|
+
- python
|
12
|
+
- php
|
13
|
+
fixme:
|
14
|
+
enabled: true
|
15
|
+
rubocop:
|
16
|
+
enabled: true
|
17
|
+
ratings:
|
18
|
+
paths:
|
19
|
+
- Gemfile.lock
|
20
|
+
- "**.inc"
|
21
|
+
- "**.js"
|
22
|
+
- "**.jsx"
|
23
|
+
- "**.module"
|
24
|
+
- "**.php"
|
25
|
+
- "**.py"
|
26
|
+
- "**.rb"
|
27
|
+
exclude_paths:
|
28
|
+
- spec/
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.3
|
3
|
+
Exclude:
|
4
|
+
- 'dayman.gemspec'
|
5
|
+
|
6
|
+
Lint/AssignmentInCondition:
|
7
|
+
Enabled: false
|
8
|
+
|
9
|
+
Metrics/AbcSize:
|
10
|
+
Max: 20
|
11
|
+
|
12
|
+
Metrics/LineLength:
|
13
|
+
Max: 100
|
14
|
+
Exclude:
|
15
|
+
- 'spec/**/*.rb'
|
16
|
+
|
17
|
+
Metrics/MethodLength:
|
18
|
+
Max: 15
|
19
|
+
|
20
|
+
Style/AlignParameters:
|
21
|
+
EnforcedStyle: with_fixed_indentation
|
22
|
+
|
23
|
+
Style/Documentation:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
Style/PredicateName:
|
27
|
+
NameWhitelist:
|
28
|
+
- 'has_one'
|
29
|
+
- 'has_many'
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
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 contact@kevinbongart.net. 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
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
dayman (0.1.1)
|
5
|
+
activesupport
|
6
|
+
faraday
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activesupport (5.0.0.1)
|
12
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
+
i18n (~> 0.7)
|
14
|
+
minitest (~> 5.1)
|
15
|
+
tzinfo (~> 1.1)
|
16
|
+
addressable (2.4.0)
|
17
|
+
ast (2.3.0)
|
18
|
+
binding_of_caller (0.7.2)
|
19
|
+
debug_inspector (>= 0.0.1)
|
20
|
+
byebug (9.0.5)
|
21
|
+
coderay (1.1.1)
|
22
|
+
concurrent-ruby (1.0.2)
|
23
|
+
crack (0.4.3)
|
24
|
+
safe_yaml (~> 1.0.0)
|
25
|
+
debug_inspector (0.0.2)
|
26
|
+
diff-lcs (1.2.5)
|
27
|
+
faraday (0.10.0)
|
28
|
+
multipart-post (>= 1.2, < 3)
|
29
|
+
hashdiff (0.3.0)
|
30
|
+
i18n (0.7.0)
|
31
|
+
method_source (0.8.2)
|
32
|
+
minitest (5.9.1)
|
33
|
+
multipart-post (2.0.0)
|
34
|
+
parser (2.3.1.2)
|
35
|
+
ast (~> 2.2)
|
36
|
+
powerpack (0.1.1)
|
37
|
+
pry (0.10.4)
|
38
|
+
coderay (~> 1.1.0)
|
39
|
+
method_source (~> 0.8.1)
|
40
|
+
slop (~> 3.4)
|
41
|
+
pry-byebug (3.4.0)
|
42
|
+
byebug (~> 9.0)
|
43
|
+
pry (~> 0.10)
|
44
|
+
pry-rails (0.3.4)
|
45
|
+
pry (>= 0.9.10)
|
46
|
+
pry-stack_explorer (0.4.9.2)
|
47
|
+
binding_of_caller (>= 0.7)
|
48
|
+
pry (>= 0.9.11)
|
49
|
+
rainbow (2.1.0)
|
50
|
+
rake (11.2.2)
|
51
|
+
rspec (3.5.0)
|
52
|
+
rspec-core (~> 3.5.0)
|
53
|
+
rspec-expectations (~> 3.5.0)
|
54
|
+
rspec-mocks (~> 3.5.0)
|
55
|
+
rspec-core (3.5.2)
|
56
|
+
rspec-support (~> 3.5.0)
|
57
|
+
rspec-expectations (3.5.0)
|
58
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
59
|
+
rspec-support (~> 3.5.0)
|
60
|
+
rspec-mocks (3.5.0)
|
61
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
62
|
+
rspec-support (~> 3.5.0)
|
63
|
+
rspec-support (3.5.0)
|
64
|
+
rubocop (0.42.0)
|
65
|
+
parser (>= 2.3.1.1, < 3.0)
|
66
|
+
powerpack (~> 0.1)
|
67
|
+
rainbow (>= 1.99.1, < 3.0)
|
68
|
+
ruby-progressbar (~> 1.7)
|
69
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
70
|
+
ruby-progressbar (1.8.1)
|
71
|
+
safe_yaml (1.0.4)
|
72
|
+
slop (3.6.0)
|
73
|
+
thread_safe (0.3.5)
|
74
|
+
tzinfo (1.2.2)
|
75
|
+
thread_safe (~> 0.1)
|
76
|
+
unicode-display_width (1.1.0)
|
77
|
+
webmock (2.1.0)
|
78
|
+
addressable (>= 2.3.6)
|
79
|
+
crack (>= 0.3.2)
|
80
|
+
hashdiff
|
81
|
+
|
82
|
+
PLATFORMS
|
83
|
+
ruby
|
84
|
+
|
85
|
+
DEPENDENCIES
|
86
|
+
bundler
|
87
|
+
dayman!
|
88
|
+
pry-byebug
|
89
|
+
pry-rails
|
90
|
+
pry-stack_explorer
|
91
|
+
rake
|
92
|
+
rspec
|
93
|
+
rubocop
|
94
|
+
webmock
|
95
|
+
|
96
|
+
BUNDLED WITH
|
97
|
+
1.13.6
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Kevin Bongart
|
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,70 @@
|
|
1
|
+
# Dayman
|
2
|
+
|
3
|
+
![Dayman](http://screenshots.kevinbongart.net/AiJbs.gif)
|
4
|
+
|
5
|
+
Yet another JSON API client, heavily inspired by
|
6
|
+
[json_api_client](https://github.com/chingor13/json_api_client) and
|
7
|
+
[ActiveRecord](http://api.rubyonrails.org/classes/ActiveRecord/Base.html).
|
8
|
+
|
9
|
+
⚠️ Not nearly feature complete! ⚠️
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'dayman'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install dayman
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
module CoolPetsApi
|
31
|
+
class Base < Dayman::Resource
|
32
|
+
self.site = "http://coolpets.com"
|
33
|
+
end
|
34
|
+
|
35
|
+
class CoolDog < Base
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
CoolPetsApi::CoolDog.all
|
40
|
+
# GET "http://coolpets.com/cool_dogs"
|
41
|
+
|
42
|
+
CoolPetsApi::CoolDog.find(123)
|
43
|
+
# GET "http://coolpets.com/cool_dogs/123"
|
44
|
+
|
45
|
+
CoolPetsApi::CoolDog.where(name: "Wolfie").where(age: 2).all
|
46
|
+
# GET "http://coolpets.com/cool_dogs?filter[name]=Wolfie&filter[age]=2"
|
47
|
+
|
48
|
+
CoolPetsApi::CoolDog.select(:name, :age).select(friends: :coolness).all
|
49
|
+
# GET "http://coolpets.com/cool_dogs?fields[cool_dogs]=name,age&fields[friends]=coolness"
|
50
|
+
|
51
|
+
CoolPetsApi::CoolDog.includes(friends: :owner).all
|
52
|
+
# GET "http://coolpets.com/cool_dogs?include=friends.owner"
|
53
|
+
|
54
|
+
CoolPetsApi::CoolDog.sort(:age, name: :desc).all
|
55
|
+
# GET "http://coolpets.com/cool_dogs?sort=age,-name"
|
56
|
+
```
|
57
|
+
|
58
|
+
## Development
|
59
|
+
|
60
|
+
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.
|
61
|
+
|
62
|
+
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).
|
63
|
+
|
64
|
+
## Contributing
|
65
|
+
|
66
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/KevinBongart/dayman. 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.
|
67
|
+
|
68
|
+
## License
|
69
|
+
|
70
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'dayman'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start
|
data/bin/setup
ADDED
data/circle.yml
ADDED
data/dayman.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'dayman/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'dayman'
|
9
|
+
spec.version = Dayman::VERSION
|
10
|
+
spec.authors = ['Kevin Bongart']
|
11
|
+
spec.email = ['contact@kevinbongart.net']
|
12
|
+
|
13
|
+
spec.summary = 'A JSON API Ruby client'
|
14
|
+
spec.homepage = "https://github.com/KevinBongart/dayman"
|
15
|
+
spec.license = 'MIT'
|
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 'activesupport'
|
22
|
+
spec.add_dependency 'faraday'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler'
|
25
|
+
spec.add_development_dependency 'pry-byebug'
|
26
|
+
spec.add_development_dependency 'pry-rails'
|
27
|
+
spec.add_development_dependency 'pry-stack_explorer'
|
28
|
+
spec.add_development_dependency 'rake'
|
29
|
+
spec.add_development_dependency 'rspec'
|
30
|
+
spec.add_development_dependency 'rubocop'
|
31
|
+
spec.add_development_dependency 'webmock'
|
32
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Dayman
|
4
|
+
module Parsers
|
5
|
+
class Base
|
6
|
+
def initialize(resource:, response:)
|
7
|
+
@resource = resource
|
8
|
+
@raw_response = response
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def parsed_response
|
14
|
+
@parsed_response ||= begin
|
15
|
+
return if @raw_response.blank?
|
16
|
+
JSON.parse(@raw_response).deep_symbolize_keys
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def resource_class_for(item)
|
21
|
+
return if item.nil?
|
22
|
+
|
23
|
+
item[:type].classify.safe_constantize
|
24
|
+
end
|
25
|
+
|
26
|
+
def response_item_to_object(item)
|
27
|
+
return unless resource_class = resource_class_for(item)
|
28
|
+
|
29
|
+
resource_class.new(item.slice(:id, :attributes))
|
30
|
+
end
|
31
|
+
|
32
|
+
def build_relationship(resource, relationship_name, relationship_content)
|
33
|
+
return unless resource.respond_to?(relationship_name)
|
34
|
+
|
35
|
+
if relationship_content.is_a?(Array)
|
36
|
+
build_collection_relationship(resource, relationship_name, relationship_content)
|
37
|
+
else
|
38
|
+
build_member_relationship(resource, relationship_name, relationship_content)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def build_collection_relationship(resource, relationship_name, relationship_content)
|
43
|
+
relationship_content.each do |relationship_item|
|
44
|
+
included_item = find_included_item(relationship_item)
|
45
|
+
next unless object = response_item_to_object(included_item)
|
46
|
+
|
47
|
+
resource.send(relationship_name) << object
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def build_member_relationship(resource, relationship_name, relationship_content)
|
52
|
+
included_item = find_included_item(relationship_content)
|
53
|
+
return unless object = response_item_to_object(included_item)
|
54
|
+
|
55
|
+
resource.send("#{relationship_name}=", object)
|
56
|
+
end
|
57
|
+
|
58
|
+
def find_included_item(relationship_item)
|
59
|
+
parsed_response[:included].find do |e|
|
60
|
+
e.slice(:id, :type) == relationship_item[:data].slice(:id, :type)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'base'
|
4
|
+
|
5
|
+
module Dayman
|
6
|
+
module Parsers
|
7
|
+
class CollectionParser < Base
|
8
|
+
def parse
|
9
|
+
return [] if parsed_response.blank?
|
10
|
+
|
11
|
+
parsed_response[:data].map do |item|
|
12
|
+
response_item_to_object(item).tap do |resource|
|
13
|
+
item[:relationships]&.each do |relationship_name, relationship_content|
|
14
|
+
build_relationship(resource, relationship_name, relationship_content)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'base'
|
4
|
+
|
5
|
+
module Dayman
|
6
|
+
module Parsers
|
7
|
+
class MemberParser < Base
|
8
|
+
def parse
|
9
|
+
return if parsed_response.blank?
|
10
|
+
|
11
|
+
item = parsed_response[:data]
|
12
|
+
response_item_to_object(item).tap do |resource|
|
13
|
+
item[:relationships]&.each do |relationship_name, relationship_content|
|
14
|
+
build_relationship(resource, relationship_name, relationship_content)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Dayman
|
3
|
+
class Request
|
4
|
+
module Fields
|
5
|
+
# http://jsonapi.org/format/#fetching-sparse-fieldsets
|
6
|
+
def fields(*selected_fields)
|
7
|
+
Array(selected_fields).flatten.each do |arg|
|
8
|
+
if arg.is_a?(Hash)
|
9
|
+
arg.each do |key, values|
|
10
|
+
fieldsets[key] ||= []
|
11
|
+
fieldsets[key] += Array(values)
|
12
|
+
end
|
13
|
+
else
|
14
|
+
# TODO: use resource name
|
15
|
+
key = resource.path.to_sym
|
16
|
+
|
17
|
+
fieldsets[key] ||= []
|
18
|
+
fieldsets[key].push(arg)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
self
|
23
|
+
end
|
24
|
+
alias select fields
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def fieldsets_to_query_parameters
|
29
|
+
return {} if fieldsets.blank?
|
30
|
+
|
31
|
+
fieldsets.each_with_object({}) do |(key, values), hash|
|
32
|
+
hash["fields[#{key}]"] = values.join(',')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Dayman
|
3
|
+
class Request
|
4
|
+
module Filters
|
5
|
+
# http://jsonapi.org/format/#fetching-filtering
|
6
|
+
def filter(conditions)
|
7
|
+
filters.merge!(conditions)
|
8
|
+
|
9
|
+
self
|
10
|
+
end
|
11
|
+
alias where filter
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def filters_to_query_parameters
|
16
|
+
return {} if filters.blank?
|
17
|
+
|
18
|
+
filters.each_with_object({}) do |(key, value), hash|
|
19
|
+
hash["filter[#{key}]"] = value
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Dayman
|
3
|
+
class Request
|
4
|
+
module Includes
|
5
|
+
# http://jsonapi.org/format/#fetching-includes
|
6
|
+
def includes(*resources)
|
7
|
+
Array(resources).each do |resource|
|
8
|
+
included_resources.push(resource)
|
9
|
+
end
|
10
|
+
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def included_resources_to_query_parameters
|
17
|
+
included_resources = @included_resources.uniq.compact
|
18
|
+
return {} if included_resources.blank?
|
19
|
+
|
20
|
+
resources = []
|
21
|
+
included_resources.each do |resource|
|
22
|
+
if resource.is_a?(Hash)
|
23
|
+
resource.each do |key, values|
|
24
|
+
Array(values).each do |value|
|
25
|
+
resources.push([key, value].join('.'))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
else
|
29
|
+
resources.push(resource)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
{ include: resources.join(',') }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Dayman
|
3
|
+
class Request
|
4
|
+
module Sorting
|
5
|
+
SORT_ORDER_WHITELIST = [:asc, :desc].freeze
|
6
|
+
|
7
|
+
# http://jsonapi.org/format/#fetching-sorting
|
8
|
+
def sort(*args)
|
9
|
+
Array(args).each do |sort_field|
|
10
|
+
if sort_field.is_a?(Hash)
|
11
|
+
sort_field, order = sort_field.first
|
12
|
+
|
13
|
+
unless order.in?(SORT_ORDER_WHITELIST)
|
14
|
+
raise ArgumentError, 'sort order must be :asc or :desc (defaults to :asc)'
|
15
|
+
end
|
16
|
+
else
|
17
|
+
order = SORT_ORDER_WHITELIST.first
|
18
|
+
end
|
19
|
+
|
20
|
+
sort_fields.push(sort_field => order)
|
21
|
+
end
|
22
|
+
|
23
|
+
self
|
24
|
+
end
|
25
|
+
alias order sort
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def sort_fields_to_query_parameters
|
30
|
+
sort_fields = @sort_fields.uniq.compact.map do |sort_field|
|
31
|
+
field, order = sort_field.first
|
32
|
+
"#{'-' if order == :desc}#{field}"
|
33
|
+
end
|
34
|
+
|
35
|
+
if sort_fields.blank?
|
36
|
+
{}
|
37
|
+
else
|
38
|
+
{ sort: sort_fields.join(',') }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'request/fields'
|
4
|
+
require_relative 'request/filters'
|
5
|
+
require_relative 'request/includes'
|
6
|
+
require_relative 'request/sorting'
|
7
|
+
|
8
|
+
module Dayman
|
9
|
+
class Request
|
10
|
+
include Fields
|
11
|
+
include Filters
|
12
|
+
include Includes
|
13
|
+
include Sorting
|
14
|
+
|
15
|
+
attr_reader :resource,
|
16
|
+
:fieldsets,
|
17
|
+
:filters,
|
18
|
+
:included_resources,
|
19
|
+
:sort_fields
|
20
|
+
|
21
|
+
def initialize(resource)
|
22
|
+
@resource = resource
|
23
|
+
|
24
|
+
@fieldsets = {}
|
25
|
+
@filters = {}
|
26
|
+
@included_resources = []
|
27
|
+
@sort_fields = []
|
28
|
+
end
|
29
|
+
|
30
|
+
# http://jsonapi.org/format/#fetching-resources
|
31
|
+
def all
|
32
|
+
response = connection.get(resource.path, query_parameters)
|
33
|
+
Parsers::CollectionParser.new(resource: resource, response: response.body).parse
|
34
|
+
end
|
35
|
+
|
36
|
+
# http://jsonapi.org/format/#fetching-resources
|
37
|
+
def find(id)
|
38
|
+
response = connection.get([resource.path, id].join('/'))
|
39
|
+
Parsers::MemberParser.new(resource: resource, response: response.body).parse
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def connection
|
45
|
+
Faraday.new(url: resource.site)
|
46
|
+
end
|
47
|
+
|
48
|
+
def query_parameters
|
49
|
+
params = {}
|
50
|
+
|
51
|
+
params.merge!(filters_to_query_parameters)
|
52
|
+
params.merge!(included_resources_to_query_parameters)
|
53
|
+
params.merge!(fieldsets_to_query_parameters)
|
54
|
+
params.merge!(sort_fields_to_query_parameters)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Dayman
|
3
|
+
class Resource
|
4
|
+
attr_accessor :id, :attributes, :relationships
|
5
|
+
|
6
|
+
class << self
|
7
|
+
extend Forwardable
|
8
|
+
|
9
|
+
attr_accessor :site
|
10
|
+
|
11
|
+
def_delegators :request,
|
12
|
+
:all,
|
13
|
+
:fields,
|
14
|
+
:filter,
|
15
|
+
:find,
|
16
|
+
:includes,
|
17
|
+
:select,
|
18
|
+
:sort,
|
19
|
+
:where
|
20
|
+
|
21
|
+
def path
|
22
|
+
name.underscore.pluralize
|
23
|
+
end
|
24
|
+
|
25
|
+
def has_one(relationship_name)
|
26
|
+
attr_accessor relationship_name
|
27
|
+
end
|
28
|
+
|
29
|
+
def has_many(relationship_name)
|
30
|
+
instance_variable_name = "@#{relationship_name}".to_sym
|
31
|
+
|
32
|
+
define_method(relationship_name) do
|
33
|
+
if instance_variable_defined?(instance_variable_name)
|
34
|
+
instance_variable_get(instance_variable_name)
|
35
|
+
else
|
36
|
+
instance_variable_set(instance_variable_name, [])
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def request
|
44
|
+
Request.new(self)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def initialize(id: nil, attributes: {})
|
49
|
+
@id = id
|
50
|
+
@attributes = attributes
|
51
|
+
end
|
52
|
+
|
53
|
+
def method_missing(name, *args, &block)
|
54
|
+
attributes.key?(name) ? attributes[name] : super
|
55
|
+
end
|
56
|
+
|
57
|
+
def respond_to_missing?(name, include_private = false)
|
58
|
+
attributes.key?(name) || super
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/lib/dayman.rb
ADDED
metadata
ADDED
@@ -0,0 +1,209 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dayman
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kevin Bongart
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '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'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: faraday
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry-byebug
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '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'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry-rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry-stack_explorer
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rake
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rubocop
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: webmock
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
description:
|
154
|
+
email:
|
155
|
+
- contact@kevinbongart.net
|
156
|
+
executables: []
|
157
|
+
extensions: []
|
158
|
+
extra_rdoc_files: []
|
159
|
+
files:
|
160
|
+
- ".codeclimate.yml"
|
161
|
+
- ".gitignore"
|
162
|
+
- ".rspec"
|
163
|
+
- ".rubocop.yml"
|
164
|
+
- CODE_OF_CONDUCT.md
|
165
|
+
- Gemfile
|
166
|
+
- Gemfile.lock
|
167
|
+
- LICENSE.txt
|
168
|
+
- README.md
|
169
|
+
- Rakefile
|
170
|
+
- bin/console
|
171
|
+
- bin/setup
|
172
|
+
- circle.yml
|
173
|
+
- dayman.gemspec
|
174
|
+
- lib/dayman.rb
|
175
|
+
- lib/dayman/parsers/base.rb
|
176
|
+
- lib/dayman/parsers/collection_parser.rb
|
177
|
+
- lib/dayman/parsers/member_parser.rb
|
178
|
+
- lib/dayman/request.rb
|
179
|
+
- lib/dayman/request/fields.rb
|
180
|
+
- lib/dayman/request/filters.rb
|
181
|
+
- lib/dayman/request/includes.rb
|
182
|
+
- lib/dayman/request/sorting.rb
|
183
|
+
- lib/dayman/resource.rb
|
184
|
+
- lib/dayman/version.rb
|
185
|
+
homepage: https://github.com/KevinBongart/dayman
|
186
|
+
licenses:
|
187
|
+
- MIT
|
188
|
+
metadata: {}
|
189
|
+
post_install_message:
|
190
|
+
rdoc_options: []
|
191
|
+
require_paths:
|
192
|
+
- lib
|
193
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
194
|
+
requirements:
|
195
|
+
- - ">="
|
196
|
+
- !ruby/object:Gem::Version
|
197
|
+
version: '0'
|
198
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
199
|
+
requirements:
|
200
|
+
- - ">="
|
201
|
+
- !ruby/object:Gem::Version
|
202
|
+
version: '0'
|
203
|
+
requirements: []
|
204
|
+
rubyforge_project:
|
205
|
+
rubygems_version: 2.5.1
|
206
|
+
signing_key:
|
207
|
+
specification_version: 4
|
208
|
+
summary: A JSON API Ruby client
|
209
|
+
test_files: []
|