Class: Runoff::SkypeDataFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/runoff/skype_data_format.rb

Overview

Defines methods to hide the specific format that is used in the Skype database.

Instance Method Summary collapse

Instance Method Details

#build_entry(fields) ⇒ Object

Public: Provides a filename and the data that should be written to the file.

fields - an array representing a single entry in the database.

Examples

build_entry {
  chatname: "#john/$doe;1243435",
  from_dispname: "John",
  body_xml: "Lorem ipsum",
  timestamp: 12435463
} # => { filename: john-doe.txt, content: "[2013-12-27 12:23:43] John: Lorem ipsum" }

Returns a hash with “filename” and “content” keys.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/runoff/skype_data_format.rb', line 41

def build_entry(fields)
  chatname = fields[:chatname]
  username = fields[:from_dispname]
  message  = parse_xml fields[:body_xml]
  datetime = Time.at(fields[:timestamp]).strftime "%Y-%m-%d %H:%M:%S"

  {
    filename: get_filename(chatname),
    content: "[#{datetime}] #{username}: #{message}\n"
  }
end

#denormalize(dispname) ⇒ Object

Public: Converts a string that is similar to the chat title stored

in the Skype database.

dispname - a string that is displayed to the user as a chat title.

Examples

denormalize "john-doe"
# => "#john/$doe;"

Returns a string that can be used to query Skype database.



81
82
83
84
85
# File 'lib/runoff/skype_data_format.rb', line 81

def denormalize(dispname)
  parts = dispname.split '-'

  parts.count == 2 ? "##{parts[0]}/$#{parts[1]};" : "##{parts[0]}/$;"
end

#get_schemaObject

Public: Defines what kind of information can be queried from the database.

Example

get_schema do |table, columns|
  # SELSECT *columns FROM table
end

get_schema
# => { table: :Messages, columns: [:chatname, :timestamp, :from_dispname, :body_xml] }

Returns a hash with keys “table” and “columns” if no block is given.



16
17
18
19
20
21
22
23
24
25
# File 'lib/runoff/skype_data_format.rb', line 16

def get_schema
  if block_given?
    yield :Messages, [:chatname, :timestamp, :from_dispname, :body_xml]
  else
    return {
      table: :Messages,
      columns: [:chatname, :timestamp, :from_dispname, :body_xml]
    }
  end
end

#normalize(chatname) ⇒ Object

Public: Parse a into a human readable format.

chatname - a string that must be normalized.

Examples

normalize "#john/$doe;2354657"
# => john-doe

Returns a string without unnecessary characters.



63
64
65
66
67
68
# File 'lib/runoff/skype_data_format.rb', line 63

def normalize(chatname)
  pattern = /^#(.+)\/\$(.+)?;.*$/
  initiator, respondent = chatname.match(pattern).captures

  "#{initiator}-#{respondent}".gsub(/(^-+|-+$)/, '')
end