class ConnectionMailchain

Handles the Mailchain API configuration and connection

Public Class Methods

new(config, config_file) click to toggle source

Initialize configs

# File lib/connection/mailchain.rb, line 9
def initialize(config, config_file)
  @config = config
  @config_file = config_file
  @api = MailchainApi.new(@config['mailchain'])
end

Public Instance Methods

addresses_by_network() click to toggle source

Returns addresses formatted by_network e.g. [{

  "protocol" => "ethereum",
  "network" => "kovan",
  "addresses"=> ["1234567890...", "d5ab4ce..."]
}]
# 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_wizard() click to toggle source

Run the Mailchain API configuration

# 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_connect() click to toggle source

Configures the Mailchain API settings then tests the connection

# 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) click to toggle source

Converts mailchain message to regular email

# 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) click to toggle source

Convert and call the append_message for each valid message

# 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) click to toggle source

Returns `text` or `html`

# 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) click to toggle source

Gets messages from api and returns `body` {“messages” => […]}

# 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) click to toggle source

Returns messages formatted by_network

e.g. [ address, res['messages'] ]
# 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_networks() click to toggle source

Returns array of each network with parent protocol e.g. [{'protocol' => 'ethereum', 'network' => 'ropsten'},…]

# 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) click to toggle source

Tests the connection to the Mailchain API

# 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