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,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 Opens
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/opens"
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 Recipients
22
+ def initialize(dyn)
23
+ @dyn = dyn
24
+ end
25
+
26
+ def status(email)
27
+ @dyn.get("#{resource_path}/status", {emailaddress:email})
28
+ end
29
+
30
+ def activate(email)
31
+ @dyn.post("#{resource_path}/activate", {emailaddress:email})
32
+ end
33
+
34
+ private
35
+
36
+ def resource_path
37
+ "recipients"
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,37 @@
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 SendMail
22
+ def initialize(dyn)
23
+ @dyn = dyn
24
+ end
25
+
26
+ def create(from, to, subject, bodytext, bodyhtml, cc, replyto, xheaders)
27
+ @dyn.post("#{resource_path}", {from:from, to:to, subject:subject, bodytext:bodytext, cc:cc, replyto:replyto, xheaders:xheaders})
28
+ end
29
+
30
+ private
31
+
32
+ def resource_path
33
+ "send"
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,61 @@
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 Senders
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(email, seeding="0")
31
+ @dyn.post("#{resource_path}", {emailaddress:email, seeding:seeding})
32
+ end
33
+
34
+ def update(email, seeding="0")
35
+ @dyn.post("#{resource_path}", {emailaddress:email, seeding:seeding})
36
+ end
37
+
38
+ def destroy(email)
39
+ @dyn.post("#{resource_path}/delete", {emailaddress:email})
40
+ end
41
+
42
+ def details(email)
43
+ @dyn.get("#{resource_path}/details", {emailaddress:email})
44
+ end
45
+
46
+ def status(email)
47
+ @dyn.get("#{resource_path}/status", {emailaddress:email})
48
+ end
49
+
50
+ def dkim(email, dkim)
51
+ @dyn.post("#{resource_path}/dkim", {emailaddress:email, dkim:dkim})
52
+ end
53
+
54
+ private
55
+
56
+ def resource_path
57
+ "senders"
58
+ end
59
+ end
60
+ end
61
+ 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 SentMail
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/sent"
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 Suppressions
22
+ def initialize(dyn)
23
+ @dyn = dyn
24
+ end
25
+
26
+ def count(startdate, enddate)
27
+ @dyn.get("#{resource_path}/count", {startdate:startdate, enddate:enddate})
28
+ end
29
+
30
+ def list(startdate, enddate, startindex=0)
31
+ @dyn.get("#{resource_path}", {startdate:startdate, enddate:enddate, startindex:startindex})
32
+ end
33
+
34
+ def create(email)
35
+ @dyn.post("#{resource_path}", {emailaddress:email})
36
+ end
37
+
38
+ def activate(email)
39
+ @dyn.post("#{resource_path}/activate", {emailaddress:email})
40
+ end
41
+
42
+ private
43
+
44
+ def resource_path
45
+ "suppressions"
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,238 @@
1
+ #
2
+ # Author:: Sunny Gleason (<sunny@thesunnycloud.com>)
3
+ # Author:: Adam Jacob (<adam@opscode.com>)
4
+ # Copyright:: Copyright (c) 2013 Dyn, Inc.
5
+ # Copyright:: Copyright (c) 2010 Opscode, Inc.
6
+ # License:: Apache License, Version 2.0
7
+ #
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
11
+ #
12
+ # http://www.apache.org/licenses/LICENSE-2.0
13
+ #
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
19
+ #
20
+
21
+ module Dyn
22
+ module Traffic
23
+ class Client
24
+ require 'json'
25
+ require 'dyn/exceptions'
26
+ require 'dyn/traffic/base'
27
+ require 'dyn/traffic/gslb'
28
+ require 'dyn/traffic/http_redirect'
29
+ require 'dyn/traffic/qps_report'
30
+ require 'dyn/traffic/resource'
31
+ require 'dyn/traffic/secondary_zone'
32
+ require 'dyn/traffic/session'
33
+ require 'dyn/traffic/zone'
34
+ require 'dyn/http/http_client'
35
+
36
+ unless defined?(Dyn::HttpClient::DefaultClient)
37
+ require 'dyn/http/net_http'
38
+ end
39
+
40
+ attr_accessor :customer_name, :user_name, :password, :rest, :auth_token
41
+
42
+ # Creates a new base object for interacting with Dyn's REST API
43
+ #
44
+ # @param [String] Your dyn customer name
45
+ # @param [String] Your dyn user name
46
+ # @param [String] Your dyn password
47
+ # @param [String] The zone you are going to be editing
48
+ # @param [Boolean] Whether to connect immediately or not - runs login for you
49
+ # @param [Boolean] Verbosity
50
+ def initialize(customer_name, user_name, password, zone=nil, connect=true, verbose=false, max_redirects=10)
51
+ @customer_name = customer_name
52
+ @user_name = user_name
53
+ @password = password
54
+ @rest = Dyn::HttpClient::DefaultClient.new("api2.dynect.net", "443", "https")
55
+ @rest.default_headers = {
56
+ 'User-Agent' => 'dyn-rb 1.0.2',
57
+ 'Content-Type' => 'application/json'
58
+ }
59
+ @zone = zone
60
+ @verbose = verbose
61
+ @session = Dyn::Traffic::Session.new(self)
62
+ login if connect
63
+ end
64
+
65
+ # Login to Dyn - must be done before any other methods called.
66
+ #
67
+ # See: https://manage.dynect.net/help/docs/api2/rest/resources/Session.html
68
+ #
69
+ # @return [Hash] The dynect API response
70
+ def login
71
+ response = @session.create
72
+ @auth_token = response["token"]
73
+ @rest.default_headers = { 'Content-Type' => 'application/json', 'Auth-Token' => @auth_token }
74
+ response
75
+ end
76
+
77
+ # Logout of Dyn - generally the last operation performed
78
+ #
79
+ # See: https://manage.dynect.net/help/docs/api2/rest/resources/Session.html
80
+ #
81
+ # @return [Hash] The dynect API response
82
+ def logout
83
+ response = @session.delete
84
+ @auth_token = nil
85
+ @rest.default_headers = { 'Content-Type' => 'application/json' }
86
+ response
87
+ end
88
+
89
+ # for convenience...
90
+ def publish
91
+ zone.publish
92
+ end
93
+
94
+ ##
95
+ # Zone attribute setter
96
+ ##
97
+ def zone=(zone)
98
+ @zone = zone
99
+ end
100
+
101
+ ##
102
+ # HTTPRedirect API
103
+ ##
104
+ def http_redirect(options = {})
105
+ Dyn::Traffic::HTTPRedirect.new(self, @zone, options)
106
+ end
107
+
108
+ ##
109
+ # Session API
110
+ ##
111
+ def session
112
+ Dyn::Traffic::Session.new(self)
113
+ end
114
+
115
+ ##
116
+ # GSLB API
117
+ ##
118
+ def gslb(options = {})
119
+ Dyn::Traffic::GSLB.new(self, @zone, options)
120
+ end
121
+
122
+ ##
123
+ # Zone API
124
+ ##
125
+ def zone
126
+ Dyn::Traffic::Zone.new(self, @zone)
127
+ end
128
+
129
+ # Convert a CamelCasedString to an under_scored_string.
130
+ def self.underscore(string)
131
+ word = string.dup
132
+ word.gsub!(/::/, '/')
133
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
134
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
135
+ word.tr!("-", "_")
136
+ word.downcase!
137
+ word
138
+ end
139
+
140
+ ##
141
+ # Resource Record API
142
+ ##
143
+ %w{AAAA A CNAME DNSKEY DS KEY LOC MX NS PTR RP SOA SRV TXT}.each do |record_type|
144
+ define_method underscore(record_type) do
145
+ Dyn::Traffic::Resource.new(self, @zone, "#{record_type}")
146
+ end
147
+ end
148
+
149
+ # Raw GET request, formatted for Dyn. See list of endpoints at:
150
+ #
151
+ # https://manage.dynect.net/help/docs/api2/rest/resources/
152
+ #
153
+ # @param [String] The partial path to GET - for example, 'User' or 'Zone'.
154
+ # @param [Hash] Additional HTTP headers
155
+ def get(path_part, additional_headers = {}, &block)
156
+ api_request { @rest.get('/REST/' + path_part, nil, additional_headers, &block) }
157
+ end
158
+
159
+ # Raw DELETE request, formatted for Dyn. See list of endpoints at:
160
+ #
161
+ # https://manage.dynect.net/help/docs/api2/rest/resources/
162
+ #
163
+ # @param [String] The partial path to DELETE - for example, 'User' or 'Zone'.
164
+ # @param [Hash] Additional HTTP headers
165
+ def delete(path_part, additional_headers = {}, &block)
166
+ api_request { @rest.delete('/REST/' + path_part, "", additional_headers, &block) }
167
+ end
168
+
169
+ # Raw POST request, formatted for Dyn. See list of endpoints at:
170
+ #
171
+ # https://manage.dynect.net/help/docs/api2/rest/resources/
172
+ #
173
+ # Read the API documentation, and submit the proper data structure from here.
174
+ #
175
+ # @param [String] The partial path to POST - for example, 'User' or 'Zone'.
176
+ # @param [Hash] The data structure to submit as the body, is automatically turned to JSON.
177
+ # @param [Hash] Additional HTTP headers
178
+ def post(path_part, payload, additional_headers = {}, &block)
179
+ api_request { @rest.post('/REST/' + path_part, payload.to_json, additional_headers, &block) }
180
+ end
181
+
182
+ # Raw PUT request, formatted for Dyn. See list of endpoints at:
183
+ #
184
+ # https://manage.dynect.net/help/docs/api2/rest/resources/
185
+ #
186
+ # Read the API documentation, and submit the proper data structure from here.
187
+ #
188
+ # @param [String] The partial path to PUT - for example, 'User' or 'Zone'.
189
+ # @param [Hash] The data structure to submit as the body, is automatically turned to JSON.
190
+ # @param [Hash] Additional HTTP headers
191
+ def put(path_part, payload, additional_headers = {}, &block)
192
+ api_request { @rest.put('/REST/' + path_part, payload.to_json, additional_headers, &block) }
193
+ end
194
+
195
+ # Handles making Dynect API requests and formatting the responses properly.
196
+ def api_request(&block)
197
+ response_body = begin
198
+ response = block.call
199
+ response.body
200
+ rescue Exception => e
201
+ if @verbose
202
+ puts "I have #{e.inspect} with #{e.http_code}"
203
+ end
204
+ if e.http_code == 307
205
+ e.response.sub!('/REST/','') if e.response =~ /^\/REST\//
206
+ get(e.response)
207
+ end
208
+ e.response
209
+ end
210
+
211
+ parse_response(JSON.parse(response_body || '{}'))
212
+ end
213
+
214
+ #
215
+ def parse_response(response)
216
+ case response["status"]
217
+ when "success"
218
+ response["data"]
219
+ when "incomplete"
220
+ # we get 'incomplete' when the API is running slow and claims the session has a previous job running
221
+ # raise an error and return the job ID in case we want to ask the API what the job's status is
222
+ error_messages = []
223
+ error_messages.push( "This session may have a job _still_ running (slowly). Call /REST/Job/#{response["job_id"]} to get its status." )
224
+ response["msgs"].each do |error_message|
225
+ error_messages << "#{error_message["LVL"]} #{error_message["ERR_CD"]} #{error_message["SOURCE"]} - #{error_message["INFO"]}"
226
+ end
227
+ raise Dyn::Exceptions::IncompleteRequest.new( "#{error_messages.join("\n")}", response["job_id"] )
228
+ when "failure"
229
+ error_messages = []
230
+ response["msgs"].each do |error_message|
231
+ error_messages << "#{error_message["LVL"]} #{error_message["ERR_CD"]} #{error_message["SOURCE"]} - #{error_message["INFO"]}"
232
+ end
233
+ raise Dyn::Exceptions::RequestFailed, "Request failed: #{error_messages.join("\n")}"
234
+ end
235
+ end
236
+ end
237
+ end
238
+ end