soda-ruby 0.2.15 → 0.2.16
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 +4 -4
- data/lib/soda/client.rb +16 -3
- data/lib/soda/exceptions.rb +207 -0
- data/lib/soda/version.rb +1 -1
- metadata +15 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13d50e52df53fba1c46a89fab16acfc9658de473
|
4
|
+
data.tar.gz: 6c5fdfd23f1a7eef3b7dbd1f4043d027d12b1040
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48d11e20e4996977d0a5026e0427e34187839cc3f844b7af033cdaeff28798cb28b957fbb298b07d967e15ecd2ef3d243a93baa60bbec3e3304a6f474ef5676a
|
7
|
+
data.tar.gz: 543f337c1b53104a21f4455e8bc20a0ad7c15f2be323c76eb378e887d72ef4aa5fdeafcb488699a42cda3f5c072665a8a612856e39e19e9c76df0b5436b6e428
|
data/lib/soda/client.rb
CHANGED
@@ -12,6 +12,7 @@ require 'cgi'
|
|
12
12
|
require 'hashie'
|
13
13
|
require 'sys/uname'
|
14
14
|
require 'soda/version'
|
15
|
+
require 'soda/exceptions'
|
15
16
|
include Sys
|
16
17
|
|
17
18
|
module SODA
|
@@ -34,6 +35,7 @@ module SODA
|
|
34
35
|
# * +:username+ - Your Socrata username (optional, only necessary for modifying data)
|
35
36
|
# * +:password+ - Your Socrata password (optional, only necessary for modifying data)
|
36
37
|
# * +:app_token+ - Your Socrata application token (register at http://dev.socrata.com/register)
|
38
|
+
# * +:access_token+ - Your Socrata OAuth token (optional, https://dev.socrata.com/docs/authentication.html)
|
37
39
|
# * +:ignore_ssl+ - Ignore ssl errors (defaults to false)
|
38
40
|
#
|
39
41
|
# Returns a SODA::Client instance.
|
@@ -183,7 +185,15 @@ module SODA
|
|
183
185
|
|
184
186
|
def check_response_fail(response)
|
185
187
|
return if %w(200 202).include? response.code
|
186
|
-
|
188
|
+
|
189
|
+
# Adapted from RestClient's exception handling
|
190
|
+
begin
|
191
|
+
klass = SODA::Exceptions::EXCEPTIONS_MAP.fetch(response.code.to_i)
|
192
|
+
|
193
|
+
raise klass.new(response, response.code)
|
194
|
+
rescue KeyError
|
195
|
+
raise RequestFailed.new(response, response.code)
|
196
|
+
end
|
187
197
|
end
|
188
198
|
|
189
199
|
def connection(method = 'Get', resource = nil, body = nil, params = {})
|
@@ -236,9 +246,12 @@ module SODA
|
|
236
246
|
end
|
237
247
|
|
238
248
|
def authenticate(request)
|
239
|
-
return unless @config[:username]
|
240
249
|
# Authenticate if we're supposed to
|
241
|
-
|
250
|
+
if @config[:username]
|
251
|
+
request.basic_auth @config[:username], @config[:password]
|
252
|
+
elsif @config[:access_token]
|
253
|
+
request.add_field('Authorization', "OAuth #{@config[:access_token]}")
|
254
|
+
end
|
242
255
|
end
|
243
256
|
|
244
257
|
def add_default_headers_to_request(request)
|
@@ -0,0 +1,207 @@
|
|
1
|
+
# Adapted (lifted) from RestClient: https://github.com/rest-client/rest-client/blob/master/lib/restclient/exceptions.rb
|
2
|
+
module SODA
|
3
|
+
|
4
|
+
STATUSES = {100 => 'Continue',
|
5
|
+
101 => 'Switching Protocols',
|
6
|
+
102 => 'Processing', #WebDAV
|
7
|
+
|
8
|
+
200 => 'OK',
|
9
|
+
201 => 'Created',
|
10
|
+
202 => 'Accepted',
|
11
|
+
203 => 'Non-Authoritative Information', # http/1.1
|
12
|
+
204 => 'No Content',
|
13
|
+
205 => 'Reset Content',
|
14
|
+
206 => 'Partial Content',
|
15
|
+
207 => 'Multi-Status', #WebDAV
|
16
|
+
|
17
|
+
300 => 'Multiple Choices',
|
18
|
+
301 => 'Moved Permanently',
|
19
|
+
302 => 'Found',
|
20
|
+
303 => 'See Other', # http/1.1
|
21
|
+
304 => 'Not Modified',
|
22
|
+
305 => 'Use Proxy', # http/1.1
|
23
|
+
306 => 'Switch Proxy', # no longer used
|
24
|
+
307 => 'Temporary Redirect', # http/1.1
|
25
|
+
|
26
|
+
400 => 'Bad Request',
|
27
|
+
401 => 'Unauthorized',
|
28
|
+
402 => 'Payment Required',
|
29
|
+
403 => 'Forbidden',
|
30
|
+
404 => 'Not Found',
|
31
|
+
405 => 'Method Not Allowed',
|
32
|
+
406 => 'Not Acceptable',
|
33
|
+
407 => 'Proxy Authentication Required',
|
34
|
+
408 => 'Request Timeout',
|
35
|
+
409 => 'Conflict',
|
36
|
+
410 => 'Gone',
|
37
|
+
411 => 'Length Required',
|
38
|
+
412 => 'Precondition Failed',
|
39
|
+
413 => 'Request Entity Too Large',
|
40
|
+
414 => 'Request-URI Too Long',
|
41
|
+
415 => 'Unsupported Media Type',
|
42
|
+
416 => 'Requested Range Not Satisfiable',
|
43
|
+
417 => 'Expectation Failed',
|
44
|
+
418 => 'I\'m A Teapot', #RFC2324
|
45
|
+
421 => 'Too Many Connections From This IP',
|
46
|
+
422 => 'Unprocessable Entity', #WebDAV
|
47
|
+
423 => 'Locked', #WebDAV
|
48
|
+
424 => 'Failed Dependency', #WebDAV
|
49
|
+
425 => 'Unordered Collection', #WebDAV
|
50
|
+
426 => 'Upgrade Required',
|
51
|
+
428 => 'Precondition Required', #RFC6585
|
52
|
+
429 => 'Too Many Requests', #RFC6585
|
53
|
+
431 => 'Request Header Fields Too Large', #RFC6585
|
54
|
+
449 => 'Retry With', #Microsoft
|
55
|
+
450 => 'Blocked By Windows Parental Controls', #Microsoft
|
56
|
+
|
57
|
+
500 => 'Internal Server Error',
|
58
|
+
501 => 'Not Implemented',
|
59
|
+
502 => 'Bad Gateway',
|
60
|
+
503 => 'Service Unavailable',
|
61
|
+
504 => 'Gateway Timeout',
|
62
|
+
505 => 'HTTP Version Not Supported',
|
63
|
+
506 => 'Variant Also Negotiates',
|
64
|
+
507 => 'Insufficient Storage', #WebDAV
|
65
|
+
509 => 'Bandwidth Limit Exceeded', #Apache
|
66
|
+
510 => 'Not Extended',
|
67
|
+
511 => 'Network Authentication Required', # RFC6585
|
68
|
+
}
|
69
|
+
|
70
|
+
# This is the base exception class. Rescue it if you want to
|
71
|
+
# catch any exception that your request might raise
|
72
|
+
# You can get the status code by e.http_code, or see anything about the
|
73
|
+
# response via e.response.
|
74
|
+
# For example, the entire result body (which is
|
75
|
+
# probably an HTML error page) is e.response.
|
76
|
+
class Exception < RuntimeError
|
77
|
+
attr_accessor :response
|
78
|
+
attr_accessor :original_exception
|
79
|
+
attr_accessor :data
|
80
|
+
attr_writer :message
|
81
|
+
|
82
|
+
def initialize response = nil, initial_response_code = nil
|
83
|
+
@response = response
|
84
|
+
@message = nil
|
85
|
+
@initial_response_code = initial_response_code
|
86
|
+
end
|
87
|
+
|
88
|
+
def http_code
|
89
|
+
# return integer for compatibility
|
90
|
+
if @response
|
91
|
+
@response.code.to_i
|
92
|
+
else
|
93
|
+
@initial_response_code
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def http_headers
|
98
|
+
@response.headers if @response
|
99
|
+
end
|
100
|
+
|
101
|
+
def http_body
|
102
|
+
@response.body if @response
|
103
|
+
end
|
104
|
+
|
105
|
+
def to_s
|
106
|
+
message
|
107
|
+
end
|
108
|
+
|
109
|
+
def message
|
110
|
+
@message || default_message
|
111
|
+
end
|
112
|
+
|
113
|
+
def default_message
|
114
|
+
self.class.name
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
# Compatibility
|
119
|
+
class ExceptionWithResponse < Exception
|
120
|
+
end
|
121
|
+
|
122
|
+
# The request failed with an error code not managed by the code
|
123
|
+
class RequestFailed < ExceptionWithResponse
|
124
|
+
|
125
|
+
def default_message
|
126
|
+
"HTTP status code #{http_code}"
|
127
|
+
end
|
128
|
+
|
129
|
+
def to_s
|
130
|
+
message
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
# Exception classes. TODO: move all exceptions into this module.
|
135
|
+
#
|
136
|
+
# We will a create an exception for each status code, see
|
137
|
+
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
|
138
|
+
#
|
139
|
+
module Exceptions
|
140
|
+
# Map http status codes to the corresponding exception class
|
141
|
+
EXCEPTIONS_MAP = {}
|
142
|
+
end
|
143
|
+
|
144
|
+
# Create HTTP status exception classes
|
145
|
+
STATUSES.each_pair do |code, message|
|
146
|
+
klass = Class.new(RequestFailed) do
|
147
|
+
send(:define_method, :default_message) {"#{http_code ? "#{http_code} " : ''}#{message}"}
|
148
|
+
end
|
149
|
+
klass_constant = const_set(message.delete(' \-\''), klass)
|
150
|
+
Exceptions::EXCEPTIONS_MAP[code] = klass_constant
|
151
|
+
end
|
152
|
+
|
153
|
+
# Backwards compatibility. "Not Found" is the actual text in the RFCs.
|
154
|
+
ResourceNotFound = NotFound
|
155
|
+
|
156
|
+
module Exceptions
|
157
|
+
# We have to split the Exceptions module like we do here because the
|
158
|
+
# EXCEPTIONS_MAP is under Exceptions, but we depend on
|
159
|
+
# SODA::RequestTimeout below.
|
160
|
+
|
161
|
+
# Base class for request timeouts.
|
162
|
+
#
|
163
|
+
# NB: Previous releases of rest-client would raise RequestTimeout both for
|
164
|
+
# HTTP 408 responses and for actual connection timeouts.
|
165
|
+
class Timeout < SODA::RequestTimeout
|
166
|
+
def initialize(message=nil, original_exception=nil)
|
167
|
+
super(nil, nil)
|
168
|
+
self.message = message if message
|
169
|
+
self.original_exception = original_exception if original_exception
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
# Timeout when connecting to a server. Typically wraps Net::OpenTimeout (in
|
174
|
+
# ruby 2.0 or greater).
|
175
|
+
class OpenTimeout < Timeout
|
176
|
+
def default_message
|
177
|
+
'Timed out connecting to server'
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
# Timeout when reading from a server. Typically wraps Net::ReadTimeout (in
|
182
|
+
# ruby 2.0 or greater).
|
183
|
+
class ReadTimeout < Timeout
|
184
|
+
def default_message
|
185
|
+
'Timed out reading data from server'
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
|
191
|
+
# The server broke the connection prior to the request completing. Usually
|
192
|
+
# this means it crashed, or sometimes that your network connection was
|
193
|
+
# severed before it could complete.
|
194
|
+
class ServerBrokeConnection < Exception
|
195
|
+
def initialize(message = 'Server broke connection')
|
196
|
+
super nil, nil
|
197
|
+
self.message = message
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
class SSLCertificateNotVerified < Exception
|
202
|
+
def initialize(message)
|
203
|
+
super nil, nil
|
204
|
+
self.message = message
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
data/lib/soda/version.rb
CHANGED
metadata
CHANGED
@@ -1,57 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: soda-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.16
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Metcalf
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashie
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 3.4.2
|
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:
|
26
|
+
version: 3.4.2
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: multipart-post
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 2.0.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:
|
40
|
+
version: 2.0.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: sys-uname
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 1.0.2
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 1.0.2
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: bundler
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -176,6 +176,7 @@ files:
|
|
176
176
|
- lib/omniauth-socrata.rb
|
177
177
|
- lib/soda.rb
|
178
178
|
- lib/soda/client.rb
|
179
|
+
- lib/soda/exceptions.rb
|
179
180
|
- lib/soda/smartupdate.rb
|
180
181
|
- lib/soda/version.rb
|
181
182
|
homepage: http://github.com/socrata/soda-ruby
|