Class: Eagleplatform::Record
- Inherits:
-
Object
- Object
- Eagleplatform::Record
- Defined in:
- lib/eagleplatform/record.rb
Overview
Records
Class Method Summary collapse
-
.delete(id) ⇒ String
Delete record from Eagleplatform.
-
.find(id) ⇒ Eagleplaform::Record
Find Record by ID # @raise [ArgumentError] id must be numeric.
-
.statistics(args = {}) ⇒ Array
Get all records statistics Date format is - ‘dd.mm.yyyy’.
-
.update(args = {}) ⇒ Hash
Update record on Eagleplatform.
-
.upload_from_ftp(args) ⇒ Eagleplatform::Record
Create record and upload file form ftp server.
-
.upload_from_http(args) ⇒ Eagleplatform::Record
Create record and upload file form http server.
Instance Method Summary collapse
-
#delete ⇒ String
Delete record from Eagleplaform.
-
#statistics(args = {}) ⇒ Array
Get current record statistics Date format is - ‘dd.mm.yyyy’.
-
#update ⇒ Eagleplatform::Record
Update record on Eagleplatform.
Class Method Details
.delete(id) ⇒ String
Delete record from Eagleplatform
89 90 91 92 93 94 |
# File 'lib/eagleplatform/record.rb', line 89 def self.delete(id) raise ArgumentError, 'id must be numeric' unless id.is_a? Numeric api_method = {method: Methods::RECORD_DELETE[:method], path: Methods::RECORD_DELETE[:path].gsub(':id',id.to_s)} Eagleplatform.call_api(api_method) == "ok" ? "Record id: '#{id}' is deleted" : (raise "Can't delete record") end |
.find(id) ⇒ Eagleplaform::Record
Find Record by ID # @raise [ArgumentError] id must be numeric
14 15 16 17 18 19 20 21 |
# File 'lib/eagleplatform/record.rb', line 14 def self.find(id) raise ArgumentError, 'id must be numeric' unless id.is_a? Numeric api_method = {method: Methods::RECORD_GET_INFO[:method], path: Methods::RECORD_GET_INFO[:path].gsub(':id',id.to_s)} result = Eagleplatform.call_api(api_method).first[1]. rec = self.new rec.each_pair { |k,v| rec[k] = result[k] } end |
.statistics(args = {}) ⇒ Array
Get all records statistics
Date format is - 'dd.mm.yyyy'
162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/eagleplatform/record.rb', line 162 def self.statistics(args = {}) params = { date_from: args[:date_from] || (Time.now - 1.day).strftime('%d.%m.%Y'), date_to: args[:date_to] || Time.now.strftime('%d.%m.%Y') } raise ArgumentError, "Wrong 'date_from' format. Must be 'dd.mm.yyyy'" unless DATE_FORMAT =~ params[:date_from] raise ArgumentError, "Wrong 'date_to' format. Must be 'dd.mm.yyyy'" unless DATE_FORMAT =~ params[:date_from] raise ArgumentError, "date_from: #{params[:date_from]} > date_to: #{params[:date_from]}" \ if params[:date_from].to_date > params[:date_to].to_date params[:uniq] = 'true' if args[:uniq] == true result = Eagleplatform.call_api(Methods::RECORDS_GET_STATISTICS, params).first[1] end |
.update(args = {}) ⇒ Hash
Update record on Eagleplatform
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/eagleplatform/record.rb', line 71 def self.update(args = {}) raise ArgumentError, 'ID is blank' if args[:id].blank? raise ArgumentError, 'id must be numeric' unless args[:id].is_a? Numeric params = { record: args } api_method = {method: Methods::RECORD_UPDATE[:method], path: Methods::RECORD_UPDATE[:path].gsub(':id',args[:id].to_s)} result = Eagleplatform.call_api(api_method, params).first[1]. end |
.upload_from_ftp(args) ⇒ Eagleplatform::Record
Create record and upload file form ftp server
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/eagleplatform/record.rb', line 40 def self.upload_from_ftp(args) raise ArgumentError, "record[:name] is blank" if args[:record][:name].blank? raise ArgumentError, "ftp[:server] is blank" if args[:ftp][:server].blank? raise ArgumentError, "ftp[:file_path] is blank" if args[:ftp][:file_path].blank? params = { record: args[:record], source: { type: 'ftp', parameters: { host: args[:ftp][:server], file: args[:ftp][:file_path], username: args[:ftp][:username] || "ftp", password: args[:ftp][:password] || "" } } } result = Eagleplatform.call_api(Methods::RECORD_UPLOAD_FROM_FTP, params).first[1]. rec = self.new rec.each_pair { |k,v| rec[k] = result[k]} end |
.upload_from_http(args) ⇒ Eagleplatform::Record
Create record and upload file form http server
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/eagleplatform/record.rb', line 108 def self.upload_from_http(args) raise ArgumentError, "record[:name] is blank" if args[:record][:name].blank? raise ArgumentError, "upload_url is blank" if args[:upload_url].blank? params = { record: args[:record], source: { type: 'http', parameters: { url: args[:upload_url] } } } result = Eagleplatform.call_api(Methods::RECORD_UPLOAD_FROM_HTTP, params).first[1]. rec = self.new rec.each_pair { |k,v| rec[k] = result[k]} end |
Instance Method Details
#delete ⇒ String
Delete record from Eagleplaform
146 147 148 149 150 |
# File 'lib/eagleplatform/record.rb', line 146 def delete api_method = {method: Methods::RECORD_DELETE[:method], path: Methods::RECORD_DELETE[:path].gsub(':id',id.to_s)} Eagleplatform.call_api(api_method) == "ok" ? "Record id: '#{self.id}', name:#{self.name} is deleted" : (raise "Can't delete record") end |
#statistics(args = {}) ⇒ Array
Get current record statistics
Date format is - 'dd.mm.yyyy'
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/eagleplatform/record.rb', line 189 def statistics(args = {}) raise "self.id is blank" if self.id.blank? params = { date_from: args[:date_from] || (Time.now - 1.day).strftime('%d.%m.%Y'), date_to: args[:date_to] || Time.now.strftime('%d.%m.%Y') } params[:uniq] = 'true' if args[:uniq] == true raise ArgumentError, "Wrong 'date_from' format. Must be 'dd.mm.yyyy'" unless DATE_FORMAT =~ params[:date_from] raise ArgumentError, "Wrong 'date_to' format. Must be 'dd.mm.yyyy'" unless DATE_FORMAT =~ params[:date_from] raise ArgumentError, "date_from: #{params[:date_from]} > date_to: #{params[:date_from]}" \ if params[:date_from].to_date > params[:date_to].to_date api_method = {method: Methods::RECORD_GET_STATISTICS[:method], path: Methods::RECORD_GET_STATISTICS[:path].gsub(':id',id.to_s)} result = Eagleplatform.call_api(api_method, params).first[1] end |
#update ⇒ Eagleplatform::Record
Update record on Eagleplatform
130 131 132 133 134 135 136 137 138 |
# File 'lib/eagleplatform/record.rb', line 130 def update api_method = {method: Methods::RECORD_UPDATE[:method], path: Methods::RECORD_UPDATE[:path].gsub(':id',id.to_s)} params = {} params[:record] = self.to_hash params[:record].delete(:record_files) result = Eagleplatform.call_api(api_method, params).first[1]. self.to_hash.diff(result).keys.include?(:updated_at) ? self : 'Something wrong' end |