jsonapi-client 0.1.1.beta1
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/README.md +121 -0
- data/lib/jsonapi/client.rb +7 -0
- data/lib/jsonapi/client/document.rb +63 -0
- data/lib/jsonapi/client/error.rb +52 -0
- data/lib/jsonapi/client/link.rb +26 -0
- data/lib/jsonapi/client/relationship.rb +43 -0
- data/lib/jsonapi/client/resource.rb +40 -0
- metadata +92 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b2e9034a41c293b47e0e8316f9f44b61769b1d99
|
4
|
+
data.tar.gz: 457f801e7143c2e03f25716590ced6315acfc855
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: abba8780cd184a180682b68c9f1d3f72754c618a344477c7b32ff4d3d962031a6c1e0e4927d2352f93d866aa4a1b4f69231ac5037ea5bbe42982466e4992322c
|
7
|
+
data.tar.gz: 0dbaf0e531dd275d687c971af1157dcac0b6c39d016a58023838e897544ff8600e0cba0e26d068824432bb71a62c5d71542123ae1f0cb0503ceffe791cc58be3
|
data/README.md
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
# jsonapi-client
|
2
|
+
Ruby gem for consuming [JSON API](http://jsonapi.org) documents.
|
3
|
+
|
4
|
+
## Status
|
5
|
+
|
6
|
+
[](https://badge.fury.io/rb/jsonapi-client)
|
7
|
+
[](http://travis-ci.org/jsonapi-rb/client?branch=master)
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
```ruby
|
11
|
+
# In Gemfile
|
12
|
+
gem 'jsonapi-client'
|
13
|
+
```
|
14
|
+
then
|
15
|
+
```
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
or manually via
|
19
|
+
```
|
20
|
+
$ gem install jsonapi-client
|
21
|
+
```
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
First, require the gem:
|
26
|
+
```ruby
|
27
|
+
require 'jsonapi/client'
|
28
|
+
```
|
29
|
+
|
30
|
+
Then
|
31
|
+
```ruby
|
32
|
+
payload = {
|
33
|
+
"links" => {
|
34
|
+
"self" => "http://example.com/articles",
|
35
|
+
"next" => "http://example.com/articles?page[offset]=2",
|
36
|
+
"last" => "http://example.com/articles?page[offset]=10"
|
37
|
+
},
|
38
|
+
"data" => [
|
39
|
+
{
|
40
|
+
"type" => "articles",
|
41
|
+
"id" => "1",
|
42
|
+
"attributes" => {
|
43
|
+
"title" => "JSON API paints my bikeshed!"
|
44
|
+
},
|
45
|
+
"relationships" => {
|
46
|
+
"author" => {
|
47
|
+
"links" => {
|
48
|
+
"self" => "http://example.com/articles/1/relationships/author",
|
49
|
+
"related" => "http://example.com/articles/1/author"
|
50
|
+
},
|
51
|
+
"data" => { "type" => "people", "id" => "9" }
|
52
|
+
},
|
53
|
+
"comments" => {
|
54
|
+
"links" => {
|
55
|
+
"self" => "http://example.com/articles/1/relationships/comments",
|
56
|
+
"related" => "http://example.com/articles/1/comments"
|
57
|
+
},
|
58
|
+
"data" => [
|
59
|
+
{ "type" => "comments", "id" => "5" },
|
60
|
+
{ "type" => "comments", "id" => "12" }
|
61
|
+
]
|
62
|
+
}
|
63
|
+
},
|
64
|
+
"links" => {
|
65
|
+
"self" => "http://example.com/articles/1"
|
66
|
+
}
|
67
|
+
}
|
68
|
+
],
|
69
|
+
"included" => [
|
70
|
+
{
|
71
|
+
"type" => "people",
|
72
|
+
"id" => "9",
|
73
|
+
"attributes" => {
|
74
|
+
"first-name" => "Dan",
|
75
|
+
"last-name" => "Gebhardt",
|
76
|
+
"twitter" => "dgeb"
|
77
|
+
},
|
78
|
+
"links" => {
|
79
|
+
"self" => "http://example.com/people/9"
|
80
|
+
}
|
81
|
+
}, {
|
82
|
+
"type" => "comments",
|
83
|
+
"id" => "5",
|
84
|
+
"attributes" => {
|
85
|
+
"body" => "First!"
|
86
|
+
},
|
87
|
+
"relationships" => {
|
88
|
+
"author" => {
|
89
|
+
"data" => { "type" => "people", "id" => "2" }
|
90
|
+
}
|
91
|
+
},
|
92
|
+
"links" => {
|
93
|
+
"self" => "http://example.com/comments/5"
|
94
|
+
}
|
95
|
+
}, {
|
96
|
+
"type" => "comments",
|
97
|
+
"id" => "12",
|
98
|
+
"attributes" => {
|
99
|
+
"body" => "I like XML better"
|
100
|
+
},
|
101
|
+
"relationships" => {
|
102
|
+
"author" => {
|
103
|
+
"data" => { "type" => "people", "id" => "9" }
|
104
|
+
}
|
105
|
+
},
|
106
|
+
"links" => {
|
107
|
+
"self" => "http://example.com/comments/12"
|
108
|
+
}
|
109
|
+
}
|
110
|
+
]
|
111
|
+
}
|
112
|
+
|
113
|
+
document = JSONAPI::Client::Document.new(json_hash)
|
114
|
+
|
115
|
+
document.data[0].relationships['author'].data.attributes['first-name']
|
116
|
+
# => "Dan"
|
117
|
+
```
|
118
|
+
|
119
|
+
## License
|
120
|
+
|
121
|
+
jsonapi-client is released under the [MIT License](http://www.opensource.org/licenses/MIT).
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module JSONAPI
|
2
|
+
module Client
|
3
|
+
class Document
|
4
|
+
attr_reader :data, :included, :errors, :links, :meta
|
5
|
+
|
6
|
+
def initialize(document, link_data = true)
|
7
|
+
JSONAPI::Parser::Document.parse!(document)
|
8
|
+
parse_data!(document['data'], document['included'])
|
9
|
+
link_data! if link_data
|
10
|
+
parse_errors!(document['errors'])
|
11
|
+
parse_links!(document['links'])
|
12
|
+
parse_meta!(document['meta'])
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def parse_data!(data, included)
|
18
|
+
return unless data
|
19
|
+
@data =
|
20
|
+
if data.respond_to?(:each)
|
21
|
+
data.map { |h| Resource.new(h) }
|
22
|
+
else
|
23
|
+
Resource.new(data)
|
24
|
+
end
|
25
|
+
@included = Array(included).map { |h| Resource.new(h) }
|
26
|
+
end
|
27
|
+
|
28
|
+
def index
|
29
|
+
return @index unless @index.nil?
|
30
|
+
@index = {}
|
31
|
+
(Array(@data) + @included).each do |resource|
|
32
|
+
resource_identifier = [resource.type, resource.id]
|
33
|
+
@index[resource_identifier] = resource
|
34
|
+
end
|
35
|
+
|
36
|
+
@index
|
37
|
+
end
|
38
|
+
|
39
|
+
def link_data!
|
40
|
+
(Array(@data) + @included).each do |resource|
|
41
|
+
resource.relationships.each do |_, rel|
|
42
|
+
rel.link_data!(index)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def parse_errors!(errors)
|
48
|
+
@errors = Array(errors).map { |h| Error.new(h) }
|
49
|
+
end
|
50
|
+
|
51
|
+
def parse_links!(links)
|
52
|
+
@links = {}
|
53
|
+
(links || {}).map do |key, h|
|
54
|
+
@links[key] = Link.new(h)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def parse_meta!(meta)
|
59
|
+
@meta = meta
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module JSONAPI
|
2
|
+
module Client
|
3
|
+
class Error
|
4
|
+
attr_reader :id, :links, :status, :code, :title, :detail, :source, :meta
|
5
|
+
|
6
|
+
def initialize(error)
|
7
|
+
parse_id!(error['id'])
|
8
|
+
parse_links!(error['links'])
|
9
|
+
parse_status!(error['status'])
|
10
|
+
parse_code!(error['code'])
|
11
|
+
parse_title!(error['title'])
|
12
|
+
parse_detail!(error['detail'])
|
13
|
+
parse_source!(error['source'])
|
14
|
+
parse_meta!(error['meta'])
|
15
|
+
end
|
16
|
+
|
17
|
+
def parse_id!(id)
|
18
|
+
@id = id
|
19
|
+
end
|
20
|
+
|
21
|
+
def parse_links!(links)
|
22
|
+
(links || {}).map do |key, h|
|
23
|
+
@links[key] = Link.new(h)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def parse_status!(status)
|
28
|
+
@status = status
|
29
|
+
end
|
30
|
+
|
31
|
+
def parse_code!(code)
|
32
|
+
@code = code
|
33
|
+
end
|
34
|
+
|
35
|
+
def parse_title!(title)
|
36
|
+
@title = title
|
37
|
+
end
|
38
|
+
|
39
|
+
def parse_detail!(detail)
|
40
|
+
@detail = detail
|
41
|
+
end
|
42
|
+
|
43
|
+
def parse_source!(source)
|
44
|
+
@source = source
|
45
|
+
end
|
46
|
+
|
47
|
+
def parse_meta!(meta)
|
48
|
+
@meta = meta
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module JSONAPI
|
2
|
+
module Client
|
3
|
+
class Link
|
4
|
+
attr_reader :href, :meta
|
5
|
+
|
6
|
+
def initialize(relationship)
|
7
|
+
if relationship.is_a?(String)
|
8
|
+
parse_href!(relationship)
|
9
|
+
else
|
10
|
+
parse_href!(relationship['href'])
|
11
|
+
parse_meta!(relationship['meta'])
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def parse_href!(href)
|
18
|
+
@href = href
|
19
|
+
end
|
20
|
+
|
21
|
+
def parse_meta!(meta)
|
22
|
+
@meta = meta
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module JSONAPI
|
2
|
+
module Client
|
3
|
+
class Relationship
|
4
|
+
attr_reader :data, :links, :meta
|
5
|
+
|
6
|
+
def initialize(relationship)
|
7
|
+
parse_data!(relationship['data'])
|
8
|
+
parse_links!(relationship['links'])
|
9
|
+
parse_meta!(relationship['meta'])
|
10
|
+
end
|
11
|
+
|
12
|
+
# @api private
|
13
|
+
def link_data!(index)
|
14
|
+
if @data.is_a?(Array)
|
15
|
+
@data.map! do |ri|
|
16
|
+
ri = [ri['type'], ri['id']]
|
17
|
+
index[ri]
|
18
|
+
end
|
19
|
+
elsif !@data.nil?
|
20
|
+
ri = [@data['type'], @data['id']]
|
21
|
+
@data = index[ri]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def parse_data!(data)
|
28
|
+
@data = data
|
29
|
+
end
|
30
|
+
|
31
|
+
def parse_links!(links)
|
32
|
+
@links = {}
|
33
|
+
(links || {}).map do |key, h|
|
34
|
+
@links[key] = Link.new(h)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def parse_meta!(meta)
|
39
|
+
@meta = meta
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module JSONAPI
|
2
|
+
module Client
|
3
|
+
class Resource
|
4
|
+
attr_reader :id, :type, :attributes, :relationships, :links, :meta
|
5
|
+
|
6
|
+
def initialize(resource)
|
7
|
+
@id = resource['id']
|
8
|
+
@type = resource['type']
|
9
|
+
parse_attributes!(resource['attributes'])
|
10
|
+
parse_relationships!(resource['relationships'])
|
11
|
+
parse_links!(resource['links'])
|
12
|
+
parse_meta!(resource['meta'])
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def parse_attributes!(attributes)
|
18
|
+
@attributes = attributes || {}
|
19
|
+
end
|
20
|
+
|
21
|
+
def parse_relationships!(relationships)
|
22
|
+
@relationships = {}
|
23
|
+
(relationships || {}).map do |key, h|
|
24
|
+
@relationships[key] = Relationship.new(h)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def parse_links!(links)
|
29
|
+
@links = {}
|
30
|
+
(links || {}).map do |key, h|
|
31
|
+
@links[key] = Link.new(h)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def parse_meta!(meta)
|
36
|
+
@meta = meta
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jsonapi-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1.beta1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lucas Hosseini
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: jsonapi-parser
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.1.1.beta2
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.1.1.beta2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.9'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.9'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.4'
|
55
|
+
description: Client library for the JSON API spec.
|
56
|
+
email: lucas.hosseini@gmail.com
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- README.md
|
62
|
+
- lib/jsonapi/client.rb
|
63
|
+
- lib/jsonapi/client/document.rb
|
64
|
+
- lib/jsonapi/client/error.rb
|
65
|
+
- lib/jsonapi/client/link.rb
|
66
|
+
- lib/jsonapi/client/relationship.rb
|
67
|
+
- lib/jsonapi/client/resource.rb
|
68
|
+
homepage: https://github.com/jsonapi-rb/client
|
69
|
+
licenses:
|
70
|
+
- MIT
|
71
|
+
metadata: {}
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">"
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 1.3.1
|
86
|
+
requirements: []
|
87
|
+
rubyforge_project:
|
88
|
+
rubygems_version: 2.5.1
|
89
|
+
signing_key:
|
90
|
+
specification_version: 4
|
91
|
+
summary: Consume JSON API documents.
|
92
|
+
test_files: []
|