Class: CartoDB::Client::Connection::CartoDBConnection

Inherits:
Object
  • Object
show all
Includes:
Authorization, Utils, Helpers::SqlHelper, OAuth::RequestProxy::Typhoeus
Defined in:
lib/cartodb-rb-client/cartodb/client/connection/cartodb.rb

Constant Summary collapse

VERSION =
'v1'.freeze

Constants included from Authorization

Authorization::CRLF

Instance Method Summary collapse

Methods included from Helpers::SqlHelper

#prepare_data

Methods included from Utils

parse_json

Constructor Details

#initialize(connection_settings) ⇒ CartoDBConnection

Returns a new instance of CartoDBConnection.



12
13
14
# File 'lib/cartodb-rb-client/cartodb/client/connection/cartodb.rb', line 12

def initialize(connection_settings)
  @hydra = Typhoeus::Hydra.new(:max_concurrency => 200)
end

Instance Method Details

#add_column(table_name, column_name, column_type) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/cartodb-rb-client/cartodb/client/connection/cartodb.rb', line 85

def add_column(table_name, column_name, column_type)
  cartodb_request "tables/#{table_name}/columns",
                  :post,
                  :params => {
                    :name => column_name,
                    :type => column_type
                  }

  execute_queue
end

#change_column(table_name, old_column_name, new_column_name, column_type) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/cartodb-rb-client/cartodb/client/connection/cartodb.rb', line 103

def change_column(table_name, old_column_name, new_column_name, column_type)
  cartodb_request "tables/#{table_name}/columns/#{old_column_name}",
                  :put,
                  :params => {
                    :new_name => new_column_name,
                    :type => column_type
                  }

  execute_queue
end

#create_table(table_name = nil, schema_or_file = nil, the_geom_type = 'Point') ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cartodb-rb-client/cartodb/client/connection/cartodb.rb', line 16

def create_table(table_name = nil, schema_or_file = nil, the_geom_type = 'Point')

  params = {:name => table_name}
  params[:the_geom_type] = the_geom_type.downcase if the_geom_type.present?

  case schema_or_file

  when String

    params[:the_geom_type] = schema_or_file.downcase

    request = cartodb_request 'tables', :post, :params => params do |response|
      return Utils.parse_json(response)
    end

  when Array

    schema = schema_or_file if schema_or_file && schema_or_file.is_a?(Array)
    params[:schema] = schema.map{|s| "#{s[:name]} #{s[:type]}"}.join(', ') if schema

    request = cartodb_request 'tables', :post, :params => params do |response|
      return Utils.parse_json(response)
    end

  when File

    file = schema_or_file if schema_or_file && schema_or_file.is_a?(File)

    request = cartodb_request nil, :post, :url => '/upload', :params => {:file => file}, :multipart => true do |response|
      upload_response = Utils.parse_json(response)

      params = {:name => table_name}
      params[:url]           = generate_url upload_response[:file_uri]
      params[:the_geom_type] = the_geom_type.downcase if the_geom_type.present?

      request = cartodb_request 'tables', :post, :params => params do |response|
        return Utils.parse_json(response)
      end

    end

  else

    request = cartodb_request 'tables', :post, :params => params do |response|
      return Utils.parse_json(response)
    end

  end

  execute_queue

  request.handled_response

end

#delete_row(table_name, row_id) ⇒ Object



180
181
182
183
184
185
186
# File 'lib/cartodb-rb-client/cartodb/client/connection/cartodb.rb', line 180

def delete_row(table_name, row_id)
  query(<<-SQL
    DELETE FROM #{table_name}
    WHERE cartodb_id = #{row_id}
  SQL
  )
end

#drop_column(table_name, column_name) ⇒ Object



96
97
98
99
100
101
# File 'lib/cartodb-rb-client/cartodb/client/connection/cartodb.rb', line 96

def drop_column(table_name, column_name)
  cartodb_request "tables/#{table_name}/columns/#{column_name}",
                  :delete

  execute_queue
end

#drop_table(table_name) ⇒ Object



134
135
136
137
138
# File 'lib/cartodb-rb-client/cartodb/client/connection/cartodb.rb', line 134

def drop_table(table_name)
  cartodb_request "tables/#{table_name}", :delete

  execute_queue
end

#insert_row(table_name, row) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cartodb-rb-client/cartodb/client/connection/cartodb.rb', line 150

def insert_row(table_name, row)
  row = prepare_data(row)

  results = query(<<-SQL
    INSERT INTO #{table_name}
    (#{row.keys.join(',')})
    VALUES (#{row.values.join(',')})
    RETURNING cartodb_id as id, *;
  SQL
  )

  results.rows.first
end

#query(sql, options = {}) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/cartodb-rb-client/cartodb/client/connection/cartodb.rb', line 198

def query(sql, options = {})
  params = {:q => sql}

  uri = 'sql'
  uri_params = []

  if options && options.any?
    uri_params << "page=#{options[:page]}"                   if options[:page]
    uri_params << "rows_per_page=#{options[:rows_per_page]}" if options[:rows_per_page]
    uri << "?#{uri_params.join('&')}" if uri_params.any?
  end

  request = cartodb_request uri, :post, :params => params do |response|
    Utils.parse_json(response)
  end

  execute_queue

  request.handled_response
end

#records(table_name, options = {}) ⇒ Object



188
189
190
191
192
193
194
195
196
# File 'lib/cartodb-rb-client/cartodb/client/connection/cartodb.rb', line 188

def records(table_name, options = {})
  request = cartodb_request "tables/#{table_name}/records", :params => options.slice(:rows_per_page, :page) do |response|
    return Utils.parse_json(response)
  end

  execute_queue

  request.handled_response
end

#rename_table(old_table_name, new_table_name) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cartodb-rb-client/cartodb/client/connection/cartodb.rb', line 71

def rename_table(old_table_name, new_table_name)
  request = cartodb_request "tables/#{old_table_name}",
                            :put,
                            :params => {
                              :name => new_table_name
                            } do |response|
    return Utils.parse_json(response)
  end

  execute_queue

  request.handled_response
end

#row(table_name, row_id) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/cartodb-rb-client/cartodb/client/connection/cartodb.rb', line 140

def row(table_name, row_id)
  cartodb_request "tables/#{table_name}/records/#{row_id}" do |response|
    return Utils.parse_json(response)
  end

  execute_queue

  request.handled_response
end

#table(table_name) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/cartodb-rb-client/cartodb/client/connection/cartodb.rb', line 124

def table(table_name)
  request = cartodb_request "tables/#{table_name}" do |response|
    return Utils.parse_json(response)
  end

  execute_queue

  request.handled_response
end

#tablesObject



114
115
116
117
118
119
120
121
122
# File 'lib/cartodb-rb-client/cartodb/client/connection/cartodb.rb', line 114

def tables
  request = cartodb_request 'tables' do |response|
    return Utils.parse_json(response)
  end

  execute_queue

  request.handled_response
end

#update_row(table_name, row_id, row) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/cartodb-rb-client/cartodb/client/connection/cartodb.rb', line 164

def update_row(table_name, row_id, row)
  row = prepare_data(row)

  results = query(<<-SQL
    UPDATE #{table_name}
    SET (#{row.keys.join(',')})
    = (#{row.values.join(',')})
    WHERE cartodb_id = #{row_id}
    RETURNING cartodb_id as id, *;
  SQL
  )

  results.rows.first

end