cdnconnect-api 0.1.2 → 0.2.0

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.
@@ -12,118 +12,250 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+ require 'json'
16
+
15
17
  module CDNConnect
16
18
 
17
19
  ##
18
- # Used to simpilfy things by creating easy to use helper functions to read
19
- # response data. Responses from the API are all structured the same way,
20
- # and this class is used as a small wrapper to make it easier to get data.
20
+ # Used to simpilfy things with helper functions to read response
21
+ # data. Responses from the API are all structured the same way, and
22
+ # this class is used as a wrapper to make it easier to read
23
+ # resposne data.
21
24
  class APIResponse
22
25
 
23
- def initialize(response)
24
- @response = response
25
- @data = nil
26
+ def initialize(http_response=nil)
27
+ @http_response = http_response
28
+ if http_response != nil
29
+ @status = http_response.status
30
+ @data = nil
31
+ else
32
+ @status = 0
33
+ @data = {"results" => {}, "msgs" => []}
34
+ end
26
35
  return self
27
36
  end
28
37
 
29
- def body()
30
- @response.body
38
+
39
+ ##
40
+ # A list of all the files that were uploaded. Each file in the array is a hash.
41
+ #
42
+ # @return [array] An array of hashes each representing a CDN Connect object.
43
+ def files
44
+ return get_result('files')
45
+ end
46
+
47
+
48
+ ##
49
+ # Can be either a file or folder object, or the first file in the files array.
50
+ #
51
+ # @return [hash] A hash representing a CDN Connect object.
52
+ def object
53
+ obj = get_result('object')
54
+ if obj != nil
55
+ return obj
56
+ end
57
+ fs = files
58
+ if fs != nil and fs.length > 0
59
+ return fs[0]
60
+ end
61
+ nil
31
62
  end
63
+
32
64
 
33
- def data()
34
- # Decode the response body from JSON into an object.
35
- # Keep the parsed data in an instance variable so we
36
- # don't keep parsing it every time we reference data()
37
- if @data == nil
38
- if format.include? 'application/json'
39
- @data = ActiveSupport::JSON.decode(body)
40
- else
41
- raise "Data response type must be JSON"
65
+ ##
66
+ # Content body of the http response. This will be read by the
67
+ # data method so the body can be parsed into a hash.
68
+ #
69
+ # @return [String]
70
+ def body
71
+ if @http_response != nil
72
+ return @http_response.body
73
+ end
74
+ nil
75
+ end
76
+
77
+
78
+ ##
79
+ # Decode the response body from JSON into a hash.
80
+ # Store the parsed data in an instance variable (@data) so we
81
+ # don't keep parsing it every time we reference data.
82
+ #
83
+ # @return [Hash]
84
+ def data
85
+ if @data == nil and body != nil
86
+ begin
87
+ @data = JSON.parse(body)
88
+ rescue
42
89
  end
43
90
  end
44
91
  @data
45
92
  end
46
93
 
47
- def results()
48
- # this is just double checking the results data
49
- # exists and is built with the correct structure
94
+
95
+ ##
96
+ # Helper method to read the results hash.
97
+ #
98
+ # @return [Hash]
99
+ def results
50
100
  d = data
51
- if d.has_key?('results')
101
+ if d != nil and d.has_key?('results')
52
102
  return d['results']
53
103
  end
54
104
  nil
55
105
  end
56
106
 
107
+
108
+ ##
109
+ # A helper method to read a key within the results.
57
110
  def get_result(key)
58
- # A helper method so you don't have to do any nil checking
59
- # as you dig through the response results
60
111
  r = results
61
112
  if r != nil
62
- return results[key]
113
+ return r.fetch(key, nil)
63
114
  end
64
115
  nil
65
116
  end
66
117
 
67
- def msgs()
68
- # this is just double checking the msgs data
69
- # exists and is built with the correct structure
118
+
119
+ ##
120
+ # An array of messages, and each message is a hash.
121
+ # Example message within the msgs array:
122
+ # "text" => "info about the message", "status" => "error"
123
+ #
124
+ # @return [array] A response object with helper methods to read the response.
125
+ def msgs
70
126
  d = data
71
- if d.has_key?('msgs')
72
- return d['msgs']
127
+ if d != nil
128
+ return d.fetch('msgs', [])
73
129
  end
