cartodb-api 0.0.2 → 0.1.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: b4935b964b5d6c1696b7d40e4523841b7bb26dd6
4
- data.tar.gz: ea948d6ec927b6f216d6acc5b4221d3d2d25a462
3
+ metadata.gz: 115e7af3d7d8e0810eca5fa32d4fd3157cde35f7
4
+ data.tar.gz: caa7a9e9f72f208875a127407a7782f3198beed5
5
5
  SHA512:
6
- metadata.gz: 884bfa172f755b0f343dd7e771c416680e3fc4a0fa96bc664be69fe453fd20d48e6dfd3cfa755103975f576863fb252a75a7f8501e91b486fb326aff1ea0d6ef
7
- data.tar.gz: 6cf5f9a1ff09b8744aeae30a09e32d8a374cd47f0480ce00f7cf9ed87f49d206e368674971473ec04c3b1c3c487bf604e4dc21413731263a4e8ab9d0c6c27dab
6
+ metadata.gz: 7154c5cb5870bf5c627838e7d38db990a8968a0e0b4cd554442f41a01fb9060fded4e1055dd96be054b9ae4e5deab9b35694ca80c860d11eae6642308a03ecb4
7
+ data.tar.gz: a89fb9e291d0a8ad5d8c689d0a7b855937dc6cdcc1bbad0c50343cb5254921f42c2f099bf5b760ca3c7ba9a64039b896dafc8a6ba8decd2eaad5a3997c200d88
data/README.md CHANGED
@@ -1,10 +1,8 @@
1
- [![Code Climate](https://codeclimate.com/github/pablo-co/cartodb-api/badges/gpa.svg)](https://codeclimate.com/github/pablo-co/cartodb-api)
2
-
3
- [![Test Coverage](https://codeclimate.com/github/pablo-co/cartodb-api/badges/coverage.svg)](https://codeclimate.com/github/pablo-co/cartodb-api/coverage)
1
+ [![Code Climate](https://codeclimate.com/github/pablo-co/cartodb-api/badges/gpa.svg)](https://codeclimate.com/github/pablo-co/cartodb-api) [![Test Coverage](https://codeclimate.com/github/pablo-co/cartodb-api/badges/coverage.svg)](https://codeclimate.com/github/pablo-co/cartodb-api/coverage)
4
2
 
5
3
  # CartoDB::Api
6
4
 
7
- CartoDB::Api is an API library for Ruby which acts as a wrapper for [CartoDB's API](cartodb.com). All requests are built using a really simple fluid interface.
5
+ CartoDB::Api is an API library for Ruby which acts as a wrapper for [CartoDB's API](https://cartodb.com/). All requests are built using a really simple fluid interface.
8
6
 
9
7
  ## Installation
10
8
 
@@ -24,7 +22,129 @@ Or install it yourself as:
24
22
 
25
23
  ## Usage
26
24
 
27
- TODO: Write usage instructions here
25
+ All requests need a configuration object. The first thing you need is to create one.
26
+
27
+ ```ruby
28
+ configuration = CartoDB::Api::Configuration.new
29
+ ```
30
+
31
+ The configuration defines all the details regarding the API requests.
32
+
33
+ * __timeout__: The request timeout in seconds (default: 30).
34
+ * __protocol__: The protocol that will be used for requests (default: https).
35
+ * __version__: The CartoDB API version that is going to be used (default: 1).
36
+ * __domain__: The domain or IP where the API can be found (default: cartodb.com).
37
+ * __api_key__: The CartoDB API key that will be used for authentication.
38
+ * __account__: The CartoDB account from which all requests are going to be made from.
39
+
40
+ ```ruby
41
+ configuration.timeout = 15
42
+ configuration.protocol = 'https'
43
+ configuration.domain = 'customdomain.com'
44
+ configuration.api_key = '<Your API key>'
45
+ configuration.account = '<Account>'
46
+ ```
47
+
48
+ __If you are trying to use the API available at cartodb.com the only configuration that you need to set is the api_key and account.__
49
+
50
+ To make this configuration the default for all requests:
51
+
52
+ ```ruby
53
+ CartoDB::Api.default_configuration = configuration
54
+ ```
55
+
56
+ For example, you could set the default configuration in an initializer file in your Rails app (config/initializers/cartodb.rb).
57
+
58
+ ```ruby
59
+ configuration = CartoDB::Api:Configuration.new
60
+ configuration.api_key = ENV['CARTODB_API_KEY']
61
+ configuration.account = '<Account>'
62
+
63
+ CartoDB::Api.default_configuration = configuration
64
+ ```
65
+
66
+ ### Making requests
67
+
68
+ Once you have a configuration object you can create a request:
69
+
70
+ ```ruby
71
+ cartodb = CartoDB::Api::Request.new(configuration)
72
+ ```
73
+
74
+ If you have set a default configuration object there's no need of specifying one during creation:
75
+
76
+
77
+ ```ruby
78
+ cartodb = CartoDB::Api::Request.new
79
+ ```
80
+
81
+ Now you can make requests to the API resources specified in [CartoDB's API](https://cartodb.com/). Resource IDs are specified as arguments and the verbs: create, retrieve, update, and delete initiate the request.
82
+
83
+ For example to interact with the [import resource](http://docs.cartodb.com/cartodb-platform/import-api.html):
84
+
85
+ We define the request.
86
+ ```ruby
87
+ cartodb = CartoDB::Api::Request.new
88
+ ```
89
+
90
+ To get all imports.
91
+ ```ruby
92
+ cartodb.imports.retrieve
93
+ ```
94
+
95
+ To retrieve a specific import.
96
+ ```ruby
97
+ cartodb.imports('<import id>').retrieve
98
+ ```
99
+
100
+ You can also specify the params, headers, body and payload when calling a CRUD method. For example:
101
+
102
+ ```ruby
103
+ cartodb.imports.create(
104
+ headers: {
105
+ some_header: 'header_value'
106
+ },
107
+ params: {
108
+ some_param: 'param_value'
109
+ },
110
+ body: body,
111
+ payload: {
112
+ file: Faraday::UploadIO.new('<file>', '<mime type>')
113
+ }
114
+ )
115
+ ```
116
+
117
+ All requests can be written inline without the need to create a Request object:
118
+
119
+ ```ruby
120
+ CartoDB::Api.imports.retrieve
121
+ CartoDB::Api.imports('<import id>').retrieve
122
+ ```
123
+
124
+ ### Uploading files
125
+
126
+ To upload files along the request you need to pass a payload. This library uses the excellent [Faraday](https://github.com/lostisland/faraday) library, thus files are sent using the `Faraday::UploadIO` class.
127
+ ```ruby
128
+ Faraday::UploadIO.new('<file>', '<mime type>')
129
+ ```
130
+ __Please note that the mime type depends on the file you are sending.__
131
+
132
+ For example to create a new import from a csv file.
133
+
134
+ ```ruby
135
+ csv_file = Faraday::UploadIO.new('file.csv', 'text/csv')
136
+ CartoDB::Api.imports.create(payload: {file: csv_file})
137
+ ```
138
+
139
+ ### Handling errors
140
+
141
+ There are 3 types of errors this gem can raise:
142
+
143
+ * __CartoDB::Api::InvalidConfiguration__: This error is raised when the configuration object is invalid or incomplete.
144
+ * __CartoDB::Api::Error__: This error is raised when there's an error related to the API request.
145
+ * __CartoDB::Api::ParsingError__: This error is raised when the request was successful but there was a problem parsing the JSON returned.
146
+
147
+ To retrieve more information about an error `CartoDB::Api::Error` and `CartoDB::Api::ParsingError` provide the following attributes: `title`, `details`, `body`, `raw_body` and `status_code`. Some may not be present depending on the nature of the error.
28
148
 
29
149
  ## Development
30
150
 
@@ -20,20 +20,28 @@ module CartoDB
20
20
  self
21
21
  end
22
22
 
23
- def create(params = nil, headers = nil, body = nil)
24
- make_request(:post, params, headers, body)
23
+ def create(params: nil, headers: nil, body: nil, payload: nil)
24
+ make_request(:post,
25
+ params: params,
26
+ headers: headers,
27
+ body: body,
28
+ payload: payload)
25
29
  end
26
30
 
27
- def update(params = nil, headers = nil, body = nil)
28
- make_request(:patch, params, headers, body)
31
+ def update(params: nil, headers: nil, body: nil, payload: nil)
32
+ make_request(:patch,
33
+ params: params,
34
+ headers: headers,
35
+ body: body,
36
+ payload: payload)
29
37
  end
30
38
 
31
- def retrieve(params = nil, headers = nil)
32
- make_request(:get, params, headers)
39
+ def retrieve(params: nil, headers: nil)
40
+ make_request(:get, params: params, headers: headers)
33
41
  end
34
42
 
35
- def delete(params = nil, headers = nil)
36
- make_request(:delete, params, headers)
43
+ def delete(params: nil, headers: nil)
44
+ make_request(:delete, params: params, headers: headers)
37
45
  end
38
46
 
39
47
  protected
@@ -43,17 +51,23 @@ module CartoDB
43
51
  end
44
52
 
45
53
  def client
46
- Faraday.new(url: url) do |client|
54
+ Faraday.new do |client|
47
55
  client.params['api_key'] = api_key
48
56
  client.response :raise_error
57
+ client.request :multipart
58
+ client.request :url_encoded
49
59
  client.adapter Faraday.default_adapter
50
60
  end
51
61
  end
52
62
 
53
- def make_request(method, params = nil, headers = nil, body = nil)
63
+ def make_request(method, params: nil, headers: nil, body: nil, payload: nil)
54
64
  begin
55
- response = client.send(method) do |request|
56
- configure_request(request, params, headers, body)
65
+ response = client.send(method, url, payload) do |request|
66
+ configure_request(request,
67
+ params: params,
68
+ headers: headers,
69
+ body: body,
70
+ payload: payload)
57
71
  end
58
72
  rescue => e
59
73
  rescue_error(e)
@@ -61,9 +75,9 @@ module CartoDB
61
75
  parse(response.body)
62
76
  end
63
77
 
64
- def configure_request(request, params = nil, headers = nil, body = nil)
78
+ def configure_request(request, params: nil, headers: nil, body: nil, payload: nil)
65
79
  request.params.merge!(params) if params
66
- request.headers['Content-Type'] = 'application/json'
80
+ request.headers['Content-Type'] = 'application/json' unless payload
67
81
  request.headers.merge!(headers) if headers
68
82
  request.body = body if body
69
83
  request.options.timeout = timeout
@@ -1,5 +1,5 @@
1
1
  module CartoDB
2
2
  module Api
3
- VERSION = '0.0.2'
3
+ VERSION = '0.1.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.0.2
4
+ version: 0.1.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-07 00:00:00.000000000 Z
11
+ date: 2015-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler