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

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

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

  request_xml = build_request_xml( data )

  @http = Net::HTTP.new( base_domain, 443 )
  @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 )

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

Instance Attribute Details

#ticketObject

Returns the value of attribute ticket.



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

def ticket
  @ticket
end

Instance Method Details

#add_record(db_id, new_values) ⇒ Object



98
99
100
101
102
103
# File 'lib/quickbase.rb', line 98

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



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/quickbase.rb', line 128

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



112
113
114
115
116
# File 'lib/quickbase.rb', line 112

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

  get_tag_value( result, :rid ).to_s == record_id.to_s
end

#do_query(db_id, options) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/quickbase.rb', line 68

def do_query( db_id, options )
  # Define the query format
  if !options[ :fmt ]
    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] )

  # Empty clist now loads all columns instead of "default"
  if options[ :clist ].to_s.empty?
    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|
      json_record[ field['id'] ] = field.text
    end

    return_json << json_record
  end

  return_json
end

#do_query_count(db_id, query) ⇒ Object



52
53
54
55
# File 'lib/quickbase.rb', line 52

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



105
106
107
108
109
110
# File 'lib/quickbase.rb', line 105

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_s == record_id.to_s
end

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



57
58
59
60
61
62
63
64
65
66
# File 'lib/quickbase.rb', line 57

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_schema(db_id) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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
# File 'lib/quickbase.rb', line 152

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, data_array, columns) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/quickbase.rb', line 144

def import_from_csv( db_id, data_array, columns )
  columns = normalize_list( columns )
  xml = build_csv_xml( data_array, columns )

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

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



118
119
120
121
122
123
124
125
126
# File 'lib/quickbase.rb', line 118

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