Class: OpenTx::Log::File

Inherits:
Object
  • Object
show all
Defined in:
lib/opentx/log/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ File

Returns a new instance of File.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/opentx/log/file.rb', line 20

def initialize uri
  @sessions = []

  open(uri, 'r') do |file|
    session = CSV.new(file, headers: true).map { |csv| csv }

    # a little sanity checking
    raise 'No session data found' if session.empty?
    raise 'Unknown column layout' unless Session.known_rows?(session.first.headers)

    @sessions << Session.new(session)
  end
rescue => e
  raise ArgumentError, "File does not appear to be an OpenTx log (#{e})"
end

Instance Attribute Details

#sessionsArray<Session> (readonly)

Returns Sessions contained in this file.

Returns:

  • (Array<Session>)

    Sessions contained in this file



10
11
12
# File 'lib/opentx/log/file.rb', line 10

def sessions
  @sessions
end

Class Method Details

.opentx?(uri) ⇒ OpenTx::Log::File

Determines if the file at the given URI is an OpenTx log file.

Parameters:

  • uri

    URI to file to read

Returns:

  • (OpenTx::Log::File)

    loaded file if the file is an OpenTx log file, nil otherwise



16
17
18
# File 'lib/opentx/log/file.rb', line 16

def self.opentx? uri
  File.new(uri) rescue nil
end

Instance Method Details

#durationFloat

Gets the total duration of all sessions contained within.

Returns:

  • (Float)

    total duration of all sessions, in seconds



39
40
41
# File 'lib/opentx/log/file.rb', line 39

def duration
  @sessions.map(&:duration).reduce(&:+)
end