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.



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

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.



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

def ticket
  @ticket
end

Instance Method Details

#add_record(db_id, new_values) ⇒ Object



86
87
88
89
90
91
# File 'lib/quickbase.rb', line 86

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

#delete_record(db_id, record_id) ⇒ Object



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

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/quickbase.rb', line 56

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



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

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



93
94
95
96
97
98
# File 'lib/quickbase.rb', line 93

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

#get_schema(db_id) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
# File 'lib/quickbase.rb', line 124

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
  end

  schema_hash
end

#import_from_csv(db_id, data_array, columns) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/quickbase.rb', line 116

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



106
107
108
109
110
111
112
113
114
# File 'lib/quickbase.rb', line 106

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