Class: AdvantageQuickbase::API
- Inherits:
-
Object
- Object
- AdvantageQuickbase::API
- Defined in:
- lib/user.rb,
lib/table.rb,
lib/quickbase.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#app_token ⇒ Object
Returns the value of attribute app_token.
-
#ticket ⇒ Object
Returns the value of attribute ticket.
Instance Method Summary collapse
- #add_record(db_id, new_values) ⇒ Object
- #create_app_token(db_id, description, page_token) ⇒ Object
- #delete_record(db_id, record_id) ⇒ Object
- #do_query(db_id, options) ⇒ Object
- #do_query_count(db_id, query) ⇒ Object
- #edit_record(db_id, record_id, new_values) ⇒ Object
- #find(db_id, rid, options = {}) ⇒ Object
- #get_schema(db_id) ⇒ Object
- #import_from_csv(db_id, data_array, columns) ⇒ Object
-
#initialize(domain, username, password, app_token = nil, ticket = nil) ⇒ API
constructor
A new instance of API.
- #purge_records(db_id, options = {}) ⇒ Object
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 51 52 |
# File 'lib/quickbase.rb', line 18 def initialize( domain, username, password, app_token=nil, ticket=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 } 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
#app_token ⇒ Object
Returns the value of attribute app_token.
13 14 15 |
# File 'lib/quickbase.rb', line 13 def app_token @app_token end |
#ticket ⇒ Object
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
100 101 102 103 104 105 |
# File 'lib/quickbase.rb', line 100 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
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/quickbase.rb', line 130 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
114 115 116 117 118 |
# File 'lib/quickbase.rb', line 114 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
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 97 98 |
# File 'lib/quickbase.rb', line 70 def do_query( db_id, ) # Define the query format if ![ :fmt ] [ :fmt ] = 'structured' end # Normalize the field list variables into period-separated strings [ :clist ] = normalize_list( [:clist] ) [ :slist ] = normalize_list( [:slist] ) # Empty clist now loads all columns instead of "default" if [ :clist ].to_s.empty? [ :clist ] = 'a' end result = send_request( :doQuery, db_id, ) 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
54 55 56 57 |
# File 'lib/quickbase.rb', line 54 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
107 108 109 110 111 112 |
# File 'lib/quickbase.rb', line 107 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
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/quickbase.rb', line 59 def find( db_id, rid, ={} ) [:query] = "{'3'.EX.'#{rid}'}" records = self.do_query( db_id, ) if records.length > 0 return records.first else return {} end end |
#get_schema(db_id) ⇒ Object
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 238 239 |
# File 'lib/quickbase.rb', line 154 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
146 147 148 149 150 151 152 |
# File 'lib/quickbase.rb', line 146 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
120 121 122 123 124 125 126 127 128 |
# File 'lib/quickbase.rb', line 120 def purge_records ( db_id, ={} ) if .length == 0 [ :query ] = '' end result = send_request( :purgeRecords, db_id, ) get_tag_value( result, :num_records_deleted ).to_s end |