cartodb-api 0.1.0 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 115e7af3d7d8e0810eca5fa32d4fd3157cde35f7
4
- data.tar.gz: caa7a9e9f72f208875a127407a7782f3198beed5
3
+ metadata.gz: 7a0e23410a86f4c4a24185eec41dbd3ef464b23a
4
+ data.tar.gz: 4c3de01fba8a8d7ef1f1e9821210c2f05498c65a
5
5
  SHA512:
6
- metadata.gz: 7154c5cb5870bf5c627838e7d38db990a8968a0e0b4cd554442f41a01fb9060fded4e1055dd96be054b9ae4e5deab9b35694ca80c860d11eae6642308a03ecb4
7
- data.tar.gz: a89fb9e291d0a8ad5d8c689d0a7b855937dc6cdcc1bbad0c50343cb5254921f42c2f099bf5b760ca3c7ba9a64039b896dafc8a6ba8decd2eaad5a3997c200d88
6
+ metadata.gz: a4998d02cbe0c9fbcab481fb14a70c3d62f266d712f049059ca7f191f13331937fb893dd0d1b1b32bc66f20f8a476ce95a127dbbc7b455876624bdfd27486bbf
7
+ data.tar.gz: 1e647c9cbf906afca5a9e8d48d725bc115488f2d4b349c74bea6b9f6333a9cfc027c5d279fa0215325dcc338537d5cf099d94d19b9159d800e10f02958389b5c
data/lib/cartodb/api.rb CHANGED
@@ -32,10 +32,18 @@ module CartoDB
32
32
  @@default_configuration = configuration
33
33
  end
34
34
 
35
+ def build_configuration(configuration = nil)
36
+ if configuration
37
+ default_configuration.dup.merge(configuration)
38
+ else
39
+ default_configuration.dup
40
+ end
41
+ end
42
+
35
43
  protected
36
44
 
37
45
  def build_request(configuration = nil)
38
- configuration = configuration ? default_configuration.merge(configuration) : default_configuration.dup
46
+ configuration = build_configuration(configuration)
39
47
  CartoDB::Api::Request.new(configuration)
40
48
  end
41
49
 
@@ -44,7 +52,6 @@ module CartoDB
44
52
  def default_configuration
45
53
  @@default_configuration ||= CartoDB::Api::Configuration.new
46
54
  end
47
-
48
55
  end
49
56
  end
50
57
  end
@@ -1,16 +1,35 @@
1
1
  module CartoDB
2
2
  module Api
3
- class ConnectionFailed < StandardError
3
+ class CartoDBError < StandardError
4
+ attr_accessor :title, :details, :body, :raw_body, :status_code
4
5
  end
5
6
 
6
- class InvalidConfiguration < StandardError
7
+ class ConnectionFailed < CartoDBError
7
8
  end
8
9
 
9
- class Error < StandardError
10
- attr_accessor :title, :details, :body, :raw_body, :status_code
10
+ class InvalidConfiguration < CartoDBError
11
+ end
12
+
13
+ class ApiError < CartoDBError
14
+ def to_s
15
+ whole_message = "the server responded with status #{status_code}"
16
+ whole_message = append_to_message(whole_message, title)
17
+ whole_message = append_to_message(whole_message, details)
18
+ append_to_message(whole_message, body)
19
+ end
20
+
21
+ private
22
+
23
+ def append_to_message(message, text)
24
+ if text && !text.empty?
25
+ "#{message}\n#{text}"
26
+ else
27
+ message
28
+ end
29
+ end
11
30
  end
12
31
 
13
- class ParsingError < Error
32
+ class ParsingError < ApiError
14
33
  end
15
34
  end
16
35
  end
@@ -3,24 +3,25 @@ module CartoDB
3
3
  class Request
4
4
  extend Forwardable
5
5
 
6
- attr_accessor :configuration
6
+ attr_reader :path
7
7
 
8
8
  def_delegators :configuration, :protocol, :domain, :api_key,
9
9
  :account, :base_url, :version, :timeout
10
10
 
11
- def initialize(configuration)
12
- self.configuration = configuration
13
- reset
11
+ def initialize(configuration = nil)
12
+ self.configuration = CartoDB::Api.build_configuration(configuration)
13
+ reset_path
14
14
  end
15
15
 
16
16
  def method_missing(method, *args)
17
- @path_steps << method.downcase
18
- @path_steps << args if args.length > 0
19
- @path_steps.flatten!
17
+ path_steps << method.downcase
18
+ path_steps << args unless args.empty?
19
+ path_steps.flatten!
20
20
  self
21
21
  end
22
22
 
23
23
  def create(params: nil, headers: nil, body: nil, payload: nil)
24
+ build_and_reset_path
24
25
  make_request(:post,
25
26
  params: params,
26
27
  headers: headers,
@@ -29,6 +30,7 @@ module CartoDB
29
30
  end
30
31
 
31
32
  def update(params: nil, headers: nil, body: nil, payload: nil)
33
+ build_and_reset_path
32
34
  make_request(:patch,
33
35
  params: params,
34
36
  headers: headers,
@@ -37,17 +39,32 @@ module CartoDB
37
39
  end
38
40
 
39
41
  def retrieve(params: nil, headers: nil)
42
+ build_and_reset_path
40
43
  make_request(:get, params: params, headers: headers)
41
44
  end
42
45
 
43
46
  def delete(params: nil, headers: nil)
47
+ build_and_reset_path
44
48
  make_request(:delete, params: params, headers: headers)
45
49
  end
46
50
 
47
51
  protected
48
52
 
49
- def reset
50
- @path_steps = []
53
+ attr_accessor :configuration
54
+
55
+ attr_accessor :path_steps
56
+
57
+ def reset_path
58
+ self.path_steps = []
59
+ end
60
+
61
+ def build_path
62
+ @path = path_steps.join('/')
63
+ end
64
+
65
+ def build_and_reset_path
66
+ build_path
67
+ reset_path
51
68
  end
52
69
 
53
70
  def client
@@ -109,12 +126,8 @@ module CartoDB
109
126
  "v#{version}/"
110
127
  end
111
128
 
112
- def path
113
- @path_steps.join('/')
114
- end
115
-
116
129
  def rescue_error(exception)
117
- cartodb_exception = Error.new(exception.message)
130
+ cartodb_exception = ApiError.new(exception.message)
118
131
 
119
132
  is_faraday_exception = exception.is_a?(Faraday::Error::ClientError)
120
133
  if is_faraday_exception && exception.response
@@ -122,14 +135,14 @@ module CartoDB
122
135
  exception)
123
136
  end
124
137
 
125
- raise cartodb_exception
138
+ fail cartodb_exception
126
139
  end
127
140
 
128
141
  def build_error_from_faraday(cartodb_exception, exception)
129
142
  cartodb_exception.status_code = exception.response[:status]
130
143
  begin
131
144
  response = MultiJson.load(exception.response[:body])
132
- build_error(cartodb_exception,
145
+ cartodb_exception = build_error(cartodb_exception,
133
146
  response['title'],
134
147
  response['detail'],
135
148
  response,
@@ -144,6 +157,7 @@ module CartoDB
144
157
  exception.details = details if details
145
158
  exception.body = body
146
159
  exception.raw_body = raw_body if raw_body
160
+ exception
147
161
  end
148
162
  end
149
163
  end
@@ -1,5 +1,5 @@
1
1
  module CartoDB
2
2
  module Api
3
- VERSION = '0.1.0'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cartodb-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pablo Cárdenas
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-12 00:00:00.000000000 Z
11
+ date: 2016-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler