Class: AdvantageQuickbase::API

Inherits:
Object
  • Object
show all
Includes:
Table, User
Defined in:
lib/user.rb,
lib/table.rb,
lib/quickbase.rb

Defined Under Namespace

Modules: Table, User

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Table

#find_db_by_name, #get_db_info, #get_key, #get_role_info, #get_users_access

Methods included from User

#add_user_to_role, #change_user_role, #deactivate_users, #deny_users, #get_app_access, #get_page_token, #get_user_ids, #get_user_info, #get_user_role, #provision_user, #reactivate_users, #remove_access, #remove_acct_management, #remove_user_from_role, #undeny_users

Constructor Details

#initialize(domain, username, password, app_token = nil, ticket = nil, user_token = nil) ⇒ API

Returns a new instance of API.



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
# File 'lib/quickbase.rb', line 18

def initialize( domain, username, password, app_token=nil, ticket=nil, user_token=nil)
  @domain = domain
  @app_token = app_token if app_token

  if username && password #authenticate with username/password
    data = {
      username: username,
      password: password,
      apptoken: app_token
    }
  elsif ticket #authenticate with existing ticket
    @ticket = ticket if ticket

    data = {}
  elsif user_token
    @user_token = user_token

    data = {}
  end

  request_xml = build_request_xml( data )

  @http = Net::HTTP.new( base_domain, 443 )
  @http.read_timeout = 360
  @http.use_ssl = true
  @http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  url = build_request_url( :authenticate )
  headers = build_request_headers( :authenticate, request_xml )

  result = @http.post( url, request_xml, headers )
  parsed_result = parse_xml( result.body )

  if !@user_token
    ticket = get_tag_value( parsed_result, :ticket )
    if ticket
      @ticket = ticket
    else
      raise "Connection Failed\n\n#{result.body}"
    end
  end
end

Instance Attribute Details

#app_tokenObject

Returns the value of attribute app_token.



13
14
15
# File 'lib/quickbase.rb', line 13

def app_token
  @app_token
end

#ticketObject

Returns the value of attribute ticket.



13
14
15
# File 'lib/quickbase.rb', line 13

def ticket
  @ticket
end

#user_tokenObject

Returns the value of attribute user_token.



13
14
15
# File 'lib/quickbase.rb', line 13

def user_token
  @user_token
end

Instance Method Details

#add_record(db_id, new_values) ⇒ Object



135
136
137
138
139
140
# File 'lib/quickbase.rb', line 135

def add_record( db_id, new_values )
  xml = build_update_xml( new_values )
  result = send_request( :addRecord, db_id, nil, xml )

  get_tag_value( result, :rid )
end

#create_app_token(db_id, description, page_token) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/quickbase.rb', line 165

def create_app_token(db_id, description, page_token)
  url = "https://#{base_domain}/db/main?a=QBI_CreateDeveloperKey"

  result = send_quickbase_ui_action(url)
  result = parse_xml( result.body )

  app_token = get_tag_value(result, "devkey")

  url = URI::encode("https://#{base_domain}/db/#{db_id}?a=QBI_AddApplicationDeveloperKey&devKey=#{app_token}&keydescription=#{description}&keyType=P&PageToken=#{page_token}")

  result = send_quickbase_ui_action(url)
  result = parse_xml( result.body )

  app_token
end

#delete_record(db_id, record_id) ⇒ Object



149
150
151
152
153
# File 'lib/quickbase.rb', line 149

def delete_record( db_id, record_id )
  result = send_request( :deleteRecord, db_id, {rid: record_id} )

  get_tag_value( result, :rid ).to_i > 0
end

#do_query(db_id, options) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/quickbase.rb', line 77

def do_query( db_id, options )
  # Define the query format
  if options.has_key?( :fmt ) && options[:fmt].to_s.strip.empty?
    options.delete :fmt
  else
    options[ :fmt ] = 'structured'
  end

  # Normalize the field list variables into period-separated strings
  options[ :clist ] = normalize_list( options[:clist] )
  options[ :slist ] = normalize_list( options[:slist] )

  # nil clist loads all columns instead of "default"
  if options[ :clist ].nil?
    options[ :clist ] = 'a'
  end

  result = send_request( :doQuery, db_id, options )

  return_json = []
  result.css( 'record' ).each do |record|
    json_record = {}

    record.css( 'f' ).each do |field|
      if options.has_key? :fmt
        # File attachment fields shuold return a hash with keys :filename and :url
        if !field.css("url").text.empty?
          fieldname = Nokogiri::XML.parse(field.to_html.gsub(/<url.*?<\/url>/, "")).text
          value = { filename: fieldname, url: field.css("url").text }
        else
          value = field.text
        end

        json_record[ field['id'] ] = value
      else
        record.element_children.each do |field|
          json_record[ field.node_name ] = value
        end
      end
    end

    return_json << json_record
  end

  return_json
end

#do_query_count(db_id, query) ⇒ Object



61
62
63
64
# File 'lib/quickbase.rb', line 61

def do_query_count( db_id, query )
  result = send_request( :doQueryCount, db_id, {query: query} )
  get_tag_value( result, :nummatches ).to_i
