ocean-rails 3.1.0 → 3.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ocean-rails.rb +1 -1
- data/lib/ocean/api_remote_resource.rb +176 -0
- data/lib/ocean/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 098dd40c3f091686952bd78105c343ee87535f61
|
4
|
+
data.tar.gz: 9d2cd112b855fd23afd076990794aad0793c4a9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2de952ef104e432eead8554f0f5a7d6a2113c080f1b84b2a0a303937d51bf9dc8ce92574fc5fa53e3cd1bd47eba1e6decb20f2d7e8d9ae10a131741a9b62b128
|
7
|
+
data.tar.gz: 7a13f27912aecad17c15a54e93f300071053f9fcda62fb77b6af64425a48a81182fb5322570d053b26e65a7f4e6d020e364ce314079985a959f0e5d6239d8fc4
|
data/lib/ocean-rails.rb
CHANGED
@@ -0,0 +1,176 @@
|
|
1
|
+
class Api
|
2
|
+
|
3
|
+
#
|
4
|
+
# This class represents an Ocean resource accessed by a URI.
|
5
|
+
#
|
6
|
+
# The resource is read lazily. Retries and back off properties are available.
|
7
|
+
# Conditional GETs are used whenever possible.
|
8
|
+
#
|
9
|
+
# thing = Api::RemoteResource.new("http://api.example.com/things/1")
|
10
|
+
# thing.present? => false
|
11
|
+
# thing['some_attr'] => {"this" => "is", "just" => "an example"}
|
12
|
+
# thing.present? => true
|
13
|
+
#
|
14
|
+
# thing.resource_type => 'thing'
|
15
|
+
# thing.status => 200
|
16
|
+
# thing.success? => true
|
17
|
+
#
|
18
|
+
# The last raw body can be read:
|
19
|
+
#
|
20
|
+
# thing.raw => nil or {"foo" => {...}}
|
21
|
+
#
|
22
|
+
# Headers can be read or set:
|
23
|
+
#
|
24
|
+
# thing.headers => {...}
|
25
|
+
# thing.headers['X-Some-Header'] = "Zalagadoola"
|
26
|
+
#
|
27
|
+
# Attributes can be read or set:
|
28
|
+
#
|
29
|
+
# thing['foo'] = "bar"
|
30
|
+
# thing['foo'] = "newbar"
|
31
|
+
#
|
32
|
+
# Hyperlinks can be read:
|
33
|
+
#
|
34
|
+
# thing.hyperlink['self'] => {"href"=>"http://api.example.com/things/1",
|
35
|
+
# "type"=>"application/json"}
|
36
|
+
#
|
37
|
+
# These are defined for convenience:
|
38
|
+
#
|
39
|
+
# thing.href => "http://api.example.com/things/1"
|
40
|
+
# thing.type => "application/json"
|
41
|
+
#
|
42
|
+
# The following two are equivalent:
|
43
|
+
#
|
44
|
+
# Api::RemoteResource.new("foo").get
|
45
|
+
# Api::RemoteResource.get("foo")
|
46
|
+
#
|
47
|
+
# as are:
|
48
|
+
#
|
49
|
+
# Api::RemoteResource.new("foo").get!
|
50
|
+
# Api::RemoteResource.get!("foo")
|
51
|
+
#
|
52
|
+
# #post, #post!, #put, #put!, #delete, and #delete! are also available.
|
53
|
+
#
|
54
|
+
# If get or get! retrieve an Ocean collection, they will return an array of RemoteResources.
|
55
|
+
#
|
56
|
+
# <tt>thing.get!</tt> returns an Api::RemoteResource if successful. If not, raises an exception.
|
57
|
+
# <tt>thing.get</tt> does the same, but returns nil if unsuccessful.
|
58
|
+
#
|
59
|
+
class RemoteResource
|
60
|
+
|
61
|
+
attr_reader :uri, :content_type, :retries, :backoff_time, :backoff_rate, :backoff_max
|
62
|
+
attr_reader :raw, :resource, :resource_type, :status, :headers
|
63
|
+
|
64
|
+
def initialize(uri, type="application/json",
|
65
|
+
retries: 3, backoff_time: 1, backoff_rate: 2, backoff_max: 30)
|
66
|
+
super()
|
67
|
+
@uri = uri
|
68
|
+
@content_type = type
|
69
|
+
@retries = retries
|
70
|
+
@backoff_time = backoff_time
|
71
|
+
@backoff_rate = backoff_rate
|
72
|
+
@backoff_max = backoff_max
|
73
|
+
@present = false
|
74
|
+
@raw = nil
|
75
|
+
@resource = nil
|
76
|
+
@resource_type = nil
|
77
|
+
@status = nil
|
78
|
+
@headers = nil
|
79
|
+
end
|
80
|
+
|
81
|
+
def present?
|
82
|
+
@present
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
def self.get!(*args)
|
87
|
+
_retrieve new(*args)
|
88
|
+
end
|
89
|
+
|
90
|
+
def get!
|
91
|
+
RemoteResource._retrieve self
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.get(*args)
|
95
|
+
begin
|
96
|
+
get!(*args)
|
97
|
+
rescue
|
98
|
+
nil
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def get
|
103
|
+
begin
|
104
|
+
get!
|
105
|
+
rescue
|
106
|
+
nil
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def [](key)
|
111
|
+
get! unless present?
|
112
|
+
resource[key]
|
113
|
+
end
|
114
|
+
|
115
|
+
def []=(key, value)
|
116
|
+
resource[key] = value
|
117
|
+
end
|
118
|
+
|
119
|
+
def hyperlink
|
120
|
+
self['_links']
|
121
|
+
end
|
122
|
+
|
123
|
+
def href
|
124
|
+
hyperlink['self']['href']
|
125
|
+
end
|
126
|
+
|
127
|
+
def type
|
128
|
+
hyperlink['self']['type']
|
129
|
+
end
|
130
|
+
|
131
|
+
|
132
|
+
class WrongContentType < StandardError; end
|
133
|
+
class UnparseableJson < StandardError; end
|
134
|
+
class JsonIsNoResource < StandardError; end
|
135
|
+
|
136
|
+
|
137
|
+
private
|
138
|
+
|
139
|
+
attr_accessor :present
|
140
|
+
attr_writer :raw, :resource, :resource_type, :status, :headers
|
141
|
+
|
142
|
+
|
143
|
+
def self._retrieve(rr)
|
144
|
+
response = Api.request rr.uri, :get, headers: {}, x_api_token: Api.service_token
|
145
|
+
return unless response.success?
|
146
|
+
raise WrongContentType unless response.headers['Content-Type'] == rr.content_type
|
147
|
+
begin
|
148
|
+
raw = response.body
|
149
|
+
rescue JSON::ParserError
|
150
|
+
raise UnparseableJson
|
151
|
+
end
|
152
|
+
rr.send(:_setup, raw, response)
|
153
|
+
rr
|
154
|
+
end
|
155
|
+
|
156
|
+
|
157
|
+
def _setup(body, response)
|
158
|
+
self.raw = body
|
159
|
+
raise JsonIsNoResource unless raw.is_a? Hash
|
160
|
+
raise JsonIsNoResource unless raw.size == 1
|
161
|
+
resource_type, value = raw.to_a.first
|
162
|
+
raise JsonIsNoResource unless value.is_a? Hash
|
163
|
+
raise JsonIsNoResource unless value['_links'].is_a? Hash
|
164
|
+
raise JsonIsNoResource unless value['_links']['self'].is_a? Hash
|
165
|
+
raise JsonIsNoResource unless value['_links']['self']['href'].is_a? String
|
166
|
+
raise JsonIsNoResource unless value['_links']['self']['type'].is_a? String
|
167
|
+
self.resource = value
|
168
|
+
self.resource_type = resource_type
|
169
|
+
self.status = response.status
|
170
|
+
self.headers = response.headers
|
171
|
+
self.present = true
|
172
|
+
end
|
173
|
+
|
174
|
+
|
175
|
+
end
|
176
|
+
end
|
data/lib/ocean/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ocean-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Bengtson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: typhoeus
|
@@ -250,6 +250,7 @@ files:
|
|
250
250
|
- lib/generators/ocean_setup/templates/spec_helper.rb
|
251
251
|
- lib/ocean-rails.rb
|
252
252
|
- lib/ocean/api.rb
|
253
|
+
- lib/ocean/api_remote_resource.rb
|
253
254
|
- lib/ocean/api_resource.rb
|
254
255
|
- lib/ocean/engine.rb
|
255
256
|
- lib/ocean/flooding.rb
|