roomorama_api 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +2 -1
- data/README.md +1 -1
- data/lib/roomorama_api.rb +9 -26
- data/lib/roomorama_api/api/errors.rb +72 -0
- data/lib/roomorama_api/version.rb +1 -1
- data/test/roomorama_api_test.rb +9 -0
- metadata +3 -2
data/.travis.yml
CHANGED
data/README.md
CHANGED
data/lib/roomorama_api.rb
CHANGED
@@ -8,6 +8,7 @@ require "roomorama_api/api/users"
|
|
8
8
|
require "roomorama_api/api/host_properties"
|
9
9
|
require "roomorama_api/api/host_availabilities"
|
10
10
|
require "roomorama_api/api/host_inquiries"
|
11
|
+
require "roomorama_api/api/errors"
|
11
12
|
|
12
13
|
require "faraday"
|
13
14
|
require "json"
|
@@ -24,6 +25,8 @@ module RoomoramaApi
|
|
24
25
|
include RoomoramaApi::Api::HostAvailabilities
|
25
26
|
include RoomoramaApi::Api::HostInquiries
|
26
27
|
|
28
|
+
include RoomoramaApi::Api::Errors
|
29
|
+
|
27
30
|
def initialize(oauth_token=nil)
|
28
31
|
@oauth_token = oauth_token
|
29
32
|
end
|
@@ -63,7 +66,7 @@ module RoomoramaApi
|
|
63
66
|
def connection(method_name, options, verb)
|
64
67
|
conn = Faraday.new(:url => api_url) do |faraday|
|
65
68
|
faraday.request :url_encoded
|
66
|
-
|
69
|
+
faraday.response(:logger) if self.class.debug
|
67
70
|
faraday.adapter Faraday.default_adapter
|
68
71
|
faraday.headers['User-Agent'] = "RoomoramaApi gem v#{VERSION}"
|
69
72
|
faraday.headers['Authorization'] = "Bearer " + @oauth_token if @oauth_token
|
@@ -79,31 +82,11 @@ module RoomoramaApi
|
|
79
82
|
|
80
83
|
|
81
84
|
def raise_errors(response)
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
when 400
|
86
|
-
raise BadRequest, message
|
87
|
-
when 401
|
88
|
-
raise Unauthorized, message
|
89
|
-
when 403
|
90
|
-
raise General, message
|
91
|
-
when 404
|
92
|
-
raise NotFound, message
|
93
|
-
when 500
|
94
|
-
raise InternalError, "An internal error is thrown."
|
95
|
-
when 502, 503
|
96
|
-
raise Unavailable, message
|
97
|
-
end
|
85
|
+
if e = HTTP_STATUS_ERRORS[response.status.to_i]
|
86
|
+
raise e
|
87
|
+
end
|
98
88
|
end
|
99
89
|
|
100
|
-
end
|
101
|
-
|
102
|
-
class BadRequest < StandardError; end
|
103
|
-
class Unauthorized < StandardError; end
|
104
|
-
class General < StandardError; end
|
105
|
-
class Unavailable < StandardError; end
|
106
|
-
class InternalError < StandardError; end
|
107
|
-
class NotFound < StandardError; end
|
90
|
+
end # end class Client
|
108
91
|
|
109
|
-
end
|
92
|
+
end # end module RoomoramaApi
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module RoomoramaApi
|
2
|
+
module Api
|
3
|
+
module Errors
|
4
|
+
|
5
|
+
#According to https://www.roomorama.com/api/documentation
|
6
|
+
|
7
|
+
class BadRequest < StandardError
|
8
|
+
def initialize
|
9
|
+
super("Bad Request")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Unauthorized < StandardError
|
14
|
+
def initialize
|
15
|
+
super("You're not authorized to access this resource, make sure you're sending the right authorization header in your request")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class General < StandardError
|
20
|
+
def initialize
|
21
|
+
super("Generic error")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Unavailable < StandardError
|
26
|
+
def initialize
|
27
|
+
super("Unavailable")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class UnprocessableEntity < StandardError
|
32
|
+
def initialize
|
33
|
+
super("Something went wrong while saving or updating your object, make sure the data you're sending in the request is correct")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class InternalError < StandardError
|
38
|
+
def initialize
|
39
|
+
super("An error has occurred in Roomorama service")
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class NotFound < StandardError
|
44
|
+
def initialize
|
45
|
+
super("The resource doesn't exist")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class Conflict < StandardError
|
50
|
+
def initialize
|
51
|
+
super("The request cannot be performed because of the state the resource is in")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# HTTP code => error class mapping
|
56
|
+
|
57
|
+
HTTP_STATUS_ERRORS = {
|
58
|
+
400 => BadRequest,
|
59
|
+
401 => Unauthorized,
|
60
|
+
403 => General,
|
61
|
+
404 => NotFound,
|
62
|
+
409 => Conflict,
|
63
|
+
422 => UnprocessableEntity,
|
64
|
+
500 => InternalError,
|
65
|
+
502 => Unavailable,
|
66
|
+
503 => Unavailable
|
67
|
+
}
|
68
|
+
|
69
|
+
end # end module Errors
|
70
|
+
end # end module Api
|
71
|
+
end # end module RoomoramaApi
|
72
|
+
|
data/test/roomorama_api_test.rb
CHANGED
@@ -179,6 +179,15 @@ class RoomoramaApiTest < MiniTest::Unit::TestCase
|
|
179
179
|
assert_equal result["result"]["id"], 9999
|
180
180
|
end
|
181
181
|
|
182
|
+
#### Errors
|
183
|
+
|
184
|
+
def test_raise_not_found_error
|
185
|
+
FakeWeb.register_uri(:get, "https://api.roomorama.com/v1.0/destinations", body: "Nothing found", status: ["404", "Not Found"])
|
186
|
+
assert_raises ::RoomoramaApi::Api::Errors::NotFound do
|
187
|
+
@client.destinations_all
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
182
191
|
# def test_get_data_for_an_empty_property
|
183
192
|
# assert_raises RoomoramaApi::NotFound do
|
184
193
|
# result = @client.properties_get_data 500
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: roomorama_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-12-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faraday
|
@@ -92,6 +92,7 @@ files:
|
|
92
92
|
- Rakefile
|
93
93
|
- lib/roomorama_api.rb
|
94
94
|
- lib/roomorama_api/api/destinations.rb
|
95
|
+
- lib/roomorama_api/api/errors.rb
|
95
96
|
- lib/roomorama_api/api/favorites.rb
|
96
97
|
- lib/roomorama_api/api/host_availabilities.rb
|
97
98
|
- lib/roomorama_api/api/host_inquiries.rb
|