Class: ConnectionMailchain
- Inherits:
-
Object
- Object
- ConnectionMailchain
- Defined in:
- lib/connection/mailchain.rb
Overview
Handles the Mailchain API configuration and connection
Instance Method Summary collapse
-
#addresses_by_network ⇒ Object
Returns addresses formatted by_network e.g.
-
#configuration_wizard ⇒ Object
# Run the Mailchain API configuration.
-
#configure_and_connect ⇒ Object
Configures the Mailchain API settings then tests the connection.
-
#convert_message(message) ⇒ Object
Converts mailchain message to regular email.
-
#convert_messages(messages) ⇒ Object
Convert and call the append_message for each valid message.
-
#get_content_type(content_type) ⇒ Object
Returns ‘text` or `html`.
-
#get_messages(addr, protocol, network) ⇒ Object
Gets messages from api and returns ‘body` => […].
-
#initialize(config, config_file) ⇒ ConnectionMailchain
constructor
Initialize configs.
-
#messages_by_network(item) ⇒ Object
Returns messages formatted by_network e.g.
-
#protocol_networks ⇒ Object
Returns array of each network with parent protocol e.g.
-
#test_connection(silent = false) ⇒ Object
Tests the connection to the Mailchain API.
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_network ⇒ Object
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_wizard ⇒ Object
# 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_connect ⇒ Object
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 () = 'Delivered by Mailchain IMAP Connector' c_type = get_content_type(['headers']['content-type']) mail = Mail.new do from ['headers']['from'] to ['headers']['to'] date ['headers']['date'] ['headers']['message-id'] subject ['subject'] if c_type == 'html' html_part do content_type ['headers']['content-type'] body "#{['body']} <br/><br/>#{}" end end if c_type == 'plain' text_part do content_type ['headers']['content-type'] body "#{['body']} \r\n#{}" end end end mail.header['X-Mailchain-Block-Id'] = ['block-id'] mail.header['X-Mailchain-Block-Id-Encoding'] = ['block-id-encoding'] mail.header['X-Mailchain-Transaction-Hash'] = ['transaction-hash'] mail.header['X-Mailchain-Transaction-Hash-Encoding'] = ['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 () cmgs = [] .each do |msg| next unless msg['status'] == 'ok' cm = (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 (addr, protocol, network) address = "#{addr}" @api.(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 (item) protocol = item['protocol'] network = item['network'] addresses = item['addresses'] = [] addresses.each do |address| address_val = address["value"] res = (address_val, protocol, network) << [address_val, res['messages']] unless res['messages'].nil? end end |
#protocol_networks ⇒ Object
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 |