dyn-rb 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
1
+ #
2
+ # Author:: Sunny Gleason (<sunny@thesunnycloud.com>)
3
+ # Copyright:: Copyright (c) 2013 Dyn, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ require 'patron'
19
+
20
+ module Dyn
21
+ module HttpClient
22
+ class PatronClient < Base
23
+ private
24
+
25
+ def fixup_request(original)
26
+ headers = original[:headers]
27
+
28
+ if !original[:body].nil? && headers["Content-Type"].nil?
29
+ headers = headers.merge({"Content-Type" => "application/x-www-form-urlencoded"})
30
+ end
31
+
32
+ original.merge({:headers => headers})
33
+ end
34
+
35
+ def perform_request(method, uri, body, headers)
36
+ options = {}
37
+ options[:data] = body unless body.nil?
38
+ options[:max_redirects] = 0
39
+ headers = headers ? headers : {}
40
+
41
+ response = Patron::Session.new.request(method.to_s.downcase.to_sym, @base_url + uri, headers, options)
42
+
43
+ Response.new(response.status, response.body, response.headers)
44
+ end
45
+ end
46
+
47
+ unless defined?(DefaultClient)
48
+ class DefaultClient < Dynect::HttpClient::PatronClient
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,156 @@
1
+ #
2
+ # Author:: Sunny Gleason (<sunny@thesunnycloud.com>)
3
+ # Copyright:: Copyright (c) 2013 Dyn, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ module Dyn
20
+ module Messaging
21
+ class Client
22
+ require 'uri'
23
+ require 'json'
24
+ require 'dyn/exceptions'
25
+ require 'dyn/messaging/senders'
26
+ require 'dyn/messaging/accounts'
27
+ require 'dyn/messaging/recipients'
28
+ require 'dyn/messaging/suppressions'
29
+ require 'dyn/messaging/delivery'
30
+ require 'dyn/messaging/sent_mail'
31
+ require 'dyn/messaging/bounces'
32
+ require 'dyn/messaging/complaints'
33
+ require 'dyn/messaging/issues'
34
+ require 'dyn/messaging/opens'
35
+ require 'dyn/messaging/clicks'
36
+ require 'dyn/messaging/send_mail'
37
+ require 'dyn/http/http_client'
38
+
39
+ unless defined?(Dyn::HttpClient::DefaultClient)
40
+ require 'dyn/http/net_http'
41
+ end
42
+
43
+ attr_accessor :apikey, :rest
44
+
45
+ # Creates a new base object for interacting with Dyn's REST API
46
+ #
47
+ # @param [String] Your dyn api key
48
+ # @param [Boolean] Verbosity
49
+ def initialize(apikey, verbose=false)
50
+ @apikey = apikey
51
+ @rest = Dyn::HttpClient::DefaultClient.new("emailapi.dynect.net", "443", "https")
52
+ @rest.default_headers = {
53
+ 'User-Agent' => 'dyn-rb 1.0.2',
54
+ 'Content-Type' => 'application/x-www-form-urlencoded'
55
+ }
56
+ @verbose = verbose
57
+ end
58
+
59
+ ##
60
+ # Senders API
61
+ ##
62
+ def senders
63
+ Dyn::Messaging::Senders.new(self)
64
+ end
65
+
66
+ def accounts
67
+ Dyn::Messaging::Accounts.new(self)
68
+ end
69
+
70
+ def recipients
71
+ Dyn::Messaging::Recipients.new(self)
72
+ end
73
+
74
+ def suppressions
75
+ Dyn::Messaging::Suppressions.new(self)
76
+ end
77
+
78
+ def delivery
79
+ Dyn::Messaging::Delivery.new(self)
80
+ end
81
+
82
+ def sent_mail
83
+ Dyn::Messaging::SentMail.new(self)
84
+ end
85
+
86
+ def bounces
87
+ Dyn::Messaging::Bounces.new(self)
88
+ end
89
+
90
+ def complaints
91
+ Dyn::Messaging::Complaints.new(self)
92
+ end
93
+
94
+ def issues
95
+ Dyn::Messaging::Issues.new(self)
96
+ end
97
+
98
+ def opens
99
+ Dyn::Messaging::Opens.new(self)
100
+ end
101
+
102
+ def clicks
103
+ Dyn::Messaging::Clicks.new(self)
104
+ end
105
+
106
+ def send_mail
107
+ Dyn::Messaging::SendMail.new(self)
108
+ end
109
+
110
+ # Raw GET request, formatted for Dyn. See list of endpoints at:
111
+ #
112
+ # https://help.dynect.net/api/
113
+ #
114
+ # @param [String] The partial path to GET - for example, 'senders' or 'accounts'.
115
+ # @param [Hash] Query Parameters
116
+ # @param [Hash] Additional HTTP headers
117
+ def get(path_part, query_params = {}, additional_headers = {}, &block)
118
+ api_request { @rest.get("/rest/json/#{path_part}?" + URI.encode_www_form(query_params.merge({apikey:@apikey})), nil, additional_headers, &block) }
119
+ end
120
+
121
+ # Raw POST request, formatted for Dyn. See list of endpoints at:
122
+ #
123
+ # https://help.dynect.net/api/
124
+ #
125
+ # Read the API documentation, and submit the proper data structure from here.
126
+ #
127
+ # @param [String] The partial path to POST - for example, 'senders' or 'accounts'.
128
+ # @param [Hash] The data structure to submit as the body, is automatically turned to encoded Form POST
129
+ # @param [Hash] Additional HTTP headers
130
+ def post(path_part, form_params = {}, additional_headers = {}, &block)
131
+ api_request { @rest.post("/rest/json/#{path_part}", URI.encode_www_form(form_params.merge({apikey:@apikey})), additional_headers, &block) }
132
+ end
133
+
134
+ # Handles making Dynect API requests and formatting the responses properly.
135
+ def api_request(&block)
136
+ response_body = begin
137
+ response = block.call
138
+ response.body
139
+ rescue Exception => e
140
+ if @verbose
141
+ puts "I have #{e.inspect} with #{e.http_code}"
142
+ end
143
+ e.response
144
+ end
145
+
146
+ response = JSON.parse(response_body || '{}')
147
+
148
+ if (response["response"] && response["response"]["status"] == 200)
149
+ response["response"]["data"]
150
+ else
151
+ response
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,53 @@
1
+ #
2
+ # Author:: Sunny Gleason (<sunny@thesunnycloud.com>)
3
+ # Copyright:: Copyright (c) 2013 Dyn, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ module Dyn
20
+ module Messaging
21
+ class Accounts
22
+ def initialize(dyn)
23
+ @dyn = dyn
24
+ end
25
+
26
+ def list(startindex="0")
27
+ @dyn.get("#{resource_path}", {startindex:startindex})
28
+ end
29
+
30
+ def create(username, password, companyname, phone, address, city, state, zipcode, country, timezone, bounceurl, spamurl, unsubscribeurl, tracelinks, trackunsubscribes, generatenewapikey)
31
+ @dyn.post("#{resource_path}", {username:username, password:password, companyname:companyname, phone:phone, address:address, city:city, state:state, zipcode:zipcode, country:country, timezone:timezone, bounceurl:bounceurl, spamurl:spamurl, unsubscribeurl:unsubscribeurl, tracelinks:tracelinks, trackunsubscribes:trackunsubscribes, generatenewapikey:generatenewapikey})
32
+ end
33
+
34
+ def destroy(username)
35
+ @dyn.post("#{resource_path}/delete", {username:username})
36
+ end
37
+
38
+ def list_xheaders
39
+ @dyn.get("#{resource_path}/xheaders")
40
+ end
41
+
42
+ def update_xheaders(xh1, xh2, xh3, xh4)
43
+ @dyn.post("#{resource_path}/xheaders", {xheader1:xh1,xheader2:xh2,xheader3:xh3,xheader4:xh4})
44
+ end
45
+
46
+ private
47
+
48
+ def resource_path
49
+ "accounts"
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,41 @@
1
+ #
2
+ # Author:: Sunny Gleason (<sunny@thesunnycloud.com>)
3
+ # Copyright:: Copyright (c) 2013 Dyn, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ module Dyn
20
+ module Messaging
21
+ class Bounces
22
+ def initialize(dyn)
23
+ @dyn = dyn
24
+ end
25
+
26
+ def count(starttime, endtime, sender, xheadername)
27
+ @dyn.get("#{resource_path}/count", {starttime:starttime, endtime:endtime, sender:sender, xheadername:xheadername})
28
+ end
29
+
30
+ def list(starttime, endtime, startindex=0, sender, email, bouncetype, noheaders, xheadername)
31
+ @dyn.get("#{resource_path}", {starttime:starttime, endtime:endtime, startindex:startindex, sender:sender, emailaddress:email, bouncetype:bouncetype, noheaders:noheaders, xheadername:xheadername})
32
+ end
33
+
34
+ private
35
+
36
+ def resource_path
37
+ "reports/bounces"
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,49 @@
1
+ #
2
+ # Author:: Sunny Gleason (<sunny@thesunnycloud.com>)
3
+ # Copyright:: Copyright (c) 2013 Dyn, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ module Dyn
20
+ module Messaging
21
+ class Clicks
22
+ def initialize(dyn)
23
+ @dyn = dyn
24
+ end
25
+
26
+ def count(starttime, endtime, xheadername)
27
+ @dyn.get("#{resource_path}/count", {starttime:starttime, endtime:endtime, xheadername:xheadername})
28
+ end
29
+
30
+ def list(starttime, endtime, startindex=0, xheadername)
31
+ @dyn.get("#{resource_path}", {starttime:starttime, endtime:endtime, startindex:startindex, xheadername:xheadername})
32
+ end
33
+
34
+ def unique_count(starttime, endtime, xheadername)
35
+ @dyn.get("#{resource_path}/count/unique", {starttime:starttime, endtime:endtime, xheadername:xheadername})
36
+ end
37
+
38
+ def unique(starttime, endtime, startindex=0, xheadername)
39
+ @dyn.get("#{resource_path}/unique", {starttime:starttime, endtime:endtime, startindex:startindex, xheadername:xheadername})
40
+ end
41
+
42
+ private
43
+
44
+ def resource_path
45
+ "reports/clicks"
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,41 @@
1
+ #
2
+ # Author:: Sunny Gleason (<sunny@thesunnycloud.com>)
3
+ # Copyright:: Copyright (c) 2013 Dyn, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ module Dyn
20
+ module Messaging
21
+ class Complaints
22
+ def initialize(dyn)
23
+ @dyn = dyn
24
+ end
25
+
26
+ def count(starttime, endtime, sender, xheadername)
27
+ @dyn.get("#{resource_path}/count", {starttime:starttime, endtime:endtime, sender:sender, xheadername:xheadername})
28
+ end
29
+
30
+ def list(starttime, endtime, startindex=0, sender, xheadername)
31
+ @dyn.get("#{resource_path}", {starttime:starttime, endtime:endtime, startindex:startindex, sender:sender, xheadername:xheadername})
32
+ end
33
+
34
+ private
35
+
36
+ def resource_path
37
+ "reports/complaints"
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ #
2
+ # Author:: Sunny Gleason (<sunny@thesunnycloud.com>)
3
+ # Copyright:: Copyright (c) 2013 Dyn, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ module Dyn
20
+ module Messaging
21
+ class Delivery
22
+ def initialize(dyn)
23
+ @dyn = dyn
24
+ end
25
+
26
+ def count(starttime, endtime, sender, xheadername)
27
+ @dyn.get("#{resource_path}/count", {starttime:starttime, endtime:endtime, sender:sender, xheadername:xheadername})
28
+ end
29
+
30
+ def list(starttime, endtime, startindex=0, sender, xheadername)
31
+ @dyn.get("#{resource_path}", {starttime:starttime, endtime:endtime, startindex:startindex, sender:sender, xheadername:xheadername})
32
+ end
33
+
34
+ private
35
+
36
+ def resource_path
37
+ "reports/delivered"
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ #
2
+ # Author:: Sunny Gleason (<sunny@thesunnycloud.com>)
3
+ # Copyright:: Copyright (c) 2013 Dyn, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ module Dyn
20
+ module Messaging
21
+ class Issues
22
+ def initialize(dyn)
23
+ @dyn = dyn
24
+ end
25
+
26
+ def count(starttime, endtime)
27
+ @dyn.get("#{resource_path}/count", {starttime:starttime, endtime:endtime})
28
+ end
29
+
30
+ def list(starttime, endtime, startindex=0)
31
+ @dyn.get("#{resource_path}", {starttime:starttime, endtime:endtime, startindex:startindex})
32
+ end
33
+
34
+ private
35
+
36
+ def resource_path
37
+ "reports/issues"
38
+ end
39
+ end
40
+ end
41
+ end