emailyak 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +22 -0
- data/Rakefile +2 -0
- data/emailyak.gemspec +23 -0
- data/lib/data/ca-certificates.crt +3524 -0
- data/lib/emailyak/version.rb +3 -0
- data/lib/emailyak.rb +220 -0
- metadata +104 -0
data/lib/emailyak.rb
ADDED
@@ -0,0 +1,220 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
require 'set'
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'json'
|
6
|
+
require 'openssl'
|
7
|
+
require 'rest_client'
|
8
|
+
|
9
|
+
# A lot of the structure here is borrowed from the Stripe Ruby
|
10
|
+
# bindings (https://github.com/stripe/stripe-ruby).
|
11
|
+
|
12
|
+
# Further adapted from https://github.com/gdb/emailyak-ruby
|
13
|
+
# because I couldn't get it to install on heroku and
|
14
|
+
# I wanted to add to it
|
15
|
+
module EmailYak
|
16
|
+
# @@version = '0.0.1'
|
17
|
+
@@ssl_bundle_path = File.join(File.dirname(__FILE__), 'data/ca-certificates.crt')
|
18
|
+
@@api_key = nil
|
19
|
+
@@api_base = 'https://api.emailyak.com/v1'
|
20
|
+
@@verify_ssl_certs = true
|
21
|
+
|
22
|
+
module Util
|
23
|
+
def self.file_readable(file)
|
24
|
+
begin
|
25
|
+
File.open(file) { |f| }
|
26
|
+
rescue
|
27
|
+
false
|
28
|
+
else
|
29
|
+
true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class EmailYakError < StandardError; end
|
35
|
+
class APIConnectionError < EmailYakError; end
|
36
|
+
class APIResponseCodeError < EmailYakError
|
37
|
+
attr_reader :rcode, :rbody
|
38
|
+
|
39
|
+
def initialize(message, rcode, rbody)
|
40
|
+
super(message)
|
41
|
+
@rcody = rcode
|
42
|
+
@rbody = rbody
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.api_url(url=''); [@@api_base, api_key, 'json', url].join('/'); end
|
47
|
+
def self.api_key=(api_key); @@api_key = api_key; end
|
48
|
+
def self.api_key; @@api_key; end
|
49
|
+
def self.api_base=(api_base); @@api_base = api_base; end
|
50
|
+
def self.api_base; @@api_base; end
|
51
|
+
def self.verify_ssl_certs=(verify); @@verify_ssl_certs = verify; end
|
52
|
+
def self.verify_ssl_certs; @@verify_ssl_certs; end
|
53
|
+
# def self.version; @@version; end
|
54
|
+
|
55
|
+
module Email
|
56
|
+
def self.all(params={})
|
57
|
+
EmailYak.request(:get, 'get/all/email/', nil, params)
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.get(params={})
|
61
|
+
EmailYak.request(:get, 'get/email/', nil, params)
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.list(params={})
|
65
|
+
EmailYak.request(:get, 'get/email/list/', nil, params)
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.new(params={})
|
69
|
+
EmailYak.request(:get, 'get/new/email/', nil, params)
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.delete(params={})
|
73
|
+
EmailYak.request(:post, 'delete/email/', nil, params)
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.send(params={})
|
77
|
+
EmailYak.request(:post, 'send/email/', nil, params)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
module Address
|
82
|
+
def self.register(params={})
|
83
|
+
EmailYak.request(:post, 'register/address/', nil, params)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
module Domain
|
88
|
+
def self.register(params={})
|
89
|
+
EmailYak.request(:post, 'register/domain/', nil, params)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def self.request(method, url, api_key, params=nil, headers={})
|
94
|
+
api_key ||= @@api_key
|
95
|
+
raise EmailYakError.new('No API key provided. (HINT: set your API key using "EmailYak.api_key = <API-KEY>".') unless api_key
|
96
|
+
|
97
|
+
if !verify_ssl_certs
|
98
|
+
unless @no_verify
|
99
|
+
$stderr.puts "WARNING: Running without SSL cert verification. Execute 'EmailYak.verify_ssl_certs = true' to enable verification."
|
100
|
+
@no_verify = true
|
101
|
+
end
|
102
|
+
ssl_opts = { :verify_ssl => false }
|
103
|
+
elsif !Util.file_readable(@@ssl_bundle_path)
|
104
|
+
unless @no_bundle
|
105
|
+
$stderr.puts "WARNING: Running without SSL cert verification because #{@@ssl_bundle_path} isn't readable"
|
106
|
+
@no_bundle = true
|
107
|
+
end
|
108
|
+
ssl_opts = { :verify_ssl => false }
|
109
|
+
else
|
110
|
+
ssl_opts = {
|
111
|
+
:verify_ssl => OpenSSL::SSL::VERIFY_PEER,
|
112
|
+
:ssl_ca_file => @@ssl_bundle_path
|
113
|
+
}
|
114
|
+
end
|
115
|
+
|
116
|
+
case method.to_s.downcase.to_sym
|
117
|
+
when :get, :head, :delete
|
118
|
+
# Make params into GET parameters
|
119
|
+
headers = { :params => params }.merge(headers)
|
120
|
+
payload = nil
|
121
|
+
else
|
122
|
+
payload = params
|
123
|
+
end
|
124
|
+
opts = {
|
125
|
+
:method => method,
|
126
|
+
:url => self.api_url(url),
|
127
|
+
:user => api_key,
|
128
|
+
:headers => headers,
|
129
|
+
:open_timeout => 30,
|
130
|
+
:payload => payload,
|
131
|
+
:timeout => 80
|
132
|
+
}.merge(ssl_opts)
|
133
|
+
|
134
|
+
begin
|
135
|
+
response = execute_request(opts)
|
136
|
+
rescue SocketError => e
|
137
|
+
self.handle_restclient_error(e)
|
138
|
+
rescue NoMethodError => e
|
139
|
+
# Work around RestClient bug
|
140
|
+
if e.message =~ /\WRequestFailed\W/
|
141
|
+
e = APIConnectionError.new('Unexpected HTTP response code')
|
142
|
+
self.handle_restclient_error(e)
|
143
|
+
else
|
144
|
+
raise
|
145
|
+
end
|
146
|
+
rescue RestClient::ExceptionWithResponse => e
|
147
|
+
if rcode = e.http_code and rbody = e.http_body
|
148
|
+
self.handle_api_error(rcode, rbody)
|
149
|
+
else
|
150
|
+
self.handle_restclient_error(e)
|
151
|
+
end
|
152
|
+
rescue RestClient::Exception, Errno::ECONNREFUSED => e
|
153
|
+
self.handle_restclient_error(e)
|
154
|
+
end
|
155
|
+
|
156
|
+
rbody = response.body
|
157
|
+
rcode = response.code
|
158
|
+
begin
|
159
|
+
resp = JSON.parse(rbody, :symbolize_names => true)
|
160
|
+
rescue JSON::ParseError
|
161
|
+
raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})")
|
162
|
+
end
|
163
|
+
|
164
|
+
[resp, api_key]
|
165
|
+
end
|
166
|
+
|
167
|
+
private
|
168
|
+
|
169
|
+
def self.execute_request(opts)
|
170
|
+
RestClient::Request.execute(opts)
|
171
|
+
end
|
172
|
+
|
173
|
+
def self.handle_api_error(rcode, rbody)
|
174
|
+
case rcode
|
175
|
+
when 402
|
176
|
+
raise APIResponseCodeError.new('Invalid JSON/XML. Malformed JSON/XML syntax.', rcode, rbody)
|
177
|
+
when 403
|
178
|
+
raise APIResponseCodeError.new('Permission denied.', rcode, rbody)
|
179
|
+
when 420
|
180
|
+
raise APIResponseCodeError.new('Internal Error. There was an error in the system.', rcode, rbody)
|
181
|
+
when 421
|
182
|
+
raise APIResponseCodeError.new('Input Parameter Error.', rcode, rbody)
|
183
|
+
when 423
|
184
|
+
raise APIResponseCodeError.new('API key does not exist.', rcode, rbody)
|
185
|
+
when 424
|
186
|
+
raise APIResponseCodeError.new('Account disabled.', rcode, rbody)
|
187
|
+
when 426
|
188
|
+
raise APIResponseCodeError.new('Domain has been disabled.', rcode, rbody)
|
189
|
+
when 427
|
190
|
+
raise APIResponseCodeError.new('The domain is not registered with Email Yak.', rcode, rbody)
|
191
|
+
when 428
|
192
|
+
raise APIResponseCodeError.new('The requested record is not found.', rcode, rbody)
|
193
|
+
when 430
|
194
|
+
raise APIResponseCodeError.new('Account not allowed access to requested version of API.', rcode, rbody)
|
195
|
+
when 431
|
196
|
+
raise APIResponseCodeError.new('Invalid Response Format. In the url, specify ../json/.. or ../xml/..', rcode, rbody)
|
197
|
+
when 432
|
198
|
+
raise APIResponseCodeError.new('Invalid when 431(Request Format. Needs to be JSON or XML.', rcode, rbody)
|
199
|
+
when 503
|
200
|
+
raise APIResponseCodeError.new('Service is Temporarily Down. Please stand by.', rcode, rbody)
|
201
|
+
else
|
202
|
+
raise APIResponseCodeError.new("Unrecognized return code #{rcode}", rcode, rbody)
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
def self.handle_restclient_error(e)
|
207
|
+
case e
|
208
|
+
when RestClient::ServerBrokeConnection, RestClient::RequestTimeout
|
209
|
+
message = "Could not connect to EmailYak (#{@@api_base}). Please check your internet connection and try again."
|
210
|
+
when RestClient::SSLCertificateNotVerified
|
211
|
+
message = "Could not verify EmailYak's SSL certificate. Please make sure that your network is not intercepting certificates."
|
212
|
+
when SocketError
|
213
|
+
message = "Unexpected error communicating when trying to connect to EmailYak. HINT: You may be seeing this message because your DNS is not working. To check, try running 'host emailyak.com' from the command line."
|
214
|
+
else
|
215
|
+
message = "Unexpected error communicating with EmailYak."
|
216
|
+
end
|
217
|
+
message += "\n\n(Network error: #{e.message})"
|
218
|
+
raise APIConnectionError.new(message)
|
219
|
+
end
|
220
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: emailyak
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Scott Ballantyne
|
14
|
+
- Greg Brockman
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-10-02 00:00:00 +08:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: rest-client
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 3
|
31
|
+
segments:
|
32
|
+
- 0
|
33
|
+
version: "0"
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: json
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: *id002
|
50
|
+
description: making several different emailyak projects into a gem description
|
51
|
+
email:
|
52
|
+
- ussballantyne@gmail.cm
|
53
|
+
- gdb@gregbrockman.com
|
54
|
+
executables: []
|
55
|
+
|
56
|
+
extensions: []
|
57
|
+
|
58
|
+
extra_rdoc_files: []
|
59
|
+
|
60
|
+
files:
|
61
|
+
- .gitignore
|
62
|
+
- Gemfile
|
63
|
+
- Gemfile.lock
|
64
|
+
- Rakefile
|
65
|
+
- emailyak.gemspec
|
66
|
+
- lib/data/ca-certificates.crt
|
67
|
+
- lib/emailyak.rb
|
68
|
+
- lib/emailyak/version.rb
|
69
|
+
has_rdoc: true
|
70
|
+
homepage: ""
|
71
|
+
licenses: []
|
72
|
+
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
hash: 3
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
hash: 3
|
93
|
+
segments:
|
94
|
+
- 0
|
95
|
+
version: "0"
|
96
|
+
requirements: []
|
97
|
+
|
98
|
+
rubyforge_project: emailyak
|
99
|
+
rubygems_version: 1.3.7
|
100
|
+
signing_key:
|
101
|
+
specification_version: 3
|
102
|
+
summary: making several different emailyak projects into a gem
|
103
|
+
test_files: []
|
104
|
+
|