Module: EmailYak

Defined in:
lib/emailyak.rb

Overview

Further adapted from github.com/gdb/emailyak-ruby because I couldn’t get it to install on heroku and I wanted to add to it

Defined Under Namespace

Modules: Address, Domain, Email, Util Classes: APIConnectionError, APIResponseCodeError, EmailYakError

Constant Summary collapse

@@ssl_bundle_path =

@@version = ‘0.0.1’

File.join(File.dirname(__FILE__), 'data/ca-certificates.crt')
@@api_key =
nil
@@api_base =
'https://api.emailyak.com/v1'
@@verify_ssl_certs =
true

Class Method Summary collapse

Class Method Details

.api_baseObject



65
# File 'lib/emailyak.rb', line 65

def self.api_base; @@api_base; end

.api_base=(api_base) ⇒ Object



64
# File 'lib/emailyak.rb', line 64

def self.api_base=(api_base); @@api_base = api_base; end

.api_docs_urlObject



45
46
47
# File 'lib/emailyak.rb', line 45

def self.api_docs_url
  "http://docs.emailyak.com/"
end

.api_keyObject



63
# File 'lib/emailyak.rb', line 63

def self.api_key; @@api_key; end

.api_key=(api_key) ⇒ Object



62
# File 'lib/emailyak.rb', line 62

def self.api_key=(api_key); @@api_key = api_key; end

.api_url(url = '') ⇒ Object



61
# File 'lib/emailyak.rb', line 61

def self.api_url(url=''); [@@api_base, api_key, 'json', url].join('/'); end

.docs(url = self.api_docs_url) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/emailyak.rb', line 35

def self.docs(url=self.api_docs_url)
  begin
  puts "opening api docs at #{self.api_docs_url} "
  `open #{url}`
  rescue
    false
  end
  true
end

.request(method, url, api_key, params = nil, headers = {}) ⇒ Object

Raises:



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/emailyak.rb', line 108

def self.request(method, url, api_key, params=nil, headers={})
  api_key ||= @@api_key
  raise EmailYakError.new('No API key provided.  (HINT: set your API key using "EmailYak.api_key = <API-KEY>".') unless api_key

  if !verify_ssl_certs
    unless @no_verify
      $stderr.puts "WARNING: Running without SSL cert verification.  Execute 'EmailYak.verify_ssl_certs = true' to enable verification."
      @no_verify = true
    end
    ssl_opts = { :verify_ssl => false }
  elsif !Util.file_readable(@@ssl_bundle_path)
    unless @no_bundle
      $stderr.puts "WARNING: Running without SSL cert verification because #{@@ssl_bundle_path} isn't readable"
      @no_bundle = true
    end
    ssl_opts = { :verify_ssl => false }
  else
    ssl_opts = {
      :verify_ssl => OpenSSL::SSL::VERIFY_PEER,
      :ssl_ca_file => @@ssl_bundle_path
    }
  end

  case method.to_s.downcase.to_sym
  when :get, :head, :delete
    # Make params into GET parameters
    headers = { :params => params }.merge(headers)
    payload = nil
  else
    headers = {:content_type => 'application/json'}
    payload = MultiJson.dump(params)
  end
  opts = {
    :method => method,
    :url => self.api_url(url),
    :user => api_key,
    :headers => headers,
    :open_timeout => 30,
    :payload => payload,
    :timeout => 80
  }.merge(ssl_opts)
  begin
    response = execute_request(opts)
  rescue SocketError => e
    self.handle_restclient_error(e)
  rescue NoMethodError => e
    # Work around RestClient bug
    if e.message =~ /\WRequestFailed\W/
      e = APIConnectionError.new('Unexpected HTTP response code')
      self.handle_restclient_error(e)
    else
      raise
    end
  rescue RestClient::ExceptionWithResponse => e
    puts e
    if rcode = e.http_code and rbody = e.http_body
      self.handle_api_error(rcode, rbody)
    else
      self.handle_restclient_error(e)
    end
  rescue RestClient::Exception, Errno::ECONNREFUSED => e
    self.handle_restclient_error(e)
  end

  rbody = response.body
  rcode = response.code
  begin
    resp = MultiJson.load(rbody)
  rescue
    raise APIError.new("Invalid response object from API: #{rbody.inspect} (HTTP response code was #{rcode})")
  end

  [resp, api_key]
end

.verify_ssl_certsObject



67
# File 'lib/emailyak.rb', line 67

def self.verify_ssl_certs; @@verify_ssl_certs; end

.verify_ssl_certs=(verify) ⇒ Object



66
# File 'lib/emailyak.rb', line 66

def self.verify_ssl_certs=(verify); @@verify_ssl_certs = verify; end