Class: SmsBackupRenderer::ConversationPage

Inherits:
BasePage
  • Object
show all
Defined in:
lib/sms_backup_renderer/renderer.rb

Instance Attribute Summary collapse

Attributes inherited from BasePage

#assets_dir_path, #output_file_path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasePage

#asset_path, #relative_path, #render, #write

Constructor Details

#initialize(output_file_path, assets_dir_path, messages) ⇒ ConversationPage

Returns a new instance of ConversationPage.



58
59
60
61
# File 'lib/sms_backup_renderer/renderer.rb', line 58

def initialize(output_file_path, assets_dir_path, messages)
  super(output_file_path, assets_dir_path)
  @messages = messages.sort_by(&:date_time)
end

Instance Attribute Details

#messagesObject (readonly)

Returns an Array of Message instances to be shown on this page.



56
57
58
# File 'lib/sms_backup_renderer/renderer.rb', line 56

def messages
  @messages
end

Class Method Details

.build_filename(participants) ⇒ Object

Returns a filename for a conversation page for the given participants, such as ‘123456789.html’. Should always return the same name for the same list of participants. The normalized addresses are used when they are numeric, but non-numeric addresses (such as email addresses) will be hex-encoded to avoid any problems with file name restrictions.

participants - an Array of Participant instances

Returns a String filename.



110
111
112
113
114
115
116
117
118
# File 'lib/sms_backup_renderer/renderer.rb', line 110

def self.build_filename(participants)
  participants.map do |participant|
    if participant.normalized_address =~ /\A\d+\z/
      participant.normalized_address
    else
      '0x' + Digest.hexencode(participant.address)
    end
  end.sort.join('_') + '.html'
end

Instance Method Details

#message_date_time_span(message, previous_message) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sms_backup_renderer/renderer.rb', line 77

def message_date_time_span(message, previous_message)
  formatted = if previous_message && message.date_time.to_date == previous_message.date_time.to_date
    if message.date_time.to_time - previous_message.date_time.to_time < 600
      nil
    else
      message.date_time.strftime('%-I:%M%P')
    end
  else
    message.date_time.strftime('%A, %b %-d, %Y at %-I:%M%P %Z')
  end
  return '' unless formatted
  "<span class=\"message-date-time\">#{formatted}</span>"
end

#sender_span(message, previous_message) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/sms_backup_renderer/renderer.rb', line 91

def sender_span(message, previous_message)
  if previous_message &&
      message.outgoing == previous_message.outgoing &&
      (message.outgoing || message.sender.normalized_address == previous_message.sender.normalized_address)
    ''
  else
    sender = message.outgoing ? 'You' : (message.sender.name || message.sender.normalized_address)
    "<span class=\"sender\">#{ERB::Util.html_escape(sender)}</span>"
  end
end

#template_nameObject



63
64
65
# File 'lib/sms_backup_renderer/renderer.rb', line 63

def template_name
  'conversation.html.erb'
end

#titleObject



67
68
69
70
71
72
73
74
75
# File 'lib/sms_backup_renderer/renderer.rb', line 67

def title
  messages.first.participants.reject(&:owner).map do |participant|
    if participant.name
      "#{participant.name} (#{participant.normalized_address})"
    else
      participant.normalized_address
    end
  end.sort.join(', ')
end