restify 0.1.2.1.b27 → 0.1.2.1.b28

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ODExNGE4NTJmNjJmOTk2YTY0YjE4YjViYmI3Mzg5MDdiNzAyYTA1Mg==
4
+ MjFmODExYzA1MWQyMWQyYjQ3NDM0M2I5NWQ3NDRmYTBkYjMzZGRhZA==
5
5
  data.tar.gz: !binary |-
6
- Njg0ZjcxZWI1ZmQ1ODEzNDdiMjYyMjQ1NzdiYjdmMTYyMTM3NGJiNw==
6
+ MWUxMzRiN2RlZmEwZGVmMTBmNzg0ZWMzODFiMjBlOGUwN2Y5YmNkOA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MTA3ZDFkNTAwN2ZmNDExMGQ2YjE0ODZlODhhNTFkZjgxYmEwMmMwNWNiYTE1
10
- OTc2NjY3MjhlMjY1OTkxNzc5YTdiYWU4N2JiMTMzNWUzOTM4NmRmMGY2NTE4
11
- NmUxNTE3NzU2YmMyNDhkYTE5Y2VmNzA1MzcxMTFmNTFjZWY3MzQ=
9
+ NmNiMmJkNTQ4Y2Y4MjBiYzllOTBkNjE1NWExZTE0NDhhNTk3ZmRjYjE0NTky
10
+ N2VlYTkzZjI3NDI5N2I5MDE2M2FlYTNkYWJiYzM2Njc5OTY4YTFjOWJjZDdm
11
+ YmNhMTg2ODU0ZTg1YWNjZWQ2MTI0OGU3ZWRkNTYwNjg0ZmFhODM=
12
12
  data.tar.gz: !binary |-
13
- NTkzYjUwYzQyM2ZjN2YwMjMwYzI5YzkwZTRhYzA0YWM4YTUzOTQ0NjQ1MDRh
14
- ZTMyODEyMzhkNmIxNDQ2YjAwNWY0ZjQyY2FiMGI4ZDRkOTQ0NWM4NWIzMmJi
15
- ZGRjODJhNTY0NjU4N2VlNDdhYTY2MGNhZGI4YWZjZDlmMzkwM2Q=
13
+ Yzc2ZjVhY2Q1NzYyOGIwNmRmYzMwOTMxZDYwOWJlNDYzMjExODc1MTViNjk3
14
+ MDBiMGI2MWUxNzc1OGU4OWI1ZjJmMzBiMjI4NzdlNzhkMjZmZDYyZWZjZDY4
15
+ MmUwYTcyM2Y0NzdhNGI5M2VhZTZjNDVlZTJjZjI3YmViZDA0ZDE=
@@ -90,9 +90,9 @@ module Restify
90
90
 
91
91
  def handle_success(response)
92
92
  if response.decoded_body.is_a?(Array)
93
- Collection.create(self, response.decoded_body, response)
93
+ Collection.new(self, response.decoded_body, response)
94
94
  else
95
- Resource.create(self, response.decoded_body, response)
95
+ Resource.new(self, response.decoded_body, response)
96
96
  end
97
97
  end
98
98
 
@@ -1,79 +1,23 @@
1
1
  module Restify
2
2
  #
3
- class Collection
3
+ class Collection < Array
4
+ include Result
4
5
  include Relations
5
6
 
6
- # @!method [](index)
7
- # Retrieve value for given index.
8
- #
9
- # @param index [Integer] Index.
10
- # @return [Object] Data value for given index.
11
- #
12
- delegate :[], to: :items
7
+ def initialize(client, data = [], response = nil)
8
+ super data
13
9
 
