Class: TryPaper::Mailer

Inherits:
Object
  • Object
show all
Defined in:
lib/TryPaper/mailer.rb

Overview

the Mailer class takes an instance of the document class and sends it to the TryPaper API

Constant Summary collapse

API_URL =
"https://api.trypaper.com/Mailing"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, return_address_id, tags = []) ⇒ Mailer

instantiate client and then add document and recipient



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

def initialize(api_key, return_address_id, tags = [])
  @recipient = TryPaper::Recipient.new
  @document = nil
  @api_key = api_key
  @return_address_id = return_address_id
  @tags = tags
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



13
14
15
# File 'lib/TryPaper/mailer.rb', line 13

def api_key
  @api_key
end

#documentObject

Returns the value of attribute document.



12
13
14
# File 'lib/TryPaper/mailer.rb', line 12

def document
  @document
end

#recipientObject

Returns the value of attribute recipient.



12
13
14
# File 'lib/TryPaper/mailer.rb', line 12

def recipient
  @recipient
end

#return_address_idObject (readonly)

Returns the value of attribute return_address_id.



13
14
15
# File 'lib/TryPaper/mailer.rb', line 13

def return_address_id
  @return_address_id
end

#tagsObject (readonly)

Returns the value of attribute tags.



13
14
15
# File 'lib/TryPaper/mailer.rb', line 13

def tags
  @tags
end

Instance Method Details

#send_dataObject



24
25
26
27
28
29
30
31
# File 'lib/TryPaper/mailer.rb', line 24

def send_data
  {
    "ReturnAddressId" => return_address_id,
    "Tags" => tags,
    "Content" => document.base64format,
    "Recipient" => recipient.formatted_address
  }
end

#submitObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/TryPaper/mailer.rb', line 33

def submit
  uri = URI.parse(API_URL)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Post.new(uri.request_uri)

  #headers
  request['Authorization'] = api_key
  request['Content-Type'] = "application/json"

  request.body = send_data.to_json
  response = http.request(request)
end