cartodb-rb-client-rails-322 0.4.3

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.
Files changed (121) hide show
  1. data/.gitignore +5 -0
  2. data/.rvmrc +1 -0
  3. data/.travis.yml +15 -0
  4. data/Gemfile +15 -0
  5. data/LICENSE +28 -0
  6. data/README.markdown +365 -0
  7. data/Rakefile +10 -0
  8. data/cartodb-rb-client.gemspec +34 -0
  9. data/lib/cartodb-rb-client.rb +18 -0
  10. data/lib/cartodb-rb-client/cartodb.rb +6 -0
  11. data/lib/cartodb-rb-client/cartodb/client.rb +4 -0
  12. data/lib/cartodb-rb-client/cartodb/client/authorization.rb +92 -0
  13. data/lib/cartodb-rb-client/cartodb/client/cache.rb +14 -0
  14. data/lib/cartodb-rb-client/cartodb/client/connection.rb +4 -0
  15. data/lib/cartodb-rb-client/cartodb/client/connection/base.rb +44 -0
  16. data/lib/cartodb-rb-client/cartodb/client/connection/cartodb.rb +280 -0
  17. data/lib/cartodb-rb-client/cartodb/client/connection/postgres.rb +255 -0
  18. data/lib/cartodb-rb-client/cartodb/client/error.rb +68 -0
  19. data/lib/cartodb-rb-client/cartodb/client/utils.rb +20 -0
  20. data/lib/cartodb-rb-client/cartodb/helpers.rb +1 -0
  21. data/lib/cartodb-rb-client/cartodb/helpers/sql_helper.rb +36 -0
  22. data/lib/cartodb-rb-client/cartodb/init.rb +30 -0
  23. data/lib/cartodb-rb-client/cartodb/libs.rb +2 -0
  24. data/lib/cartodb-rb-client/cartodb/libs/object.rb +15 -0
  25. data/lib/cartodb-rb-client/cartodb/libs/string.rb +116 -0
  26. data/lib/cartodb-rb-client/cartodb/model.rb +11 -0
  27. data/lib/cartodb-rb-client/cartodb/model/base.rb +20 -0
  28. data/lib/cartodb-rb-client/cartodb/model/constants.rb +30 -0
  29. data/lib/cartodb-rb-client/cartodb/model/defaults.rb +15 -0
  30. data/lib/cartodb-rb-client/cartodb/model/geo.rb +101 -0
  31. data/lib/cartodb-rb-client/cartodb/model/getters.rb +75 -0
  32. data/lib/cartodb-rb-client/cartodb/model/persistence.rb +69 -0
  33. data/lib/cartodb-rb-client/cartodb/model/query.rb +66 -0
  34. data/lib/cartodb-rb-client/cartodb/model/schema.rb +121 -0
  35. data/lib/cartodb-rb-client/cartodb/model/scope.rb +163 -0
  36. data/lib/cartodb-rb-client/cartodb/model/setters.rb +37 -0
  37. data/lib/cartodb-rb-client/cartodb/types.rb +2 -0
  38. data/lib/cartodb-rb-client/cartodb/types/metadata.rb +97 -0
  39. data/lib/cartodb-rb-client/cartodb/types/pg_result.rb +17 -0
  40. data/lib/cartodb-rb-client/install_utils.rb +19 -0
  41. data/lib/cartodb-rb-client/version.rb +7 -0
  42. data/run_tests.sh +6 -0
  43. data/spec/client_spec.rb +357 -0
  44. data/spec/fixtures/cassettes/CartoDB_client/should_add_and_remove_colums_in_a_previously_created_table.yml +635 -0
  45. data/spec/fixtures/cassettes/CartoDB_client/should_allow_reserved_words_in_columns_names.yml +284 -0
  46. data/spec/fixtures/cassettes/CartoDB_client/should_change_a_previously_created_column.yml +362 -0
  47. data/spec/fixtures/cassettes/CartoDB_client/should_create_a_table_and_get_its_table_definition.yml +1634 -0
  48. data/spec/fixtures/cassettes/CartoDB_client/should_create_a_table_forcing_the_schema_and_get_its_table_definition.yml +298 -0
  49. data/spec/fixtures/cassettes/CartoDB_client/should_create_a_table_from_a_csv_file.yml +2947 -0
  50. data/spec/fixtures/cassettes/CartoDB_client/should_create_a_table_with_MULTILINESTRING_type_geometry.yml +299 -0
  51. data/spec/fixtures/cassettes/CartoDB_client/should_create_a_table_with_MULTIPOLYGON_type_geometry.yml +298 -0
  52. data/spec/fixtures/cassettes/CartoDB_client/should_create_a_table_with_POINT_type_geometry.yml +580 -0
  53. data/spec/fixtures/cassettes/CartoDB_client/should_delete_a_table_s_row.yml +410 -0
  54. data/spec/fixtures/cassettes/CartoDB_client/should_drop_a_table.yml +380 -0
  55. data/spec/fixtures/cassettes/CartoDB_client/should_escape_properly_input_data_in_insert_queries.yml +295 -0
  56. data/spec/fixtures/cassettes/CartoDB_client/should_execute_a_select_query_and_return_results.yml +987 -0
  57. data/spec/fixtures/cassettes/CartoDB_client/should_get_a_table_by_its_name.yml +298 -0
  58. data/spec/fixtures/cassettes/CartoDB_client/should_import_any_kind_of_data_file.yml +6951 -0
  59. data/spec/fixtures/cassettes/CartoDB_client/should_insert_a_row_in_a_table.yml +357 -0
  60. data/spec/fixtures/cassettes/CartoDB_client/should_paginate_records.yml +3642 -0
  61. data/spec/fixtures/cassettes/CartoDB_client/should_rename_an_existing_table.yml +299 -0
  62. data/spec/fixtures/cassettes/CartoDB_client/should_return_errors_on_invalid_queries.yml +132 -0
  63. data/spec/fixtures/cassettes/CartoDB_client/should_return_nil_when_requesting_a_table_which_does_not_exists.yml +218 -0
  64. data/spec/fixtures/cassettes/CartoDB_client/should_return_user_s_table_list.yml +244 -0
  65. data/spec/fixtures/cassettes/CartoDB_client/should_update_a_row_in_a_table.yml +347 -0
  66. data/spec/fixtures/cassettes/CartoDB_model_data_methods/should_destroy_a_previously_created_record.yml +1920 -0
  67. data/spec/fixtures/cassettes/CartoDB_model_data_methods/should_initialize_attributes_of_the_model_without_persisting_them.yml +963 -0
  68. data/spec/fixtures/cassettes/CartoDB_model_data_methods/should_persist_into_cartodb_using_the_save_method.yml +1946 -0
  69. data/spec/fixtures/cassettes/CartoDB_model_data_methods/should_persist_into_cartodb_using_the_static_create_method.yml +1796 -0
  70. data/spec/fixtures/cassettes/CartoDB_model_data_methods/should_save_polygons_in_different_formats.yml +1801 -0
  71. data/spec/fixtures/cassettes/CartoDB_model_data_methods/should_update_an_existing_record.yml +2856 -0
  72. data/spec/fixtures/cassettes/CartoDB_model_metadata_methods/should_add_more_columns_if_the_table_previously_exists.yml +1509 -0
  73. data/spec/fixtures/cassettes/CartoDB_model_metadata_methods/should_contain_an_array_of_columns.yml +2007 -0
  74. data/spec/fixtures/cassettes/CartoDB_model_metadata_methods/should_create_a_table_with_custom_name_if_specified.yml +357 -0
  75. data/spec/fixtures/cassettes/CartoDB_model_metadata_methods/should_create_model_with_custom_data_types_columns.yml +565 -0
  76. data/spec/fixtures/cassettes/CartoDB_model_metadata_methods/should_create_model_with_polygon_type_geometry_columns.yml +493 -0
  77. data/spec/fixtures/cassettes/CartoDB_model_metadata_methods/should_create_the_table_in_cartodb_if_it_doesn_t_exists.yml +1048 -0
  78. data/spec/fixtures/cassettes/CartoDB_model_metadata_methods/should_have_a_valid_CartoDB_Client_instance_as_a_connection_object.yml +971 -0
  79. data/spec/fixtures/cassettes/CartoDB_model_metadata_methods/should_have_a_valid_table_name.yml +970 -0
  80. data/spec/fixtures/cassettes/CartoDB_model_metadata_methods/should_return_only_data_columns.yml +1096 -0
  81. data/spec/fixtures/cassettes/CartoDB_model_scopes/should_allow_to_select_the_specified_fiels.yml +25828 -0
  82. data/spec/fixtures/cassettes/CartoDB_model_scopes/should_count_all_records.yml +22401 -0
  83. data/spec/fixtures/cassettes/CartoDB_model_scopes/should_find_a_record_by_its_id.yml +21852 -0
  84. data/spec/fixtures/cassettes/CartoDB_model_scopes/should_order_results.yml +23701 -0
  85. data/spec/fixtures/cassettes/CartoDB_model_scopes/should_paginate_results.yml +35644 -0
  86. data/spec/fixtures/cassettes/CartoDB_model_scopes/should_return_all_records_paginated.yml +23699 -0
  87. data/spec/fixtures/cassettes/CartoDB_model_scopes/should_search_records_by_certain_filters.yml +7080 -0
  88. data/spec/fixtures/cassettes/cartodb_spec_models.yml +3409 -0
  89. data/spec/fixtures/cassettes/clean_tables.yml +224 -0
  90. data/spec/model/data_spec.rb +157 -0
  91. data/spec/model/metadata_spec.rb +124 -0
  92. data/spec/model/scopes_spec.rb +171 -0
  93. data/spec/model_specs_helper.rb +2 -0
  94. data/spec/spec_helper.rb +54 -0
  95. data/spec/support/cartodb_config.yml +11 -0
  96. data/spec/support/cartodb_config.yml.sample +16 -0
  97. data/spec/support/cartodb_factories.rb +33 -0
  98. data/spec/support/cartodb_helpers.rb +14 -0
  99. data/spec/support/cartodb_models.rb +29 -0
  100. data/spec/support/data/110m-glaciated-areas.zip +0 -0
  101. data/spec/support/data/CartoDB_csv_export.zip +0 -0
  102. data/spec/support/data/CartoDB_shp_export.zip +0 -0
  103. data/spec/support/data/rmnp.kml +51 -0
  104. data/spec/support/data/states.kml.zip +0 -0
  105. data/spec/support/database.yml +5 -0
  106. data/spec/support/shp/cereal.dbf +0 -0
  107. data/spec/support/shp/cereal.shp +0 -0
  108. data/spec/support/shp/cereal.shx +0 -0
  109. data/spec/support/shp/cereal.zip +0 -0
  110. data/spec/support/shp/parcelas.dbf +0 -0
  111. data/spec/support/shp/parcelas.shp +0 -0
  112. data/spec/support/shp/parcelas.shx +0 -0
  113. data/spec/support/shp/parcelas.zip +0 -0
  114. data/spec/support/shp/zonas.dbf +0 -0
  115. data/spec/support/shp/zonas.shp +0 -0
  116. data/spec/support/shp/zonas.shx +0 -0
  117. data/spec/support/shp/zonas.zip +0 -0
  118. data/spec/support/whs_features.csv +315 -0
  119. data/spec/support/whs_features.csv.zip +0 -0
  120. data/spec/support/whs_features_temp.csv +315 -0
  121. metadata +400 -0
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'ostruct'
5
+ require 'oauth'
6
+ require 'typhoeus'
7
+ require 'mime/types'
8
+ require 'active_support/core_ext/hash/slice'
9
+ require 'active_support/time_with_zone'
10
+ require 'rgeo'
11
+ require 'rgeo/geo_json'
12
+ require 'pg'
13
+ require 'json/ext'
14
+
15
+ require 'cartodb-rb-client/cartodb'
16
+
17
+ OpenSSL::SSL.send :remove_const, :VERIFY_PEER
18
+ OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
@@ -0,0 +1,6 @@
1
+ require 'cartodb-rb-client/cartodb/init'
2
+ require 'cartodb-rb-client/cartodb/libs'
3
+ require 'cartodb-rb-client/cartodb/helpers'
4
+ require 'cartodb-rb-client/cartodb/client'
5
+ require 'cartodb-rb-client/cartodb/types'
6
+ require 'cartodb-rb-client/cartodb/model'
@@ -0,0 +1,4 @@
1
+ require 'cartodb-rb-client/cartodb/client/error'
2
+ require 'cartodb-rb-client/cartodb/client/authorization'
3
+ require 'cartodb-rb-client/cartodb/client/utils'
4
+ require 'cartodb-rb-client/cartodb/client/connection'
@@ -0,0 +1,92 @@
1
+ require 'oauth/request_proxy/typhoeus_request'
2
+
3
+ module CartoDB
4
+ module Client
5
+ module Authorization
6
+
7
+ CRLF = "\r\n"
8
+
9
+ def signed_request(request_uri, arguments)
10
+ arguments[:disable_ssl_peer_verification] = true
11
+
12
+ if CartoDB::Settings['api_key'].present?
13
+ arguments[:params][:api_key] = CartoDB::Settings['api_key']
14
+ end
15
+
16
+ request = Typhoeus::Request.new(request_uri, arguments)
17
+
18
+ request = as_multipart(request, arguments[:params]) if arguments[:multipart] == true
19
+
20
+ unless arguments[:params][:api_key].present?
21
+ request.headers.merge!({"Authorization" => oauth_helper(request, request_uri).header})
22
+ end
23
+
24
+ request
25
+ end
26
+ private :signed_request
27
+
28
+ def access_token
29
+ return @access_token if @access_token
30
+
31
+ @access_token ||= if CartoDB::Settings['oauth_access_token'] && CartoDB::Settings['oauth_access_token_secret']
32
+ OAuth::AccessToken.new(oauth_consumer, CartoDB::Settings['oauth_access_token'], CartoDB::Settings['oauth_access_token_secret'])
33
+ elsif CartoDB::Settings['username'] && CartoDB::Settings['password']
34
+
35
+ x_auth_params = {
36
+ :x_auth_mode => :client_auth,
37
+ :x_auth_username => CartoDB::Settings['username'],
38
+ :x_auth_password => CartoDB::Settings['password']
39
+ }
40
+ response = oauth_consumer.request(:post, oauth_consumer.access_token_url, nil, {}, x_auth_params)
41
+
42
+ values = response.body.split('&').inject({}) { |h,v| h[v.split("=")[0]] = v.split("=")[1]; h }
43
+
44
+ OAuth::AccessToken.new(oauth_consumer, values["oauth_token"], values["oauth_token_secret"])
45
+ else
46
+ nil
47
+ end
48
+ end
49
+ private :access_token
50
+
51
+ def oauth_params
52
+ {:consumer => oauth_consumer, :token => access_token}
53
+ end
54
+ private :oauth_params
55
+
56
+ def oauth_consumer
57
+ @oauth_consumer ||= OAuth::Consumer.new(CartoDB::Settings['oauth_key'], CartoDB::Settings['oauth_secret'], :site => CartoDB::Settings['host'])
58
+ end
59
+ private :oauth_consumer
60
+
61
+ def oauth_helper(request, request_uri)
62
+ OAuth::Client::Helper.new(request, oauth_params.merge(:request_uri => request_uri))
63
+ end
64
+ private :oauth_helper
65
+
66
+ def as_multipart(request, params)
67
+
68
+ boundary = Time.now.to_i.to_s(16)
69
+ request.headers["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
70
+ body = ""
71
+ params.each do |key,value|
72
+ esc_key = CGI.escape(key.to_s)
73
+ body << "--#{boundary}#{CRLF}"
74
+ if value.respond_to?(:read)
75
+ body << "Content-Disposition: form-data; name=\"#{esc_key}\"; filename=\"#{File.basename(value.path)}\"#{CRLF}"
76
+ body << "Content-Type: application/octet-stream#{CRLF*2}"
77
+ body << value.read.force_encoding('utf-8')
78
+ else
79
+ body << "Content-Disposition: form-data; name=\"#{esc_key}\"#{CRLF*2}#{value}"
80
+ end
81
+ body << CRLF
82
+ end
83
+ body << "--#{boundary}--#{CRLF*2}"
84
+ request.body = body
85
+ request.params = {}
86
+ request
87
+ end
88
+ private :as_multipart
89
+
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,14 @@
1
+ module CartoDB
2
+ module Client
3
+ class Cache < Hash
4
+
5
+ def get(key)
6
+ self[key]
7
+ end
8
+
9
+ def set(key, object, timeout = 0)
10
+ self[key] = object
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ require 'cartodb-rb-client/cartodb/client/connection/base'
2
+ require 'cartodb-rb-client/cartodb/client/connection/cartodb'
3
+ require 'cartodb-rb-client/cartodb/client/connection/postgres'
4
+
@@ -0,0 +1,44 @@
1
+ module CartoDB
2
+ module Client
3
+ module Connection
4
+ class Base
5
+
6
+ attr_reader :active_connection
7
+
8
+ def initialize
9
+ raise Exception.new 'CartoDB settings not found' if CartoDB::Settings.nil?
10
+
11
+ @active_connection = if cartodb_settings?
12
+ CartoDB::Client::Connection::CartoDBConnection.new(settings)
13
+ elsif postgresql_settings?
14
+ CartoDB::Client::Connection::PostgreSQL.new(settings)
15
+ end
16
+ end
17
+
18
+ def method_missing(method, *args, &block)
19
+ if @active_connection.respond_to?(method)
20
+ @active_connection.send(method, *args, &block)
21
+ else
22
+ super
23
+ end
24
+ end
25
+
26
+ def settings
27
+ CartoDB::Settings || {}
28
+ end
29
+ private :settings
30
+
31
+ def cartodb_settings?
32
+ settings.has_key?('api_key') || (settings.has_key?('oauth_key') && settings.has_key?('oauth_secret'))
33
+ end
34
+ private :cartodb_settings?
35
+
36
+ def postgresql_settings?
37
+ settings.has_key?('host') && settings.has_key?('user') && settings.has_key?('password') && settings.has_key?('database')
38
+ end
39
+ private :postgresql_settings?
40
+
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,280 @@
1
+ module CartoDB
2
+ module Client
3
+ module Connection
4
+ class CartoDBConnection
5
+ include OAuth::RequestProxy::Typhoeus
6
+ include CartoDB::Client::Authorization
7
+ include CartoDB::Client::Utils
8
+ include CartoDB::Helpers::SqlHelper
9
+
10
+ VERSION = 'v1'.freeze
11
+
12
+ def initialize(connection_settings)
13
+ @hydra = Typhoeus::Hydra.new(:max_concurrency => 200)
14
+ end
15
+
16
+ def create_table(table_name = nil, schema_or_file = nil, the_geom_type = 'Point')
17
+
18
+ params = {:name => table_name}
19
+ params[:the_geom_type] = the_geom_type.downcase if the_geom_type.present?
20
+
21
+ case schema_or_file
22
+
23
+ when String
24
+
25
+ params[:the_geom_type] = schema_or_file.downcase
26
+
27
+ request = cartodb_request 'tables', :post, :params => params do |response|
28
+ return Utils.parse_json(response)
29
+ end
30
+
31
+ when Array
32
+
33
+ schema = schema_or_file if schema_or_file && schema_or_file.is_a?(Array)
34
+ params[:schema] = schema.map{|s| "#{s[:name]} #{s[:type]}"}.join(', ') if schema
35
+
36
+ request = cartodb_request 'tables', :post, :params => params do |response|
37
+ return Utils.parse_json(response)
38
+ end
39
+
40
+ when File
41
+
42
+ file = schema_or_file if schema_or_file && schema_or_file.is_a?(File)
43
+
44
+ request = cartodb_request nil, :post, :url => '/upload', :params => {:file => file}, :multipart => true do |response|
45
+ upload_response = Utils.parse_json(response)
46
+
47
+ params = {:name => table_name}
48
+ params[:url] = generate_url upload_response[:file_uri]
49
+ params[:the_geom_type] = the_geom_type.downcase if the_geom_type.present?
50
+
51
+ request = cartodb_request 'tables', :post, :params => params do |response|
52
+ return Utils.parse_json(response)
53
+ end
54
+
55
+ end
56
+
57
+ else
58
+
59
+ request = cartodb_request 'tables', :post, :params => params do |response|
60
+ return Utils.parse_json(response)
61
+ end
62
+
63
+ end
64
+
65
+ execute_queue
66
+
67
+ request.handled_response
68
+
69
+ end
70
+
71
+ def rename_table(old_table_name, new_table_name)
72
+ request = cartodb_request "tables/#{old_table_name}",
73
+ :put,
74
+ :params => {
75
+ :name => new_table_name
76
+ } do |response|
77
+ return Utils.parse_json(response)
78
+ end
79
+
80
+ execute_queue
81
+
82
+ request.handled_response
83
+ end
84
+
85
+ def add_column(table_name, column_name, column_type)
86
+ cartodb_request "tables/#{table_name}/columns",
87
+ :post,
88
+ :params => {
89
+ :name => column_name,
90
+ :type => column_type
91
+ }
92
+
93
+ execute_queue
94
+ end
95
+
96
+ def drop_column(table_name, column_name)
97
+ cartodb_request "tables/#{table_name}/columns/#{column_name}",
98
+ :delete
99
+
100
+ execute_queue
101
+ end
102
+
103
+ def change_column(table_name, old_column_name, new_column_name, column_type)
104
+ cartodb_request "tables/#{table_name}/columns/#{old_column_name}",
105
+ :put,
106
+ :params => {
107
+ :new_name => new_column_name,
108
+ :type => column_type
109
+ }
110
+
111
+ execute_queue
112
+ end
113
+
114
+ def tables
115
+ request = cartodb_request 'tables' do |response|
116
+ return Utils.parse_json(response)
117
+ end
118
+
119
+ execute_queue
120
+
121
+ request.handled_response
122
+ end
123
+
124
+ def table(table_name)
125
+ request = cartodb_request "tables/#{table_name}" do |response|
126
+ return Utils.parse_json(response)
127
+ end
128
+
129
+ execute_queue
130
+
131
+ request.handled_response
132
+ end
133
+
134
+ def drop_table(table_name)
135
+ cartodb_request "tables/#{table_name}", :delete
136
+
137
+ execute_queue
138
+ end
139
+
140
+ def row(table_name, row_id)
141
+ cartodb_request "tables/#{table_name}/records/#{row_id}" do |response|
142
+ return Utils.parse_json(response)
143
+ end
144
+
145
+ execute_queue
146
+
147
+ request.handled_response
148
+ end
149
+
150
+ def insert_row(table_name, row)
151
+ row = prepare_data(row)
152
+
153
+ results = query(<<-SQL
154
+ INSERT INTO #{table_name}
155
+ (#{row.keys.join(',')})
156
+ VALUES (#{row.values.join(',')})
157
+ RETURNING cartodb_id as id, *;
158
+ SQL
159
+ )
160
+
161
+ results.rows.first
162
+ end
163
+
164
+ def update_row(table_name, row_id, row)
165
+ row = prepare_data(row)
166
+
167
+ results = query(<<-SQL
168
+ UPDATE #{table_name}
169
+ SET (#{row.keys.join(',')})
170
+ = (#{row.values.join(',')})
171
+ WHERE cartodb_id = #{row_id}
172
+ RETURNING cartodb_id as id, *;
173
+ SQL
174
+ )
175
+
176
+ results.rows.first
177
+
178
+ end
179
+
180
+ def delete_row(table_name, row_id)
181
+ query(<<-SQL
182
+ DELETE FROM #{table_name}
183
+ WHERE cartodb_id = #{row_id}
184
+ SQL
185
+ )
186
+ end
187
+
188
+ def records(table_name, options = {})
189
+ request = cartodb_request "tables/#{table_name}/records", :params => options.slice(:rows_per_page, :page) do |response|
190
+ return Utils.parse_json(response)
191
+ end
192
+
193
+ execute_queue
194
+
195
+ request.handled_response
196
+ end
197
+
198
+ def query(sql, options = {})
199
+ params = {:q => sql}
200
+
201
+ uri = 'sql'
202
+ uri_params = []
203
+
204
+ if options && options.any?
205
+ uri_params << "page=#{options[:page]}" if options[:page]
206
+ uri_params << "rows_per_page=#{options[:rows_per_page]}" if options[:rows_per_page]
207
+ uri << "?#{uri_params.join('&')}" if uri_params.any?
208
+ end
209
+
210
+ request = cartodb_request uri, :post, :params => params do |response|
211
+ Utils.parse_json(response)
212
+ end
213
+
214
+ execute_queue
215
+
216
+ request.handled_response
217
+ end
218
+
219
+ def cartodb_request(uri, method = :get, arguments = {:params => {}}, &block)
220
+ params = arguments[:params]
221
+ if method.is_a? Hash
222
+ params = method[:params]
223
+ method = :get
224
+ end
225
+
226
+ uri = "/api/#{VERSION}/#{uri}"
227
+ url = generate_url uri
228
+ url = generate_url(arguments[:url]) if arguments[:url]
229
+
230
+ headers = {}
231
+ headers['Accept'] = MIME::Types['application/json']
232
+ headers.merge!(arguments[:headers]) if arguments[:headers]
233
+
234
+ request = signed_request(url,
235
+ :method => method,
236
+ :headers => headers,
237
+ :params => params,
238
+ :multipart => arguments[:multipart],
239
+ :cache_timeout => settings['cache_timeout'],
240
+ :verbose => settings['debug']
241
+ )
242
+
243
+ request.on_complete do |response|
244
+ if response.success?
245
+ yield(response) if block_given?
246
+ else
247
+ raise CartoDB::Client::Error.new url, method, response
248
+ end
249
+ end
250
+
251
+ enqueue request
252
+ end
253
+ private :cartodb_request
254
+
255
+ def enqueue(request)
256
+ @hydra.queue request
257
+ request
258
+ end
259
+ private :enqueue
260
+
261
+ def execute_queue
262
+ @hydra.run
263
+ end
264
+ private :execute_queue
265
+
266
+ def generate_url(uri)
267
+ uri = URI.parse("#{settings['host']}#{uri}")
268
+ "#{uri.scheme}://#{uri.host}:#{uri.port}#{uri.path}"
269
+ end
270
+ private :generate_url
271
+
272
+ def settings
273
+ CartoDB::Settings || {}
274
+ end
275
+ private :settings
276
+
277
+ end
278
+ end
279
+ end
280
+ end