14
- # @!method size
15
- # Return size of collection. Only includes size of current
16
- # page in paginated resources.
17
- #
18
- # @return [Integer] Number of items in current collection.
19
- #
20
- delegate :size, to: :items
21
-
22
- # @!method first
23
- # Return first item of collection.
24
- #
25
- # @return [Resource, Collection, Object] Return first
26
- #
27
- delegate :first, to: :items
28
-
29
- # @!method each
30
- # Iterate over all items or return enumerator.
31
- #
32
- # @overload each
33
- # Calls block once for each item, passing the item as
34
- # parameters.
35
- #
36
- # @yield [item] Yield for each item.
37
- # @yieldparam item [Resource, Collection, Object] Collection item.
38
- #
39
- # @overload each
40
- # Return enumerator for each item.
41
- #
42
- # @return [Enumerator] Enumerator for each item.
43
- #
44
- delegate :each, to: :items
45
-
46
- # Return parsed resource attributes as hash.
47
- #
48
- # @return [Array] Collection items.
49
- #
50
- def items
51
- @items ||= []
52
- end
53
-
54
- def initialize(client, relations = {}, items = [])
55
- @client = client
56
- @relations = HashWithIndifferentAccess.new relations
57
- @items = Array items
58
- end
59
-
60
- class << self
61
- def create(client, data, response)
62
- data = data.map do |value|
63
- case value
64
- when Hash
65
- Resource.create client, value, nil
66
- when Array
67
- create client, value, nil
68
- else
69
- value
70
- end
10
+ map! do |item|
11
+ case item
12
+ when Hash
13
+ Resource.new client, item
14
+ else
15
+ item
71
16
  end
72
-
73
- relations = response ? response.relations(client) : nil
74
-
75
- new client, relations, data
76
17
  end
18
+
19
+ @client = client
20
+ @relations = response ? response.relations(client) : nil
77
21
  end
78
22
  end
79
23
  end
data/lib/restify/link.rb CHANGED
@@ -13,13 +13,13 @@ module Restify
13
13
 
14
14
  # Link metadata like "rel" if specified.
15
15
  #
16
- # @return [HashWithIndifferentAccess<String, String>] Metadata.
16
+ # @return [Hash<String, String>] Metadata.
17
17
  #
18
18
  attr_reader :metadata
19
19
 
20
20
  def initialize(uri, metadata = {})
21
21
  @uri = uri
22
- @metadata = HashWithIndifferentAccess.new(metadata)
22
+ @metadata = metadata
23
23
  end
24
24
 
25
25
  class << self
@@ -30,13 +30,17 @@ module Restify
30
30
  request :delete, params.merge(data: data)
31
31
  end
32
32
 
33
+ def ==(other)
34
+ super || (other.is_a?(String) && @template.pattern == other)
35
+ end
36
+
33
37
  private
34
38
 
35
39
  attr_reader :client, :template
36
40
 
37
41
  def request(method, opts = {})
38
42
  keys = template.variables - Client::RESERVED_KEYS
39
- params = opts.except!(keys)
43
+ params = Hash[keys.map{|key| [key, opts.delete(:key)]}]
40
44
  uri = template.expand(params)
41
45
 
42
46
  client.request method, uri, opts
@@ -8,7 +8,7 @@ module Restify
8
8
  # @return [Boolean] True if resource has relation, false otherwise.
9
9
  #
10
10
  def rel?(name)
11
- relations.key? name
11
+ relations.key? name.to_s
12
12
  end
13
13
  alias_method :relation?, :rel?
14
14
  alias_method :has_rel?, :rel?
@@ -20,16 +20,16 @@ module Restify
20
20
  # @return [Relation] Relation.
21
21
  #
22
22
  def rel(name)
23
- relations.fetch name
23
+ relations.fetch name.to_s
24
24
  end
25
25
  alias_method :relation, :rel
26
26
 
27
27
  # Hash of all known relations.
28
28
  #
29
- # @return [HashWithIndifferentAccess<String, Relation>] Relations.
29
+ # @return [Hash<String, Relation>] Relations.
30
30
  #
31
31
  def relations
32
- @relations ||= HashWithIndifferentAccess.new
32
+ @relations ||= Hash.new
33
33
  end
34
34
  end
35
35
  end
@@ -1,6 +1,7 @@
1
1
  module Restify
2
2
  #
3
- class Resource
3
+ class Resource < Hashie::Mash
4
+ include Result
4
5
  include Relations
5
6
 
6
7
  # Return content Media-Type.
@@ -9,130 +10,58 @@ module Restify
9
10
  #
10
11
  attr_reader :media_type
11
12
 
12
- # @!method [](key)
13
- # Retrieve value for given key.
14
13
  #
15
- # @param key [String, Symbol] Data key.
16
- # @return [Object] Data value for given key.
17
- #
18
- delegate :[], to: :attributes
14
+ def initialize(client, data = {}, response = nil)
15
+ @client = client
16
+ @response = response
19
17
 
20
- # @!method []=(key, value)
21
- # Set value for given key.
22
- #
23
- # @param key [String, Symbol] Data key.
24
- # @param key [String, Fixnum, Object] Data value.
25
- #
26
- delegate :[]=, to: :attributes
18
+ relations.merge! @response.relations(client) if @response
27
19
 
28
- # @!method key?(name)
29
- # Check if resource has given key.
30
- #
31
- # @param name [String, Symbol] Key name.
32
- # @return [Boolean] True if resource contains key, false otherwise.
33
- #
34
- delegate :key?, :has_key?, to: :attributes
20
+ data.each_pair do |key, value|
21
+ self[key.to_s] = convert_value(value)
35
22
 
36
- # @!method each
37
- # Iterate over keys and values or return enumerator.
38
- #
39
- # @overload each
40
- # Calls block once for each key, passing the key-value pair as
41
- # parameters.
42
- #
43
- # @yield [key, value] Yield for each key-value pair.
44
- # @yieldparam key [String] Attribute key.
45
- # @yieldparam value [Object] Attribute value.
46
- #
47
- # @overload each
48
- # Return enumerator for each key-value pair.
49
- #
50
- # @return [Enumerator] Enumerator for each key-value pair.
51
- #
52
- delegate :each, to: :attributes
23
+ name = case key.to_s.downcase
24
+ when /\A(\w+)_url\z/
25
+ $1
26
+ when 'url'
27
+ 'self'
28
+ else
29
+ next
30
+ end
53
31
 
54
- # Return parsed resource attributes as hash.
55
- #
56
- # @return [HashWithIndifferentAccess] Attribute hash.
57
- #
58
- def attributes
59
- @attributes ||= HashWithIndifferentAccess.new
32
+ unless relations.key?(name) || value.nil? || value.to_s.empty?
33
+ relations[name] = Relation.new(client, value.to_s)
34
+ end
35
+ end
60
36
  end
61
37
 
62
- # @!method status
63
- # Return response status if available.
64
- #
65
- # @return [Symbol] Response status.
66
- # @see Response#status
67
- #
68
- delegate :status, to: :@response, allow_nil: true
69
-
70
- # @!method code
71
- # Return response status code if available.
72
- #
73
- # @return [Fixnum] Response status code.
74
- # @see Response#code
75
- #
76
- delegate :code, to: :@response, allow_nil: true
77
-
78
- # Follow the Location header from the response of
79
- # this resource if available.
80
- #
81
- # @return [Obligation<Resource>] Followed resource.
82
- #
83
- def follow
84
- if @response && @response.headers['LOCATION']
85
- @client.request :get, @response.headers['LOCATION']
86
- else
87
- raise RuntimeError.new 'Nothing to follow.'
38
+ # Compare with other {Resource}s or {Hash}s.
39
+ #
40
+ def ==(other)
41
+ case other
42
+ when Resource
43
+ @data == other.data && relations == other.relations
44
+ when Hash
45
+ @data == other
46
+ else
47
+ super
88
48
  end
89
49
  end
90
50
 
91
- def initialize(client, relations = {}, attributes = {}, response = nil)
92
- @client = client
93
- @response = response
94
- @relations = HashWithIndifferentAccess.new relations
95
- @attributes = HashWithIndifferentAccess.new attributes
51
+ # @private
52
+ #
53
+ def convert_key(key)
54
+ key.to_s
96
55
  end
97
56
 
98
- class << self
99
- #
100
- # Build a resource from given response.
101
- #
102
- def create(client, data, response)
103
- relations = {}
104
- relations = response.relations(client) if response
105
-
106
- hash = {}
107
- if data
108
- data.each do |key, value|
109
- hash[key] = case value
110
- when Array
111
- Collection.create(client, value, nil)
112
- when Hash
113
- Resource.create(client, value, nil)
114
- else
115
- value
116
- end
117
- end
118
-
119
- data.keys.each do |key|
120
- name = nil
121
- if (m = /\A(\w+)_url\z/.match(key))
122
- name = m[1]
123
- elsif key == 'url'
124
- name = :self
125
- else
126
- next
127
- end
128
-
129
- unless relations.key?(name) || data[key].to_s.blank?
130
- relations[name] = Relation.new(client, data[key].to_s)
131
- end
132
- end
133
- end
134
-
135
- new client, relations, hash, response
57
+ # @private
58
+ #
59
+ def convert_value(value)
60
+ case value
61
+ when Hash
62
+ self.new client, value
63
+ else
64
+ value
136
65
  end
