json_roa-client 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c4d7651511267f0efcb676968189e34177b9f6d2
4
+ data.tar.gz: d4e9af5a21128239ab328f7e067c2e4d95c069d6
5
+ SHA512:
6
+ metadata.gz: fea1c51ea58d00a4d1f463c8b27305ebcebce2dd3ce5b4b50a67b17c69e48f43d0eedae98d384882fb2d1fc822c78d5926049f6d354feeb895a21a8e78c8b20c
7
+ data.tar.gz: ac5ec164e6903eef845ea64a42b454ea775deaf94a0cd2da8fb0b35017e05d9d83d63d8abad2d2627c077210b4aaea1da607687db5df2cf1d5e121edb72b28bc
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in json_roa-client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Thomas Schank
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # Ruby JSON-ROA Client
2
+
3
+ A ruby client for `JSON_ROA`.
4
+
5
+ ## Usage
6
+
7
+ ### Connecting
8
+
9
+ ```ruby
10
+ require 'json_roa/client'
11
+
12
+ @root_resource = JSON_ROA::Client.connect @base_url do |conn|
13
+ conn.basic_auth(@username,@password)
14
+ end
15
+ ```
16
+
17
+ `conn` is a [Faraday][] connection.
18
+
19
+
20
+ ### Resources
21
+
22
+ #### Data
23
+
24
+ ```ruby
25
+ @root_resource.data
26
+ ```
27
+
28
+ #### JSON-ROA Data
29
+
30
+ ```ruby
31
+ @root_resource.json_roa_data
32
+ ```
33
+
34
+ ### Relations
35
+
36
+ ```ruby
37
+ @root_resource.relation('execution')
38
+ ```
39
+
40
+ #### Resource of a Relation
41
+
42
+ In this case the relation is based on an [URI Template][] and we
43
+ must provide an `id` parameter.
44
+
45
+ ```ruby
46
+ @execution_resource= @root_resource.relation('execution').get('id' => '55744c40-b764-4fd4-98e2-7a69bc57f496')
47
+ ```
48
+
49
+ ### Collections
50
+
51
+ A resource that is conceptual an *index* can be processed as an
52
+ [Enumerable][] of its relations. The client will abstract pagination
53
+ away.
54
+
55
+ ```ruby
56
+ @root_resource.relation('executions').get.collection.each do |execution_relation|
57
+ puts execution_relation.get.data
58
+ end
59
+ ```
60
+
61
+ [Enumerable]: http://ruby-doc.org/core-2.1.0/Enumerable.html
62
+ [URI Template]: http://tools.ietf.org/html/rfc6570
63
+ [Faraday]: https://github.com/lostisland/faraday
64
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'json_roa/client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "json_roa-client"
8
+ spec.version = JSON_ROA::Client::VERSION
9
+ spec.authors = ["Thomas Schank"]
10
+ spec.email = ["DrTom@schank.ch"]
11
+ spec.summary = "The Ruby JSON-ROA Client Reference Implementation"
12
+ spec.description = ""
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "pry", "~> 0.10"
24
+
25
+ spec.add_dependency 'addressable', '~> 2'
26
+ spec.add_dependency 'faraday', '~> 0.9'
27
+ spec.add_dependency 'faraday_middleware', '~> 0.9'
28
+ # spec.add_dependency "pry", "~> 0.10"
29
+
30
+ end
@@ -0,0 +1,29 @@
1
+ module JSON_ROA
2
+ module Client
3
+
4
+ class Collection
5
+ include Enumerable
6
+
7
+ def initialize conn, resource
8
+ @conn= conn
9
+ @resource= resource
10
+ end
11
+
12
+ def each &block
13
+ relations= @resource.response.env \
14
+ .json_roa_data['collection']['relations'] rescue []
15
+ relations.each do |key, data|
16
+ yield Relation.new(@conn,key,data) if block_given?
17
+ end
18
+
19
+ next_val= @resource.response \
20
+ .env.json_roa_data['collection']['next'] rescue nil
21
+ if next_val
22
+ Relation.new(@conn,"next",next_val).get().collection.each(&block)
23
+ end
24
+
25
+ end
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,34 @@
1
+ require 'addressable/template'
2
+ require 'addressable/uri'
3
+
4
+ module JSON_ROA
5
+ module Client
6
+
7
+ class Relation
8
+
9
+ attr_reader :data
10
+ attr_reader :key
11
+
12
+ def initialize conn, key, data
13
+ @conn= conn
14
+ @key= key
15
+ @data= data
16
+ end
17
+
18
+ def get query_parameters= {}
19
+ href= @data['href']
20
+ template= ::Addressable::Template.new(href)
21
+ expanded= template.expand(query_parameters)
22
+ response=@conn.get(expanded)
23
+ ::JSON_ROA::Client::Resource.new(@conn,response)
24
+ end
25
+
26
+ def methods
27
+ @data['methods']
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+ end
34
+
@@ -0,0 +1,40 @@
1
+ require "json_roa/client/relation"
2
+ require "json_roa/client/collection"
3
+
4
+ module JSON_ROA
5
+ module Client
6
+
7
+ class Resource
8
+
9
+ attr_reader :response
10
+
11
+ def initialize conn, response = nil
12
+ @conn= conn
13
+ @response= response || conn.get
14
+ end
15
+
16
+ def relation key
17
+ relhash= @response.env.json_roa_data['relations'][key]
18
+ ::JSON_ROA::Client::Relation.new @conn, key, relhash
19
+ end
20
+
21
+ def relations_data
22
+ @response.env.json_roa_data['relations']
23
+ end
24
+
25
+ def data
26
+ @response.body
27
+ end
28
+
29
+ def json_roa_data
30
+ @response.env.json_roa_data
31
+ end
32
+
33
+ def collection
34
+ ::JSON_ROA::Client::Collection.new @conn, self
35
+ end
36
+ end
37
+
38
+ end
39
+ end
40
+
@@ -0,0 +1,5 @@
1
+ module JSON_ROA
2
+ module Client
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,43 @@
1
+ require "json_roa/client/version"
2
+ require "json_roa/client/resource"
3
+ require 'faraday'
4
+ require 'faraday_middleware'
5
+
6
+ module JSON_ROA
7
+
8
+ class Middleware < Faraday::Middleware
9
+ def call(env)
10
+ @app.call(env).on_complete do |env|
11
+ env.class.class_eval do |klass|
12
+ attr_accessor :json_roa_data
13
+ end
14
+ env.json_roa_data= env.body.delete("_json-roa")
15
+ end
16
+ end
17
+ end
18
+
19
+ module Client
20
+
21
+ class << self
22
+
23
+ def connect url, &block
24
+
25
+ @conn= Faraday.new(url: url,
26
+ headers: {accept: "application/json-roa+json"}) do |conn|
27
+ conn.use ::JSON_ROA::Middleware
28
+ conn.response :json, :content_type => /\bjson$/
29
+ conn.request :retry
30
+ conn.use Faraday::Response::RaiseError
31
+ conn.adapter Faraday.default_adapter
32
+ end
33
+
34
+ yield @conn if block_given?
35
+
36
+ Resource.new @conn
37
+
38
+ end
39
+
40
+ end
41
+ end
42
+
43
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_roa-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Schank
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-19 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
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: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: addressable
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.9'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.9'
83
+ - !ruby/object:Gem::Dependency
84
+ name: faraday_middleware
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.9'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.9'
97
+ description: ''
98
+ email:
99
+ - DrTom@schank.ch
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - json_roa-client.gemspec
110
+ - lib/json_roa/client.rb
111
+ - lib/json_roa/client/collection.rb
112
+ - lib/json_roa/client/relation.rb
113
+ - lib/json_roa/client/resource.rb
114
+ - lib/json_roa/client/version.rb
115
+ homepage: ''
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.2.2
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: The Ruby JSON-ROA Client Reference Implementation
139
+ test_files: []