Class: Eagleplatform::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/eagleplatform/record.rb

Overview

Records

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delete(id) ⇒ String

Delete record from Eagleplatform

Examples:

Eagleplatform::Record.delete(1234)

Returns:

  • (String)

    ‘Record id: #id is deleted’ if record deleted successfully

Raises:

  • (ArgumentError)


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

Examples:

Eagleplatform::Record.find(45632)

Parameters:

  • id (Numeric)

    ID of record

Returns:

  • (Eagleplaform::Record)

    if record present

Raises:

  • (ArgumentError)


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].to_options  
  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'

Examples:

DATE_FROMAT: 'dd.mm.yyyy'
Eagleplatform::Record.statistics(date_from: '1.5.2012', date_to: '25.5.2012', uniq: 'true')

Parameters:

  • args (Hash) (defaults to: {})

    a customizable set of options

Options Hash (args):

  • :date_from (String) — default: 'yesterday'

    yesterday date

  • :date_to (String) — default: 'today'

    today date

  • :uniq (String) — default: 'false'

    unique user statistics

Returns:

  • (Array)

    return records statistics

Raises:

  • (ArgumentError)


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

Examples:

Eagleplatform::Record.update(id: 1234, name: 'Hello world', description: 'Heyy')

Parameters:

  • record (Hash)

    Hash of record fields

Returns:

  • (Hash)

    Updated record

Raises:

  • (ArgumentError)


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].to_options
end

.upload_from_ftp(args) ⇒ Eagleplatform::Record

Create record and upload file form ftp server

Examples:

record_params = { name: 'SomeRecord', description: 'Example Video' }
ftp_params = { server: 'ftp.example_server.com',file_path: '/videos/my_video.mpg',username: 'ftp_username', password: 'ftp_passowrd' }  
Eagleplatform::Record.upload_form_ftp( record: record_params, ftp: ftp_params)

Parameters:

  • args (Hash)

    the ftp options to upload video

Options Hash (args):

  • :ftp (Hash)

    server: ‘ftp_server’, file_path: ‘file_path’, username: ‘user’, password: ‘pass’

  • :record (Hash)

    name: ‘record_name’ description: ‘record_description’

Returns:

Raises:

  • (ArgumentError)


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].to_options
  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

Examples:

record_params = { name: 'SomeRecord', description: 'Example Video' }
Eagleplatform::Record.upload_form_http( record: record_params, upload_url: 'http://exapmle.com/video.mpg')

Parameters:

  • args (Hash)

    the http options to upload video

Options Hash (args):

  • :upload_url (String)
  • :record (Hash)

    name: ‘record_name’ description: ‘record_description’

Returns:

Raises:

  • (ArgumentError)


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].to_options
  rec = self.new
  rec.each_pair { |k,v| rec[k] = result[k]}
end

Instance Method Details

#deleteString

Delete record from Eagleplaform

Examples:

record = Eagleplatform::Record.find(1234)
record.delete

Returns:

  • (String)

    ‘Record id: #id, name:#Eagleplatform::Record.selfself.name is deleted’ if record deleted successfully



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'

Examples:

record = Eagleplatform::Record.find(12345)
record.statistics(date_from: '1.5.2012', date_to: '25.5.2012')

Parameters:

  • args (Hash) (defaults to: {})

    the statistics options

Options Hash (args):

  • :date_from (String) — default: 'yesterday'

    yesterday date

  • :date_to (String) — default: 'today'

    today date

  • :uniq (String) — default: 'false'

    unique user statistics

Returns:

  • (Array)

    return record statistics

Raises:

  • (ArgumentError)


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

#updateEagleplatform::Record

Update record on Eagleplatform

Examples:

record = Eagleplatform::Record.find(1234)
record.description = 'Very fun record'
record.update

Returns:



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].to_options
  self.to_hash.diff(result).keys.include?(:updated_at) ? self : 'Something wrong'
end