Class: Runoff::Adapters::JsonAdapter

Inherits:
Adapter
  • Object
show all
Defined in:
lib/runoff/adapters/json_adapter.rb

Constant Summary collapse

ENTRY_FORMAT =

Public: A format String used to build a single entry.

'{ "date": "%s", "user": "%s", "message": %s }'

Instance Method Summary collapse

Instance Method Details

#build_entry(row) ⇒ Object

Public: Builds a single entry.

row - An Array containing a single row of data from the database.

Examples

build_entry { chatname: "#first_user/$second_user;d3d86c6b0e3b8320" ... }
# => "{ "date": "2014-04-18 20:20:12", "user": "first_user", "message": "This is a text" }"


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/runoff/adapters/json_adapter.rb', line 17

def build_entry(row)
  formated_data = []

  # NOTE: The first column in the array is used for the grouping by id and
  # the second is used for the filename.
  Runoff::COLUMNS[2..-1].each do |column|
    formated_data << send("format_#{column}", row[column])
  end

  ENTRY_FORMAT % formated_data
end

#format_file_content(buffer) ⇒ Object

Public: Formats the provided data buffer so that it could be writter to

a JSON file.

buffer - An Array containing all the chat entries.

Returns a String



66
67
68
69
70
# File 'lib/runoff/adapters/json_adapter.rb', line 66

def format_file_content(buffer)
  content = buffer.join(",\n")

  "[#{content}]"
end

#get_file_name(chatname) ⇒ Object

Public: returns a file name.

chatname - A String with a Skype chatname

Examples

get_file_name "#first_user/$second_user;d3d86c6b0e3b8320"
# => first_user_second_user.json

Returns a valid file name.



39
40
41
# File 'lib/runoff/adapters/json_adapter.rb', line 39

def get_file_name(chatname)
  parse_chatname(chatname) + '.json'
end

#parse_chatname(raw_chatname) ⇒ Object

Public: Parses a chatname into a human readable name.

raw_chatname - A String with a Skype chatname.

Examples

parse_chatname "#first_user/$second_user;d3d86c6b0e3b8320"
# => first_user_second_user

Returns a valid name.



53
54
55
56
57
58
# File 'lib/runoff/adapters/json_adapter.rb', line 53

def parse_chatname(raw_chatname)
  pattern = /^#(.*)\/\$(.*);.*$/
  parts = raw_chatname.match(pattern).captures

  parts.reject(&:empty?).join('_')
end