137
66
  end
138
67
  end
@@ -0,0 +1,34 @@
1
+ module Restify
2
+ module Result
3
+ # Return response status if available.
4
+ #
5
+ # @return [Symbol] Response status.
6
+ # @see Response#status
7
+ #
8
+ def status
9
+ @response ? @response.status : nil
10
+ end
11
+
12
+ # Return response status code if available.
13
+ #
14
+ # @return [Fixnum] Response status code.
15
+ # @see Response#code
16
+ #
17
+ def code
18
+ @response ? @response.code : nil
19
+ end
20
+
21
+ # Follow the Location header from the response of
22
+ # this resource if available.
23
+ #
24
+ # @return [Obligation<Resource>] Followed resource.
25
+ #
26
+ def follow
27
+ if @response && @response.headers['LOCATION']
28
+ @client.request :get, @response.headers['LOCATION']
29
+ else
30
+ raise RuntimeError.new 'Nothing to follow.'
31
+ end
32
+ end
33
+ end
34
+ end
data/lib/restify.rb CHANGED
@@ -1,17 +1,15 @@
1
1
  require 'restify/version'
2
2
  require 'addressable/uri'
3
3
  require 'addressable/template'
4
- require 'active_support'
5
- require 'active_support/core_ext'
6
- require 'active_support/hash_with_indifferent_access'
7
- require 'active_support/core_ext/module/delegation'
8
4
  require 'multi_json'
9
5
  require 'obligation'
6
+ require 'hashie'
10
7
 
11
8
  #
12
9
  module Restify
13
10
  require 'restify/adapter'
14
11
  require 'restify/client'
12
+ require 'restify/result'
15
13
  require 'restify/relations'
16
14
  require 'restify/collection'
17
15
  require 'restify/link'
data/restify.gemspec CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.add_runtime_dependency 'obligation', '~> 0.1'
22
22
  spec.add_runtime_dependency 'addressable', '~> 2.3'
23
23
  spec.add_runtime_dependency 'em-http-request', '~> 1.1'
24
- spec.add_runtime_dependency 'activesupport', '>= 3.2', '< 5'
24
+ spec.add_runtime_dependency 'hashie', '~> 3.3'
25
25
  spec.add_runtime_dependency 'multi_json'
26
26
  spec.add_runtime_dependency 'rack'
27
27
 
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe Restify::Collection do
4
+
5
+ end
@@ -2,14 +2,15 @@ require 'spec_helper'
2
2
 
3
3
  describe Restify::Resource do
4
4
  let(:client) { double 'client' }
5
- let(:relations) { {} }
6
- let(:attributes) { {} }
7
- let(:res) { described_class.new(client, relations, attributes) }
5
+ let(:data) { {} }
6
+ let(:res) { described_class.new(client, data) }
8
7
 
9
8
  describe '#rel?' do
10
- before do
11
- res.relations['users'] = true
12
- res.relations[:projects] = true
9
+ let(:data) do
10
+ {
11
+ 'users_url' => 'http://example.org/users',
12
+ 'projects_url' => 'http://example.org/projects',
13
+ }
13
14
  end
14
15
 
15
16
  it 'should match relations' do
@@ -34,30 +35,30 @@ describe Restify::Resource do
34
35
  end
35
36
 
36
37
  describe '#rel' do
37
- let(:users) { double 'users rel' }
38
- let(:projects) { double 'projects rel' }
39
- before do
40
- res.relations['users'] = users
41
- res.relations[:projects] = projects
38
+ let(:data) do
39
+ {
40
+ 'users_url' => 'http://example.org/users',
41
+ 'projects_url' => 'http://example.org/projects',
42
+ }
42
43
  end
43
44
 
44
45
  it 'should return relation' do
45
- expect(res.rel(:users)).to eq users
46
- expect(res.rel('users')).to eq users
47
- expect(res.rel(:projects)).to eq projects
48
- expect(res.rel('projects')).to eq projects
46
+ expect(res.rel(:users)).to eq 'http://example.org/users'
47
+ expect(res.rel('users')).to eq 'http://example.org/users'
48
+ expect(res.rel(:projects)).to eq 'http://example.org/projects'
49
+ expect(res.rel('projects')).to eq 'http://example.org/projects'
49
50
  expect { res.rel(:fuu) }.to raise_error KeyError
