jacoat 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +6 -0
- data/Guardfile +70 -0
- data/LICENSE.txt +21 -0
- data/README.md +109 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/jacoat.gemspec +25 -0
- data/lib/jacoat/detector.rb +37 -0
- data/lib/jacoat/document/attribute.rb +22 -0
- data/lib/jacoat/document/data.rb +54 -0
- data/lib/jacoat/document/error.rb +28 -0
- data/lib/jacoat/document/included.rb +23 -0
- data/lib/jacoat/document/jsonapi.rb +14 -0
- data/lib/jacoat/document/link.rb +69 -0
- data/lib/jacoat/document/meta.rb +30 -0
- data/lib/jacoat/document/relationship.rb +49 -0
- data/lib/jacoat/document/resource.rb +40 -0
- data/lib/jacoat/document/resource_identifier.rb +48 -0
- data/lib/jacoat/document.rb +58 -0
- data/lib/jacoat/version.rb +3 -0
- data/lib/jacoat.rb +13 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b90bcea2b15f17d4e2766df1d3a8c18c50de275f
|
4
|
+
data.tar.gz: 4e11452c19ca00e9f38edbb110f6cb52d8f74073
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c0b4091682a4daaedf3c1eabf199e1a731ff78728283a26db4acdba7904f21955139f84602e7ace4af2d6d6b9da4b37cfc410be7b5f4d081a7263915cc44ed8f
|
7
|
+
data.tar.gz: 9fb97a37ba63348520f9157702f94b4e0fe6c214f3e41e5fd1adbaeb7b5119d203c92f9b857dc672b6914352e2fec96ca7083b8f32fa5e87171d61474efe75b5
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
## Uncomment and set this to only include directories you want to watch
|
5
|
+
# directories %w(app lib config test spec features) \
|
6
|
+
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
|
7
|
+
|
8
|
+
## Note: if you are using the `directories` clause above and you are not
|
9
|
+
## watching the project directory ('.'), then you will want to move
|
10
|
+
## the Guardfile to a watched dir and symlink it back, e.g.
|
11
|
+
#
|
12
|
+
# $ mkdir config
|
13
|
+
# $ mv Guardfile config/
|
14
|
+
# $ ln -s config/Guardfile .
|
15
|
+
#
|
16
|
+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
|
17
|
+
|
18
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
19
|
+
# rspec may be run, below are examples of the most common uses.
|
20
|
+
# * bundler: 'bundle exec rspec'
|
21
|
+
# * bundler binstubs: 'bin/rspec'
|
22
|
+
# * spring: 'bin/rspec' (This will use spring if running and you have
|
23
|
+
# installed the spring binstubs per the docs)
|
24
|
+
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
25
|
+
# * 'just' rspec: 'rspec'
|
26
|
+
|
27
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
28
|
+
require "guard/rspec/dsl"
|
29
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
30
|
+
|
31
|
+
# Feel free to open issues for suggestions and improvements
|
32
|
+
|
33
|
+
# RSpec files
|
34
|
+
rspec = dsl.rspec
|
35
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
36
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
37
|
+
watch(rspec.spec_files)
|
38
|
+
|
39
|
+
# Ruby files
|
40
|
+
ruby = dsl.ruby
|
41
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
42
|
+
|
43
|
+
# Rails files
|
44
|
+
rails = dsl.rails(view_extensions: %w(erb haml slim))
|
45
|
+
dsl.watch_spec_files_for(rails.app_files)
|
46
|
+
dsl.watch_spec_files_for(rails.views)
|
47
|
+
|
48
|
+
watch(rails.controllers) do |m|
|
49
|
+
[
|
50
|
+
rspec.spec.("routing/#{m[1]}_routing"),
|
51
|
+
rspec.spec.("controllers/#{m[1]}_controller"),
|
52
|
+
rspec.spec.("acceptance/#{m[1]}")
|
53
|
+
]
|
54
|
+
end
|
55
|
+
|
56
|
+
# Rails config changes
|
57
|
+
watch(rails.spec_helper) { rspec.spec_dir }
|
58
|
+
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
|
59
|
+
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
|
60
|
+
|
61
|
+
# Capybara features specs
|
62
|
+
watch(rails.view_dirs) { |m| rspec.spec.("features/#{m[1]}") }
|
63
|
+
watch(rails.layouts) { |m| rspec.spec.("features/#{m[1]}") }
|
64
|
+
|
65
|
+
# Turnip features and steps
|
66
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
67
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
|
68
|
+
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
|
69
|
+
end
|
70
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Celso Fernandes
|
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,109 @@
|
|
1
|
+
# Jacoat
|
2
|
+
|
3
|
+
[](https://travis-ci.org/fernandes/jacoat)
|
4
|
+
|
5
|
+
Jacoat is a Ruby Coat for your JSON-API Hashes
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'jacoat'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install jacoat
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Consider you have a have like this one:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
{
|
29
|
+
"type": "articles",
|
30
|
+
"id": "1",
|
31
|
+
"attributes": {
|
32
|
+
"title": "Rails is Omakase"
|
33
|
+
},
|
34
|
+
"relationships": {
|
35
|
+
"author": {
|
36
|
+
"links": {
|
37
|
+
"self": "/articles/1/relationships/author",
|
38
|
+
"related": "/articles/1/author"
|
39
|
+
},
|
40
|
+
"data": { "type": "people", "id": "9" }
|
41
|
+
}
|
42
|
+
}
|
43
|
+
}
|
44
|
+
```
|
45
|
+
|
46
|
+
You can just:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
Jacoat::Document.new(hash)
|
50
|
+
```
|
51
|
+
|
52
|
+
And you will have your JSON-API hash parsed to Ruby Objects, then you can access your document properties like this:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
document.data.resource.attributes
|
56
|
+
# => #<Jacoat::Document::Attributes:0x007fb9223b92b8 @hash={:title=>"Rails is Omakase"}>
|
57
|
+
```
|
58
|
+
|
59
|
+
Considering you want to change your title attribute to "Jacoat is a cool gem"
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
document.data.resource.attributes.title = "Jacoat is a cool gem"
|
63
|
+
# => "Jacoat is a cool gem"
|
64
|
+
```
|
65
|
+
|
66
|
+
And know you what to generate your JSON-API hash again, here we go...
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
document.to_hash
|
70
|
+
# => {
|
71
|
+
# :data=>{
|
72
|
+
# :type=>"articles",
|
73
|
+
# :id=>"1",
|
74
|
+
# :attributes=>{
|
75
|
+
# :title=>"Jacoat is a cool gem"
|
76
|
+
# },
|
77
|
+
# :relationships=>{
|
78
|
+
# :author=>{
|
79
|
+
# :links=>{
|
80
|
+
# :self=>"/articles/1/relationships/author",
|
81
|
+
# :related=>"/articles/1/author"
|
82
|
+
# },
|
83
|
+
# :data=>{
|
84
|
+
# :type=>"people",
|
85
|
+
# :id=>"9"
|
86
|
+
# }
|
87
|
+
# }
|
88
|
+
# }
|
89
|
+
# }
|
90
|
+
# }
|
91
|
+
```
|
92
|
+
|
93
|
+
## Development
|
94
|
+
|
95
|
+
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.
|
96
|
+
|
97
|
+
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).
|
98
|
+
|
99
|
+
## Contributing
|
100
|
+
|
101
|
+
You know how to do it! ;)
|
102
|
+
|
103
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/fernandes/jacoat.
|
104
|
+
|
105
|
+
|
106
|
+
## License
|
107
|
+
|
108
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
109
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "jacoat"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/jacoat.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'jacoat/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "jacoat"
|
8
|
+
spec.version = Jacoat::VERSION
|
9
|
+
spec.authors = ["Celso Fernandes"]
|
10
|
+
spec.email = ["fernandes@zertico.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Your JSON-API Coat in Ruby}
|
13
|
+
spec.description = %q{Handle JSON-API Hashes as Ruby Objects}
|
14
|
+
spec.homepage = "http://github.com/fernandes/jacoat"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Jacoat
|
2
|
+
module Detector
|
3
|
+
def self.what_is(hash)
|
4
|
+
return "compound" if is_compound?(hash)
|
5
|
+
return "document" if is_document?(hash)
|
6
|
+
return "resource" if is_resource?(hash)
|
7
|
+
return "resource_identifier" if is_resource_identifier?(hash)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.is_compound?(hash)
|
11
|
+
return true if hash.has_key?(:data) and hash.has_key?(:included) and hash[:data].is_a?(Array)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.is_document?(hash)
|
15
|
+
return true if hash.has_key?(:data) and !hash[:data].is_a?(Array)
|
16
|
+
return true if hash.has_key?(:errors)
|
17
|
+
return true if hash.has_key?(:meta)
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.is_resource_identifier?(hash)
|
21
|
+
return true if hash.has_key?(:type) &&
|
22
|
+
hash.has_key?(:id) &&
|
23
|
+
!hash.has_key?(:attributes)
|
24
|
+
!hash.has_key?(:relationships)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.is_resource?(hash)
|
28
|
+
# resource when parsing (must have id)
|
29
|
+
return true if hash.has_key?(:type) &&
|
30
|
+
hash.has_key?(:id) &&
|
31
|
+
hash.has_key?(:attributes)
|
32
|
+
# or a resource when being created (without id)
|
33
|
+
return true if hash.has_key?(:type) &&
|
34
|
+
hash.has_key?(:attributes)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Jacoat
|
2
|
+
class Document
|
3
|
+
class Attributes
|
4
|
+
def initialize(arguments = {})
|
5
|
+
@hash = arguments
|
6
|
+
end
|
7
|
+
|
8
|
+
def method_missing(m, *args)
|
9
|
+
#setter
|
10
|
+
if /^(\w+)=$/ =~ m
|
11
|
+
@hash[:"#{$1}"] = args[0]
|
12
|
+
end
|
13
|
+
#getter
|
14
|
+
@hash[:"#{m}"]
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_hash
|
18
|
+
proc = Proc.new { |k, v| v.kind_of?(Hash) ? (v.delete_if(&proc); nil) : v.nil? }; @hash.delete_if(&proc)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Jacoat
|
2
|
+
class Document
|
3
|
+
class Data
|
4
|
+
def self.process(arguments)
|
5
|
+
data = Data.new
|
6
|
+
if arguments.is_a?(Array)
|
7
|
+
data.resource = create_resource_array(arguments)
|
8
|
+
else
|
9
|
+
data.resource = create_resource(arguments)
|
10
|
+
end
|
11
|
+
data
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.create_resource(arguments)
|
15
|
+
return nil if arguments.nil?
|
16
|
+
if Detector.what_is(arguments) == "resource"
|
17
|
+
return Resource.new(arguments)
|
18
|
+
else
|
19
|
+
return ResourceIdentifier.new(arguments)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.create_resource_array(arguments)
|
24
|
+
resources = []
|
25
|
+
arguments.each do |resource|
|
26
|
+
if Detector.what_is(resource) == "resource"
|
27
|
+
resources << Resource.new(resource)
|
28
|
+
else
|
29
|
+
resources << ResourceIdentifier.new(resource)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
resources
|
33
|
+
end
|
34
|
+
|
35
|
+
attr_accessor :resource
|
36
|
+
def resources
|
37
|
+
@resource
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_hash
|
41
|
+
return nil unless @resource
|
42
|
+
if @resource.is_a?(Array)
|
43
|
+
array = []
|
44
|
+
@resource.each do |data|
|
45
|
+
array << data.to_hash
|
46
|
+
end
|
47
|
+
return array
|
48
|
+
else
|
49
|
+
@resource.to_hash
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Jacoat
|
2
|
+
class Document
|
3
|
+
class Error
|
4
|
+
attr_reader :id, :links, :status, :code, :title, :detail, :meta
|
5
|
+
def initialize(arguments = {})
|
6
|
+
@id = arguments[:id]
|
7
|
+
@links = Link.process(arguments[:links]) if arguments.has_key?(:links)
|
8
|
+
%w{ status code title detail }.each do |type|
|
9
|
+
self.instance_variable_set("@#{type}", arguments[type.to_sym]) if arguments.has_key?(type.to_sym)
|
10
|
+
end
|
11
|
+
@meta = Meta.new(arguments[:meta]) if arguments.has_key?(:meta)
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_hash
|
15
|
+
hash = {}
|
16
|
+
hash[:id] = @id if @id
|
17
|
+
hash[:links] = @links.to_hash if @links
|
18
|
+
%w{ status code title detail }.each do |type|
|
19
|
+
if self.instance_variable_defined?("@#{type}".to_sym)
|
20
|
+
hash[type.to_sym] = instance_variable_get("@#{type}".to_sym)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
hash[:meta] = @meta.to_hash if @meta
|
24
|
+
hash
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Jacoat
|
2
|
+
class Document
|
3
|
+
class Included
|
4
|
+
def self.process(arguments)
|
5
|
+
resources = []
|
6
|
+
arguments.each do |item|
|
7
|
+
resources << Resource.new(item)
|
8
|
+
end
|
9
|
+
included = Included.new
|
10
|
+
included.resources = resources
|
11
|
+
included
|
12
|
+
end
|
13
|
+
attr_accessor :resources
|
14
|
+
def to_hash
|
15
|
+
array = []
|
16
|
+
@resources.each do |resource|
|
17
|
+
array << resource.to_hash
|
18
|
+
end
|
19
|
+
return array
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Jacoat
|
2
|
+
class Document
|
3
|
+
class Link
|
4
|
+
def self.process(hash)
|
5
|
+
Link.new(hash)
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize(arguments = {})
|
9
|
+
@hash = {}
|
10
|
+
process_links(arguments)
|
11
|
+
end
|
12
|
+
|
13
|
+
def method_missing(m, *args)
|
14
|
+
#setter
|
15
|
+
if /^(\w+)=$/ =~ m
|
16
|
+
@hash[:"#{$1}"] = args[0]
|
17
|
+
end
|
18
|
+
#getter
|
19
|
+
@hash[:"#{m}"]
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_hash
|
23
|
+
hash = {}
|
24
|
+
@hash.each_pair do |k, v|
|
25
|
+
hash[k] = v.to_hash
|
26
|
+
end
|
27
|
+
hash
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
def process_links(arguments)
|
32
|
+
arguments.each_pair do |k, v|
|
33
|
+
if v.is_a?(String)
|
34
|
+
link = Simple.new(v)
|
35
|
+
else
|
36
|
+
link = Complex.new(v)
|
37
|
+
end
|
38
|
+
send("#{k}=", link)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class Simple
|
43
|
+
attr_reader :href
|
44
|
+
def initialize(href)
|
45
|
+
@href = href
|
46
|
+
end
|
47
|
+
|
48
|
+
def to_hash
|
49
|
+
href
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class Complex
|
54
|
+
attr_reader :href, :meta
|
55
|
+
def initialize(arguments)
|
56
|
+
@href = arguments[:href]
|
57
|
+
@meta = Meta.new(arguments[:meta])
|
58
|
+
end
|
59
|
+
|
60
|
+
def to_hash
|
61
|
+
hash = {}
|
62
|
+
hash.merge!(href: href) if @href
|
63
|
+
hash.merge!(meta: meta.to_hash) if @meta
|
64
|
+
hash
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Jacoat
|
2
|
+
class Document
|
3
|
+
class Meta
|
4
|
+
def initialize(arguments = {})
|
5
|
+
@hash = {}
|
6
|
+
process_meta(arguments)
|
7
|
+
end
|
8
|
+
|
9
|
+
def method_missing(m, *args)
|
10
|
+
#setter
|
11
|
+
if /^(\w+)=$/ =~ m
|
12
|
+
@hash[:"#{$1}"] = args[0]
|
13
|
+
end
|
14
|
+
#getter
|
15
|
+
@hash[:"#{m}"]
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_hash
|
19
|
+
@hash
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def process_meta(arguments)
|
24
|
+
arguments.each_pair do |k, v|
|
25
|
+
send("#{k}=", v)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Jacoat
|
2
|
+
class Document
|
3
|
+
class Relationship
|
4
|
+
attr_reader :id, :type, :data, :links
|
5
|
+
def self.process(hash)
|
6
|
+
relationships = []
|
7
|
+
hash.each_pair do |k, v|
|
8
|
+
relationships << Document::Relationship.new(k, v)
|
9
|
+
end
|
10
|
+
relationships
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(type, body)
|
14
|
+
@type = type.to_s
|
15
|
+
process_data(body[:data]) if body.has_key?(:data)
|
16
|
+
process_body(body[:links]) if body.has_key?(:links)
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_hash
|
20
|
+
hash = {}
|
21
|
+
hash[type.to_sym] = {}
|
22
|
+
hash[type.to_sym].merge!(links: @links.to_hash) if @links
|
23
|
+
hash[type.to_sym].merge!(data: data_to_hash) if @data
|
24
|
+
hash
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
def process_data(data)
|
29
|
+
@data = Document::ResourceIdentifier.process(data)
|
30
|
+
end
|
31
|
+
|
32
|
+
def process_body(links)
|
33
|
+
@links = Document::Link.process(links)
|
34
|
+
end
|
35
|
+
|
36
|
+
def data_to_hash
|
37
|
+
if @data.is_a?(Array)
|
38
|
+
array = []
|
39
|
+
@data.each do |data|
|
40
|
+
array << data.to_hash
|
41
|
+
end
|
42
|
+
return array
|
43
|
+
else
|
44
|
+
@data.to_hash
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Jacoat
|
2
|
+
class Document
|
3
|
+
class Resource
|
4
|
+
attr_reader :id, :type, :attributes, :links, :relationships
|
5
|
+
def initialize(arguments)
|
6
|
+
@id = arguments[:id]
|
7
|
+
@type = arguments[:type]
|
8
|
+
@attributes = Document::Attributes.new(arguments[:attributes])
|
9
|
+
create_relationships(arguments[:relationships]) if arguments.has_key?(:relationships)
|
10
|
+
@links = Link.process(arguments[:links]) if arguments.has_key?(:links)
|
11
|
+
end
|
12
|
+
|
13
|
+
def create_relationships(hash)
|
14
|
+
@relationships = []
|
15
|
+
hash.each_pair do |k, v|
|
16
|
+
@relationships << Document::Relationship.new(k, v)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_hash
|
21
|
+
hash = {
|
22
|
+
type: @type,
|
23
|
+
id: @id.to_s,
|
24
|
+
attributes: @attributes.to_hash
|
25
|
+
}
|
26
|
+
hash.merge!(links: @links.to_hash) if @links
|
27
|
+
hash.merge!(relationships: relationship_hash) if @relationships
|
28
|
+
hash
|
29
|
+
end
|
30
|
+
|
31
|
+
def relationship_hash
|
32
|
+
hash = {}
|
33
|
+
@relationships.each do |relationship|
|
34
|
+
hash.merge!(relationship.to_hash)
|
35
|
+
end
|
36
|
+
hash
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Jacoat
|
2
|
+
class Document
|
3
|
+
class ResourceIdentifier
|
4
|
+
attr_reader :id, :type, :relationships
|
5
|
+
def self.process(body)
|
6
|
+
if body.is_a?(Array)
|
7
|
+
resources = []
|
8
|
+
body.each do |item|
|
9
|
+
resources << ResourceIdentifier.new(item)
|
10
|
+
end
|
11
|
+
else
|
12
|
+
resources = ResourceIdentifier.new(body)
|
13
|
+
end
|
14
|
+
resources
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(arguments)
|
18
|
+
@id = arguments[:id]
|
19
|
+
@type = arguments[:type]
|
20
|
+
create_relationships(arguments[:relationships]) if arguments.has_key?(:relationships)
|
21
|
+
end
|
22
|
+
|
23
|
+
def create_relationships(hash)
|
24
|
+
@relationships = []
|
25
|
+
hash.each_pair do |k, v|
|
26
|
+
@relationships << Document::Relationship.new(k, v)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_hash
|
31
|
+
hash = {
|
32
|
+
type: @type,
|
33
|
+
id: @id
|
34
|
+
}
|
35
|
+
hash.merge!(relationships: relationship_hash) if @relationships
|
36
|
+
hash
|
37
|
+
end
|
38
|
+
|
39
|
+
def relationship_hash
|
40
|
+
hash = {}
|
41
|
+
@relationships.each do |relationship|
|
42
|
+
hash.merge!(relationship.to_hash)
|
43
|
+
end
|
44
|
+
hash
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'jacoat/document/data'
|
2
|
+
require 'jacoat/document/error'
|
3
|
+
require 'jacoat/document/meta'
|
4
|
+
require 'jacoat/document/attribute'
|
5
|
+
require 'jacoat/document/jsonapi'
|
6
|
+
require 'jacoat/document/link'
|
7
|
+
require 'jacoat/document/included'
|
8
|
+
require 'jacoat/document/resource_identifier'
|
9
|
+
require 'jacoat/document/resource'
|
10
|
+
require 'jacoat/document/relationship'
|
11
|
+
|
12
|
+
module Jacoat
|
13
|
+
class Document
|
14
|
+
attr_reader :data, :errors, :meta, :jsonapi, :links, :included
|
15
|
+
def initialize(arguments = {})
|
16
|
+
validate_arguments(arguments)
|
17
|
+
@has_data = true if arguments.has_key?(:data)
|
18
|
+
@data = Data.process(arguments[:data]) if arguments.has_key?(:data)
|
19
|
+
@errors = Error.new(arguments[:errors]) if arguments.has_key?(:errors)
|
20
|
+
@meta = Meta.new(arguments[:meta]) if arguments.has_key?(:meta)
|
21
|
+
@jsonapi = Jsonapi.new(arguments[:jsonapi]) if arguments.has_key?(:jsonapi)
|
22
|
+
@links = Link.process(arguments[:links]) if arguments.has_key?(:links)
|
23
|
+
@included = Included.process(arguments[:included]) if arguments.has_key?(:included)
|
24
|
+
end
|
25
|
+
|
26
|
+
def validate_arguments(arguments)
|
27
|
+
raise Invalid.new('included key without data key') if arguments.has_key?(:included) && !arguments.has_key?(:data)
|
28
|
+
raise Invalid.new('must contain data, errors or meta key') if !arguments.has_key?(:data) && !arguments.has_key?(:errors) && !arguments.has_key?(:meta)
|
29
|
+
raise Invalid.new('data and errors keys set') if arguments.has_key?(:data) && arguments.has_key?(:errors)
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_hash
|
33
|
+
hash = {}
|
34
|
+
hash[:data] = data_hash if @has_data
|
35
|
+
%w{ errors meta jsonapi links included }.each do |type|
|
36
|
+
if self.instance_variable_defined?("@#{type}".to_sym)
|
37
|
+
hash[type.to_sym] = instance_variable_get("@#{type}".to_sym).to_hash
|
38
|
+
end
|
39
|
+
end
|
40
|
+
hash
|
41
|
+
end
|
42
|
+
|
43
|
+
def data_hash
|
44
|
+
return nil if @data.nil? and @has_data
|
45
|
+
if @data.is_a?(Array)
|
46
|
+
array = []
|
47
|
+
@data.each do |data|
|
48
|
+
array << data.to_hash
|
49
|
+
end
|
50
|
+
return array
|
51
|
+
else
|
52
|
+
@data.to_hash
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class Invalid < Exception; end
|
57
|
+
end
|
58
|
+
end
|
data/lib/jacoat.rb
ADDED
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jacoat
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Celso Fernandes
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
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
|
+
description: Handle JSON-API Hashes as Ruby Objects
|
56
|
+
email:
|
57
|
+
- fernandes@zertico.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- Guardfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- jacoat.gemspec
|
73
|
+
- lib/jacoat.rb
|
74
|
+
- lib/jacoat/detector.rb
|
75
|
+
- lib/jacoat/document.rb
|
76
|
+
- lib/jacoat/document/attribute.rb
|
77
|
+
- lib/jacoat/document/data.rb
|
78
|
+
- lib/jacoat/document/error.rb
|
79
|
+
- lib/jacoat/document/included.rb
|
80
|
+
- lib/jacoat/document/jsonapi.rb
|
81
|
+
- lib/jacoat/document/link.rb
|
82
|
+
- lib/jacoat/document/meta.rb
|
83
|
+
- lib/jacoat/document/relationship.rb
|
84
|
+
- lib/jacoat/document/resource.rb
|
85
|
+
- lib/jacoat/document/resource_identifier.rb
|
86
|
+
- lib/jacoat/version.rb
|
87
|
+
homepage: http://github.com/fernandes/jacoat
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.4.8
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Your JSON-API Coat in Ruby
|
111
|
+
test_files: []
|