74
- nil
130
+ return []
131
+ end
132
+
133
+
134
+ ##
135
+ # @return [bool] A boolean indicating if this response has messages or not.
136
+ def has_msgs
137
+ msgs and msgs.length > 0
138
+ end
139
+
140
+
141
+ ##
142
+ # @return [bool] A boolean indicating if this response has messages or not.
143
+ def has_errors
144
+ if msgs and msgs.length > 0
145
+ for msg in msgs
146
+ if msg["status"] == "error"
147
+ return true
148
+ end
149
+ end
150
+ end
151
+ false
75
152
  end
76
153
 
77
- def format()
78
- @response.headers['content-type']
154
+
155
+ ##
156
+ # Used to merge two responses into one APIResponse. This is
157
+ # done automatically when uploading numerous files.
158
+ #
159
+ # @return [APIResponse]
160
+ def merge(updating_response)
161
+ if updating_response == nil or updating_response.data == nil
162
+ return self
163
+ end
164
+
165
+ if updating_response.status > status
166
+ @status = updating_response.status
167
+ end
168
+
169
+ if data == nil
170
+ @data = updating_response.data
171
+ return self
172
+ end
173
+
174
+ @data['msgs'] += updating_response.msgs
175
+
176
+ if updating_response.files != nil
177
+ f = files
178
+ if f != nil
179
+ @data['results']['files'] += updating_response.files
180
+ else
181
+ @data['results']['files'] = updating_response.files
182
+ end
183
+ end
184
+
185
+ return self
79
186
  end
80
187
 
81
- def status()
82
- @response.status
188
+
189
+ ##
190
+ # @return [int] The HTTP response status.
191
+ def status
192
+ @status
83
193
  end
84
194
 
85
- def is_success()
86
- (status >= 200 and status < 300)
195
+ ##
196
+ # @return [bool]
197
+ def is_success
198
+ status >= 200 and status < 300 and not has_errors
87
199
  end
88
200
 
89
- def is_error()
90
- (status >= 400)
201
+ ##
202
+ # @return [bool]
203
+ def is_error
204
+ status >= 400 or has_errors
91
205
  end
92
206
 
93
- def is_client_error()
94
- (status >= 400 and status < 500)
207
+ ##
208
+ # @return [bool]
209
+ def is_client_error
210
+ status >= 400 and status < 500
95
211
  end
96
212
 
97
- def is_bad_request()
98
- (status == 400)
213
+ ##
214
+ # @return [bool]
215
+ def is_bad_request
216
+ status == 400
99
217
  end
100
218
 
101
- def is_unauthorized()
102
- (status == 401)
219
+ ##
220
+ # @return [bool]
221
+ def is_unauthorized
222
+ status == 401
103
223
  end
104
224
 
105
- def is_forbidden()
106
- (status == 403)
225
+ ##
226
+ # @return [bool]
227
+ def is_forbidden
228
+ status == 403
107
229
  end
108
230
 
109
- def is_not_found()
110
- (status == 404)
231
+ ##
232
+ # @return [bool]
233
+ def is_not_found
234
+ status == 404
111
235
  end
112
236
 
113
- def is_method_not_allowed()
114
- (status == 405)
237
+ ##
238
+ # @return [bool]
239
+ def is_method_not_allowed
240
+ status == 405
115
241
  end
116
242
 
117
- def is_server_error()
118
- (status >= 500)
243
+ ##
244
+ # @return [bool]
245
+ def is_server_error
246
+ status >= 500
119
247
  end
120
248
 
121
- def is_bad_gateway()
122
- (status >= 502)
249
+ ##
250
+ # @return [bool]
251
+ def is_bad_gateway
252
+ status == 502
123
253
  end
124
254
 
125
- def is_service_unavailable()
126
- (status >= 503)
255
+ ##
256
+ # @return [bool]
257
+ def is_service_unavailable
258
+ status == 503
127
259
  end
128
260
 
129
261
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cdnconnect-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Bradley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-17 00:00:00.000000000 Z
11
+ date: 2013-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -16,28 +16,42 @@ dependencies:
16
16
  requirements:
17
17
  - - '>='
18
18
  - !ruby/object:Gem::Version
19
- version: 0.8.6
19
+ version: 0.8.7
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
- version: 0.8.6
26
+ version: 0.8.7
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: signet
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - '>='
32
32
  - !ruby/object:Gem::Version
33
- version: 0.4.5
33
+ version: 0.5.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
- version: 0.4.5
40
+ version: 0.5.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description: CDN Connect makes it easier to manage production assets for teams of
42
56
  developers and designers, all while serving files from a fast content delivery network.
43
57
  Features include image optimization, resizing, cropping, filters, etc. The CDN Connect