Class: Dyn::Messaging::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/dyn/messaging.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apikey, verbose = false) ⇒ Client

Creates a new base object for interacting with Dyn’s REST API

Parameters:

  • Your (String)

    dyn api key

  • Verbosity (Boolean)


49
50
51
52
53
54
55
56
57
# File 'lib/dyn/messaging.rb', line 49

def initialize(apikey, verbose=false)
  @apikey = apikey
  @rest = Dyn::HttpClient::DefaultClient.new("emailapi.dynect.net", "443", "https")
  @rest.default_headers = {
    'User-Agent'   => 'dyn-rb 1.0.3',
    'Content-Type' => 'application/x-www-form-urlencoded'
  }
  @verbose = verbose
end

Instance Attribute Details

#apikeyObject

Returns the value of attribute apikey.



43
44
45
# File 'lib/dyn/messaging.rb', line 43

def apikey
  @apikey
end

#restObject

Returns the value of attribute rest.



43
44
45
# File 'lib/dyn/messaging.rb', line 43

def rest
  @rest
end

Instance Method Details

#accountsObject



66
67
68
# File 'lib/dyn/messaging.rb', line 66

def accounts
  Dyn::Messaging::Accounts.new(self)
end

#api_request(&block) ⇒ Object

Handles making Dynect API requests and formatting the responses properly.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/dyn/messaging.rb', line 135

def api_request(&block)
  response_body = begin
    response = block.call
    response.body
  rescue Exception => e
    if @verbose
      puts "I have #{e.inspect} with #{e.http_code}"
    end
    e.response
  end
  
  response = JSON.parse(response_body || '{}')
  
  if (response["response"] && response["response"]["status"] == 200)
    response["response"]["data"]
  else
    response
  end
end

#bouncesObject



86
87
88
# File 'lib/dyn/messaging.rb', line 86

def bounces
  Dyn::Messaging::Bounces.new(self)
end

#clicksObject



102
103
104
# File 'lib/dyn/messaging.rb', line 102

def clicks
  Dyn::Messaging::Clicks.new(self)
end

#complaintsObject



90
91
92
# File 'lib/dyn/messaging.rb', line 90

def complaints
  Dyn::Messaging::Complaints.new(self)
end

#deliveryObject



78
79
80
# File 'lib/dyn/messaging.rb', line 78

def delivery
  Dyn::Messaging::Delivery.new(self)
end

#get(path_part, query_params = {}, additional_headers = {}, &block) ⇒ Object

Raw GET request, formatted for Dyn. See list of endpoints at:

help.dynect.net/api/

Parameters:

  • The (String)

    partial path to GET - for example, ‘senders’ or ‘accounts’.

  • Query (Hash)

    Parameters

  • Additional (Hash)

    HTTP headers



117
118
119
# File 'lib/dyn/messaging.rb', line 117

def get(path_part, query_params = {}, additional_headers = {}, &block)
  api_request { @rest.get("/rest/json/#{path_part}?" + URI.encode_www_form(query_params.merge({apikey:@apikey})), nil, additional_headers, &block) }
end

#issuesObject



94
95
96
# File 'lib/dyn/messaging.rb', line 94

def issues
  Dyn::Messaging::Issues.new(self)
end

#opensObject



98
99
100
# File 'lib/dyn/messaging.rb', line 98

def opens
  Dyn::Messaging::Opens.new(self)
end

#post(path_part, form_params = {}, additional_headers = {}, &block) ⇒ Object

Raw POST request, formatted for Dyn. See list of endpoints at:

help.dynect.net/api/

Read the API documentation, and submit the proper data structure from here.

Parameters:

  • The (String)

    partial path to POST - for example, ‘senders’ or ‘accounts’.

  • The (Hash)

    data structure to submit as the body, is automatically turned to encoded Form POST

  • Additional (Hash)

    HTTP headers



130
131
132
# File 'lib/dyn/messaging.rb', line 130

def post(path_part, form_params = {}, additional_headers = {}, &block)
  api_request { @rest.post("/rest/json/#{path_part}", URI.encode_www_form(form_params.merge({apikey:@apikey})), additional_headers, &block) }
end

#recipientsObject



70
71
72
# File 'lib/dyn/messaging.rb', line 70

def recipients
  Dyn::Messaging::Recipients.new(self)
end

#send_mailObject



106
107
108
# File 'lib/dyn/messaging.rb', line 106

def send_mail
  Dyn::Messaging::SendMail.new(self)
end

#sendersObject

Senders API



62
63
64
# File 'lib/dyn/messaging.rb', line 62

def senders
  Dyn::Messaging::Senders.new(self)
end

#sent_mailObject



82
83
84
# File 'lib/dyn/messaging.rb', line 82

def sent_mail
  Dyn::Messaging::SentMail.new(self)
end

#suppressionsObject



74
75
76
# File 'lib/dyn/messaging.rb', line 74

def suppressions
  Dyn::Messaging::Suppressions.new(self)
end