json_roa-client 1.0.0.pre.beta.0 → 1.0.0.pre.beta.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/README.md +7 -1
- data/json_roa-client.gemspec +3 -2
- data/lib/json_roa/client/relation.rb +17 -12
- data/lib/json_roa/client/resource.rb +6 -1
- data/lib/json_roa/client/version.rb +1 -1
- data/spec/api_query_spec.rb +46 -0
- data/spec/collection_spec.rb +49 -0
- data/spec/connect_spec.rb +21 -0
- data/spec/data/root.yml +12 -0
- data/spec/data/task1.yml +13 -0
- data/spec/data/tasks_page0.yml +11 -0
- data/spec/data/tasks_page1.yml +7 -0
- data/spec/methods_spec.rb +30 -0
- data/spec/spec_helper.rb +65 -0
- data/spec/template_expansion_spec.rb +31 -0
- metadata +52 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a343d233e0c8c79b865158eefdfa6294c079ba80
|
4
|
+
data.tar.gz: d3c1da41839add17ac785165b4e3e8696d1352b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f1e660f2cbe03948725619ebee7f7d53467207f64119e693ea60c17f09197d21c9c573d46849dbef057c46ea7ec4e9cf924da18e4d3ad82121441cd858773894
|
7
|
+
data.tar.gz: cdae12606a74ce5a99b3ee3928cab9877500209292f719208db32090f0c22df9d9d49cc09ccc6c69e603a7414aaac5d1db2842d44a09028dc041ab4ff0b98fe5
|
data/.rspec
ADDED
data/README.md
CHANGED
@@ -9,7 +9,7 @@ A ruby client for `JSON_ROA`.
|
|
9
9
|
```ruby
|
10
10
|
require 'json_roa/client'
|
11
11
|
|
12
|
-
@
|
12
|
+
@root_relation= JSON_ROA::Client.connect @base_url do |conn|
|
13
13
|
conn.basic_auth(@username,@password)
|
14
14
|
end
|
15
15
|
```
|
@@ -19,6 +19,12 @@ end
|
|
19
19
|
|
20
20
|
### Resources
|
21
21
|
|
22
|
+
#### Retrieving the Resource of the Root Relation
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
@root_resource= @root_relation.get()
|
26
|
+
```
|
27
|
+
|
22
28
|
#### Data
|
23
29
|
|
24
30
|
```ruby
|
data/json_roa-client.gemspec
CHANGED
@@ -20,11 +20,12 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.6"
|
22
22
|
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
-
spec.add_development_dependency "pry",
|
23
|
+
spec.add_development_dependency "pry", '~> 0.10'
|
24
|
+
spec.add_development_dependency 'simplecov', '~> 0.9'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 3'
|
24
26
|
|
25
27
|
spec.add_dependency 'addressable', '~> 2'
|
26
28
|
spec.add_dependency 'faraday', '~> 0.9'
|
27
29
|
spec.add_dependency 'faraday_middleware', '~> 0.9'
|
28
|
-
# spec.add_dependency "pry", "~> 0.10"
|
29
30
|
|
30
31
|
end
|
@@ -3,11 +3,8 @@ require 'addressable/uri'
|
|
3
3
|
|
4
4
|
module JSON_ROA
|
5
5
|
module Client
|
6
|
-
|
7
6
|
class Relation
|
8
7
|
|
9
|
-
SUPPORTED_METHODS= %w(get put post delete)
|
10
|
-
|
11
8
|
attr_reader :data
|
12
9
|
attr_reader :key
|
13
10
|
|
@@ -15,27 +12,35 @@ module JSON_ROA
|
|
15
12
|
@conn= conn
|
16
13
|
@key= key
|
17
14
|
@data= data
|
15
|
+
|
16
|
+
Faraday::Connection::METHODS.intersection(available_methods) \
|
17
|
+
.each do |method_name|
|
18
|
+
define_http_method method_name
|
19
|
+
end
|
18
20
|
end
|
19
21
|
|
20
|
-
def
|
21
|
-
|
22
|
-
|
22
|
+
def define_http_method method_name
|
23
|
+
define_singleton_method method_name \
|
24
|
+
do |query_parameters={}, body=nil, headers=nil, &block|
|
25
|
+
run_request method_name, query_parameters, body, headers, &block
|
23
26
|
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def run_request method, query_parameters, body, headers, &block
|
24
30
|
href= @data['href']
|
25
31
|
template= ::Addressable::Template.new(href)
|
26
32
|
expanded_url= template.expand(query_parameters)
|
27
|
-
response=@conn.run_request
|
33
|
+
response=@conn.run_request( \
|
34
|
+
method.to_sym, expanded_url, body, headers, &block)
|
28
35
|
::JSON_ROA::Client::Resource.new(@conn,response)
|
29
36
|
end
|
30
37
|
|
31
|
-
|
32
|
-
|
33
|
-
run_request method_name, query_parameters, body, headers, &block
|
34
|
-
end
|
38
|
+
def available_methods_data
|
39
|
+
@data['methods'] ||= {"get" => {}}
|
35
40
|
end
|
36
41
|
|
37
42
|
def available_methods
|
38
|
-
|
43
|
+
available_methods_data.keys.map(&:to_sym)
|
39
44
|
end
|
40
45
|
|
41
46
|
end
|
@@ -14,10 +14,15 @@ module JSON_ROA
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def relation key
|
17
|
-
relhash=
|
17
|
+
relhash= json_roa_data['relations'][key]
|
18
18
|
::JSON_ROA::Client::Relation.new @conn, key, relhash
|
19
19
|
end
|
20
20
|
|
21
|
+
def self_relation
|
22
|
+
::JSON_ROA::Client::Relation.new( \
|
23
|
+
@conn, "self", json_roa_data['self_relation']) rescue nil
|
24
|
+
end
|
25
|
+
|
21
26
|
def data
|
22
27
|
@response.body
|
23
28
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require_relative 'spec_helper.rb'
|
2
|
+
|
3
|
+
|
4
|
+
describe "API: traversing from root to resource to some relation" do
|
5
|
+
|
6
|
+
describe "root relation" do
|
7
|
+
|
8
|
+
let :root_relation do
|
9
|
+
JSON_ROA::Client.connect("/")
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "root_resource via get" do
|
13
|
+
|
14
|
+
subject(:root_resource) {root_relation.get() }
|
15
|
+
|
16
|
+
it do
|
17
|
+
expect(root_resource.class).to be== JSON_ROA::Client::Resource
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "data" do
|
21
|
+
subject(:data) {root_resource.data}
|
22
|
+
|
23
|
+
it do
|
24
|
+
expect(data).to be== {"x" => 42}
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "task relation " do
|
30
|
+
subject(:task_relation) { root_resource.relation('task')}
|
31
|
+
|
32
|
+
it "is of class Relation" do
|
33
|
+
expect(task_relation.class).to be== JSON_ROA::Client::Relation
|
34
|
+
end
|
35
|
+
|
36
|
+
it "responds to get" do
|
37
|
+
expect(task_relation.respond_to? :get).to be
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require_relative 'spec_helper.rb'
|
2
|
+
|
3
|
+
describe JSON_ROA::Client::Collection do
|
4
|
+
|
5
|
+
let :root_relation do
|
6
|
+
JSON_ROA::Client.connect("/")
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "tasks resource via get" do
|
10
|
+
subject(:tasks_resource) {root_relation.get().relation('tasks').get() }
|
11
|
+
|
12
|
+
it "is a resource" do
|
13
|
+
expect(tasks_resource.class).to be== JSON_ROA::Client::Resource
|
14
|
+
end
|
15
|
+
|
16
|
+
it "responds to collection" do
|
17
|
+
expect(tasks_resource.respond_to? :collection).to be
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "tasks collection" do
|
21
|
+
subject(:tasks_collection) {tasks_resource.collection}
|
22
|
+
|
23
|
+
it "is a collection" do
|
24
|
+
expect(tasks_collection.class).to be== JSON_ROA::Client::Collection
|
25
|
+
end
|
26
|
+
|
27
|
+
it "has exactly one element" do
|
28
|
+
expect(tasks_collection.count).to be== 1
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "the first task" do
|
32
|
+
subject(:first_task) {tasks_collection.first}
|
33
|
+
|
34
|
+
it "is a relation" do
|
35
|
+
expect(first_task.class).to be== JSON_ROA::Client::Relation
|
36
|
+
end
|
37
|
+
|
38
|
+
it "leads to /task/t1" do
|
39
|
+
expect(first_task.data["href"]).to be== "/tasks/t1"
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'json_roa/client'
|
3
|
+
require 'pry'
|
4
|
+
|
5
|
+
describe JSON_ROA::Client do
|
6
|
+
context "when calling connect" do
|
7
|
+
|
8
|
+
it "returns an instance of JSON_ROA::Client::Relation" do
|
9
|
+
expect(JSON_ROA::Client.connect("http://example.com").class).to \
|
10
|
+
be== JSON_ROA::Client::Relation
|
11
|
+
end
|
12
|
+
|
13
|
+
it "exposes Faraday::Connection via the block" do
|
14
|
+
JSON_ROA::Client.connect("http://example.com") do |conn|
|
15
|
+
expect(conn.class).to be== Faraday::Connection
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
data/spec/data/root.yml
ADDED
data/spec/data/task1.yml
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require_relative 'spec_helper.rb'
|
2
|
+
|
3
|
+
describe "methods of the relation /tasks/t1" do
|
4
|
+
|
5
|
+
let :root_relation do
|
6
|
+
JSON_ROA::Client.connect("/")
|
7
|
+
end
|
8
|
+
|
9
|
+
subject(:task1_self_relation) \
|
10
|
+
{root_relation.get().relation("task").get(id: "t1").self_relation}
|
11
|
+
|
12
|
+
it "is a relation" do
|
13
|
+
expect(task1_self_relation.class).to be== JSON_ROA::Client::Relation
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "defined get" do
|
17
|
+
it "does not raise" do
|
18
|
+
expect{task1_self_relation.get()}.not_to raise_error
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "not defined post" do
|
23
|
+
it "does raise" do
|
24
|
+
expect{task1_self_relation.post()}.to raise_error
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start
|
3
|
+
|
4
|
+
require 'faraday'
|
5
|
+
require 'rspec'
|
6
|
+
require 'pry'
|
7
|
+
require 'YAML'
|
8
|
+
require 'json_roa/client'
|
9
|
+
|
10
|
+
module JSON_ROA
|
11
|
+
module Client
|
12
|
+
|
13
|
+
class Relation
|
14
|
+
attr_accessor :conn
|
15
|
+
end
|
16
|
+
|
17
|
+
class << self
|
18
|
+
alias_method :original_connect, :connect
|
19
|
+
|
20
|
+
def connect url, &block
|
21
|
+
res= original_connect url, &block
|
22
|
+
res.conn= test_api
|
23
|
+
res
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
def root_data
|
32
|
+
YAML.load_file(File.expand_path("./data/root.yml",
|
33
|
+
File.dirname(__FILE__)))
|
34
|
+
end
|
35
|
+
|
36
|
+
def tasks_page0_data
|
37
|
+
YAML.load_file( File.expand_path("./data/tasks_page0.yml",
|
38
|
+
File.dirname(__FILE__)))
|
39
|
+
end
|
40
|
+
|
41
|
+
def tasks_page1_data
|
42
|
+
YAML.load_file( File.expand_path("./data/tasks_page1.yml",
|
43
|
+
File.dirname(__FILE__)))
|
44
|
+
end
|
45
|
+
|
46
|
+
def task1_data
|
47
|
+
YAML.load_file( File.expand_path("./data/task1.yml",
|
48
|
+
File.dirname(__FILE__)))
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
def test_api
|
53
|
+
Faraday.new do |conn|
|
54
|
+
headers= {content_type: "application/json-roa+json"}
|
55
|
+
conn.use ::JSON_ROA::Middleware
|
56
|
+
conn.response :json, :content_type => /\bjson$/
|
57
|
+
conn.adapter :test do |stub|
|
58
|
+
stub.get('/') { |env| [ 200, headers, root_data.to_json]}
|
59
|
+
stub.get('/tasks/') { |env| [ 200, headers, tasks_page0_data.to_json]}
|
60
|
+
stub.get('/tasks/?page=0') { |env| [ 200, headers, tasks_page0_data.to_json]}
|
61
|
+
stub.get('/tasks/?page=1') { |env| [ 200, headers, tasks_page1_data.to_json]}
|
62
|
+
stub.get('/tasks/t1') { |env| [ 200, headers, task1_data.to_json]}
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative 'spec_helper.rb'
|
2
|
+
|
3
|
+
describe "template expansion of task with id" do
|
4
|
+
|
5
|
+
let :root_relation do
|
6
|
+
JSON_ROA::Client.connect("/")
|
7
|
+
end
|
8
|
+
|
9
|
+
subject(:task_relation) {root_relation.get().relation("task")}
|
10
|
+
|
11
|
+
it "is a relation" do
|
12
|
+
expect(task_relation.class).to be== JSON_ROA::Client::Relation
|
13
|
+
end
|
14
|
+
|
15
|
+
it "is templated" do
|
16
|
+
expect(task_relation.data['href']).to be== "/tasks/{id}"
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "get with parameter to expand" do
|
20
|
+
subject(:task_resource){task_relation.get(id: "t1")}
|
21
|
+
|
22
|
+
it "is a resource of t1" do
|
23
|
+
expect(task_resource.class).to be== JSON_ROA::Client::Resource
|
24
|
+
expect(task_resource.self_relation.data["href"]).to be== "/tasks/t1"
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_roa-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.pre.beta.
|
4
|
+
version: 1.0.0.pre.beta.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thomas Schank
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,34 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0.10'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.9'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.9'
|
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'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3'
|
55
83
|
- !ruby/object:Gem::Dependency
|
56
84
|
name: addressable
|
57
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -102,6 +130,7 @@ extensions: []
|
|
102
130
|
extra_rdoc_files: []
|
103
131
|
files:
|
104
132
|
- ".gitignore"
|
133
|
+
- ".rspec"
|
105
134
|
- Gemfile
|
106
135
|
- LICENSE.txt
|
107
136
|
- README.md
|
@@ -112,6 +141,16 @@ files:
|
|
112
141
|
- lib/json_roa/client/relation.rb
|
113
142
|
- lib/json_roa/client/resource.rb
|
114
143
|
- lib/json_roa/client/version.rb
|
144
|
+
- spec/api_query_spec.rb
|
145
|
+
- spec/collection_spec.rb
|
146
|
+
- spec/connect_spec.rb
|
147
|
+
- spec/data/root.yml
|
148
|
+
- spec/data/task1.yml
|
149
|
+
- spec/data/tasks_page0.yml
|
150
|
+
- spec/data/tasks_page1.yml
|
151
|
+
- spec/methods_spec.rb
|
152
|
+
- spec/spec_helper.rb
|
153
|
+
- spec/template_expansion_spec.rb
|
115
154
|
homepage: ''
|
116
155
|
licenses:
|
117
156
|
- MIT
|
@@ -136,4 +175,14 @@ rubygems_version: 2.2.2
|
|
136
175
|
signing_key:
|
137
176
|
specification_version: 4
|
138
177
|
summary: The Ruby JSON-ROA Client Reference Implementation
|
139
|
-
test_files:
|
178
|
+
test_files:
|
179
|
+
- spec/api_query_spec.rb
|
180
|
+
- spec/collection_spec.rb
|
181
|
+
- spec/connect_spec.rb
|
182
|
+
- spec/data/root.yml
|
183
|
+
- spec/data/task1.yml
|
184
|
+
- spec/data/tasks_page0.yml
|
185
|
+
- spec/data/tasks_page1.yml
|
186
|
+
- spec/methods_spec.rb
|
187
|
+
- spec/spec_helper.rb
|
188
|
+
- spec/template_expansion_spec.rb
|