Class: ConnectionMailchain

Inherits:
Object
  • Object
show all
Defined in:
lib/connection/mailchain.rb

Overview

Handles the Mailchain API configuration and connection

Instance Method Summary collapse

Constructor Details

#initialize(config, config_file) ⇒ ConnectionMailchain

Initialize configs



9
10
11
12
13
# File 'lib/connection/mailchain.rb', line 9

def initialize(config, config_file)
  @config = config
  @config_file = config_file
  @api = MailchainApi.new(@config['mailchain'])
end

Instance Method Details

#addresses_by_networkObject

Returns addresses formatted by_network e.g. [{

  "protocol" => "ethereum",
  "network" => "kovan",
  "addresses"=> ["1234567890...", "d5ab4ce..."]
}]


100
101
102
103
104
105
106
107
108
# File 'lib/connection/mailchain.rb', line 100

def addresses_by_network
  protocol_networks.map do |obj|
    {
      'protocol' => obj['protocol'],
      'network' => obj['network'],
      'addresses' => @api.addresses(obj['protocol'], obj['network'])[:body]['addresses']
    }
  end
end

#configuration_wizardObject

# Run the Mailchain API configuration



25
26
27
28
29
30
31
32
33
# File 'lib/connection/mailchain.rb', line 25

def configuration_wizard
  connection_configuration = ConnectionConfigurationMailchain.new(@config)
  result = connection_configuration.configuration_wizard
  if result['save']
    result['config']['imap'].delete('password')
    new_config_json = JSON.pretty_generate(result['config'])
    File.write(@config_file, new_config_json)
  end
end

#configure_and_connectObject

Configures the Mailchain API settings then tests the connection



16
17
18
19
20
21
22
# File 'lib/connection/mailchain.rb', line 16

def configure_and_connect
  if !configuration_wizard # TODO: - wire up to connection configuration
    exit
  else
    test_connection
  end
end

#convert_message(message) ⇒ Object

Converts mailchain message to regular email



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/connection/mailchain.rb', line 52

def convert_message(message)
  footer = 'Delivered by Mailchain IMAP Connector'
  c_type = get_content_type(message['headers']['content-type'])

  mail = Mail.new do
    from        message['headers']['from']
    to          message['headers']['to']
    date        message['headers']['date']
    message_id  message['headers']['message-id']
    subject     message['subject']
    if c_type == 'html'
      html_part do
        content_type message['headers']['content-type']
        body "#{message['body']} <br/><br/>#{footer}"
      end
    end
    if c_type == 'plain'
      text_part do
        content_type message['headers']['content-type']
        body "#{message['body']} \r\n#{footer}"
      end
    end
  end
  mail.header['X-Mailchain-Block-Id'] = message['block-id']
  mail.header['X-Mailchain-Block-Id-Encoding'] = message['block-id-encoding']
  mail.header['X-Mailchain-Transaction-Hash']  = message['transaction-hash']
  mail.header['X-Mailchain-Transaction-Hash-Encoding'] = message['transaction-hash-encoding']
  mail
end

#convert_messages(messages) ⇒ Object

Convert and call the append_message for each valid message



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/connection/mailchain.rb', line 132

def convert_messages(messages)
  cmgs = []
  messages.each do |msg|
    next unless msg['status'] == 'ok'

    cm = convert_message(msg)
    cmgs << {
      'message' => cm,
      'message_id' => msg['headers']['message-id'],
      'message_date' => cm.date.to_time
    }
  end
  cmgs
end

#get_content_type(content_type) ⇒ Object

Returns ‘text` or `html`



83
84
85
86
87
88
89
90
91
92
# File 'lib/connection/mailchain.rb', line 83

def get_content_type(content_type)
  case content_type
  when '"text/html; charset=\"UTF-8\""'
    'html'
  when '"text/plain; charset=\"UTF-8\""'
    'plain'
  else
    'plain'
  end
end

#get_messages(addr, protocol, network) ⇒ Object

Gets messages from api and returns ‘body` => […]



126
127
128
129
# File 'lib/connection/mailchain.rb', line 126

def get_messages(addr, protocol, network)
  address = "#{addr}"
  @api.messages(address, protocol, network)[:body]
end

#messages_by_network(item) ⇒ Object

Returns messages formatted by_network

e.g. [ address, res['messages'] ]


112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/connection/mailchain.rb', line 112

def messages_by_network(item)
  protocol = item['protocol']
  network = item['network']
  addresses = item['addresses']
  messages = []
  addresses.each do |address|
    address_val = address["value"]
    res = get_messages(address_val, protocol, network)
    messages << [address_val, res['messages']] unless res['messages'].nil?
  end
  messages
end

#protocol_networksObject

Returns array of each network with parent protocol e.g. [=> ‘ethereum’, ‘network’ => ‘ropsten’,…]



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/connection/mailchain.rb', line 149

def protocol_networks
  output = []
  @api.protocols[:body]['protocols'].each do |proto|
    output << proto['networks'].map do |n|
      { 'protocol' => proto['name'], 'network' => n['name'] }
    end
  end
  output.flatten
rescue StandardError => e
  puts "Error: #{e}"
end

#test_connection(silent = false) ⇒ Object

Tests the connection to the Mailchain API



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/connection/mailchain.rb', line 36

def test_connection(silent = false)
  puts 'Testing API connection...' unless silent
  result = true
  begin
    res = @api.version
    res[:status_code] != 200
    puts "Connection was successful (API version: #{res[:body]['version']})" unless silent
  rescue StandardError => e
    puts "Mailchain API failed to connect with the following error: #{e}"
    puts 'Check the Mailchain client is running and configured correctly'
    result = false
  end
  result
end