end

#edit_record(db_id, record_id, new_values) ⇒ Object



142
143
144
145
146
147
# File 'lib/quickbase.rb', line 142

def edit_record( db_id, record_id, new_values )
  xml = build_update_xml( new_values, record_id )
  result = send_request( :editRecord, db_id, nil, xml )

  get_tag_value( result, :rid ).to_i > 0
end

#find(db_id, rid, options = {}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/quickbase.rb', line 66

def find( db_id, rid, options={} )
  options[:query] = "{'3'.EX.'#{rid}'}"
  records = self.do_query( db_id, options )

  if records.length > 0
    return records.first
  else
    return {}
  end
end

#get_db_var(app_id, variable_name) ⇒ Object



124
125
126
127
128
# File 'lib/quickbase.rb', line 124

def get_db_var( app_id, variable_name )
	result = send_request( :GetDBVar, app_id, { varname: variable_name } )

	get_tag_value( result, :value )
end

#get_schema(db_id) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/quickbase.rb', line 197

def get_schema( db_id )
  schema_hash = {}
  result = send_request( :getSchema, db_id, {} )

  # Get the table data
  schema_hash[ :app_id ] = get_tag_value( result, 'app_id' )
  schema_hash[ :table_id ] = get_tag_value( result, 'table_id' )
  schema_hash[ :name ] = get_tag_value( result, 'name' )

  if schema_hash[ :app_id ] == schema_hash[ :table_id ]
    # App Mode
    schema_hash[ :mode ] = 'app'

    schema_hash[ :tables ] = {}
    tables = result.css( 'chdbid' )
    tables.each do |table|
      table_hash = {
        dbid: table.text,
        name: table.attributes['name'].to_s
      }

      table_hash[ :name ].gsub!( /^_dbid_/, '' )
      table_hash[ :name ].gsub!( '_', ' ' )
      table_hash[ :name ].capitalize!

      schema_hash[ :tables ][ table_hash[:dbid] ] = table_hash
    end
  else
    # Table mode
    schema_hash[ :mode ] = 'table'

    # Parse the field data
    schema_hash[ :fields ] = {}

    fields = result.css( 'field' )

    fields.each do |field|
      field_hash = {
        id: field.attributes[ 'id' ].to_s,
        data_type: field.attributes[ 'field_type' ].to_s,
        base_type: field.attributes[ 'base_type' ].to_s,
        name: get_tag_value( field, 'label' ),
        required: get_tag_value( field, 'required' ).to_i == 1,
        unique: get_tag_value( field, 'unique' ).to_i == 1,
      }

      # Field type (Summary, Formula, Lookup, etc) is poorly represented. Fix that.
      case field.attributes[ 'mode' ].to_s
      when 'virtual'
        field_hash[ :field_type ] = 'formula'
      when ''
        field_hash[ :field_type ] = 'normal'
      else
        field_hash[ :field_type ] = field.attributes[ 'mode' ].to_s
      end

      choices = field.css( 'choice' )
      if choices.length > 0
        field_hash[ :choices ] = []
        choices.each do |choice|
          field_hash[ :choices ] << choice.text
        end
      end

      schema_hash[ :fields ][ field_hash[:id] ] = field_hash
    end

    #Parse the report data
    schema_hash[ :reports ] = {}
    reports = result.css( 'query' )
    reports.each do |report|
      report_hash = {
        id: report.attributes[ 'id' ].to_s,
        name: get_tag_value( report, 'qyname' ),
        type: get_tag_value( report, 'qytype' ),
        criteria: get_tag_value( report, 'qycrit' ),
        clist: get_tag_value( report, 'qyclst' ),
        slist: get_tag_value( report, 'qyslst' )
      }

      schema_hash[ :reports ][ report_hash[:id] ] = report_hash
    end
  end

  schema_hash
end

#import_from_csv(db_id, import_data, columns = nil) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/quickbase.rb', line 181

def import_from_csv( db_id, import_data, columns=nil )
  result = []
  if import_data && import_data.length > 0
    # If import_data contains hashes, use the keys as the import headers
    columns ||= import_data[ 0 ].map{ |fid, value| fid }
    columns = normalize_list( columns )

    xml = build_csv_xml( import_data, columns )

    result = send_request( :importFromCSV, db_id, nil, xml )
    result = result.css('rid').map{ |xml_node| xml_node.text.to_i }
  end

  result
end

#purge_records(db_id, options = {}) ⇒ Object



155
156
157
158
159
160
161
162
163
# File 'lib/quickbase.rb', line 155

def purge_records ( db_id, options={} )
  if options.length == 0
    options[ :query ] = ''
  end

  result = send_request( :purgeRecords, db_id, options )

  get_tag_value( result, :num_records_deleted ).to_s
end

#set_db_var(app_id, variable_name, value = nil) ⇒ Object



130
131
132
133
# File 'lib/quickbase.rb', line 130

def set_db_var( app_id, variable_name, value=nil )
	send_request( :SetDBVar, app_id, { varname: variable_name, value: value } )
	true
end