50
51
 
51
- expect(res.relation(:users)).to eq users
52
- expect(res.relation('users')).to eq users
53
- expect(res.relation(:projects)).to eq projects
54
- expect(res.relation('projects')).to eq projects
52
+ expect(res.relation(:users)).to eq 'http://example.org/users'
53
+ expect(res.relation('users')).to eq 'http://example.org/users'
54
+ expect(res.relation(:projects)).to eq 'http://example.org/projects'
55
+ expect(res.relation('projects')).to eq 'http://example.org/projects'
55
56
  expect { res.relation(:fuu) }.to raise_error KeyError
56
57
  end
57
58
  end
58
59
 
59
60
  describe '#key?' do
60
- let(:attributes) { {a: 0, 'b' => 1, 0 => 2} }
61
+ let(:data) { {a: 0, 'b' => 1, 0 => 2} }
61
62
 
62
63
  it 'should test for key inclusion' do
63
64
  expect(res.key?(:a)).to eq true
@@ -81,7 +82,7 @@ describe Restify::Resource do
81
82
  end
82
83
 
83
84
  describe '#each' do
84
- let(:attributes) { {a: 0, b: 1} }
85
+ let(:data) { {a: 0, b: 1} }
85
86
 
86
87
  it 'should yield' do
87
88
  expect{|cb| res.each(&cb) }.to yield_control.twice
@@ -95,18 +96,30 @@ describe Restify::Resource do
95
96
  end
96
97
 
97
98
  describe '#[]' do
98
- let(:attributes) { {a: 0, b: 1} }
99
+ let(:data) { {a: 0, b: 1} }
99
100
 
100
- it 'should return attributes' do
101
+ it 'should return data' do
101
102
  expect(res[:a]).to eq 0
102
103
  expect(res[:b]).to eq 1
103
104
  end
104
105
  end
105
106
 
107
+ describe '<getter>' do
108
+ let(:data) { {a: 0, b: 1} }
109
+
110
+ it 'should return data' do
111
+ expect(res).to respond_to :a
112
+ expect(res).to respond_to :b
113
+
114
+ expect(res.a).to eq 0
115
+ expect(res.b).to eq 1
116
+ end
117
+ end
118
+
106
119
  describe '#[]=' do
107
- let(:attributes) { {a: 0, b: 1} }
120
+ let(:data) { {a: 0, b: 1} }
108
121
 
109
- it 'should return attributes' do
122
+ it 'should return data' do
110
123
  res[:a] = 5
111
124
  res[:c] = 15
112
125
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2.1.b27
4
+ version: 0.1.2.1.b28
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Graichen
@@ -53,25 +53,19 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.1'
55
55
  - !ruby/object:Gem::Dependency
56
- name: activesupport
56
+ name: hashie
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: '3.2'
62
- - - <
59
+ - - ~>
63
60
  - !ruby/object:Gem::Version
64
- version: '5'
61
+ version: '3.3'
65
62
  type: :runtime
66
63
  prerelease: false
67
64
  version_requirements: !ruby/object:Gem::Requirement
68
65
  requirements:
69
- - - ! '>='
70
- - !ruby/object:Gem::Version
71
- version: '3.2'
72
- - - <
66
+ - - ~>
73
67
  - !ruby/object:Gem::Version
74
- version: '5'
68
+ version: '3.3'
75
69
  - !ruby/object:Gem::Dependency
76
70
  name: multi_json
77
71
  requirement: !ruby/object:Gem::Requirement
@@ -135,8 +129,10 @@ files:
135
129
  - lib/restify/request.rb
136
130
  - lib/restify/resource.rb
137
131
  - lib/restify/response.rb
132
+ - lib/restify/result.rb
138
133
  - lib/restify/version.rb
139
134
  - restify.gemspec
135
+ - spec/restify/collection_spec.rb
140
136
  - spec/restify/link_spec.rb
141
137
  - spec/restify/resource_spec.rb
142
138
  - spec/restify_spec.rb
@@ -166,6 +162,7 @@ signing_key:
166
162
  specification_version: 4
167
163
  summary: An experimental hypermedia REST client.
168
164
  test_files:
165
+ - spec/restify/collection_spec.rb
169
166
  - spec/restify/link_spec.rb
170
167
  - spec/restify/resource_spec.rb
171
168
  - spec/restify_spec.rb