Skip to content

Commit

Permalink
Merge branch 'release/3.1.1'
Browse files Browse the repository at this point in the history
* release/3.1.1:
  3.1.1
  Removed unecessary require
  Updated travis url
  Moved inspect with email to verification resource since others won't have it. Also updated test.
  Fixed error handling to be more standard. Fixed bin/console that didn't work properly. Improved tests to not load active_record except where needed. Added requires for json and net/http that are needed in plain ruby environments.
  Add #to_json on APIResources
  • Loading branch information
jclusso committed Nov 23, 2021
2 parents 9475df5 + a659398 commit 57b9746
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
emailable (3.0.2)
emailable (3.1.1)

GEM
remote: https://rubygems.org/
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Emailable Ruby Library

[![Build Status](https://app.travis-ci.com/emailable/emailable-ruby.svg)](https://travis-ci.com/emailable/emailable-ruby)
[![Build Status](https://app.travis-ci.com/emailable/emailable-ruby.svg)](https://app.travis-ci.com/emailable/emailable-ruby)
[![Maintainability](https://api.codeclimate.com/v1/badges/e7eef54e491adec95e6d/maintainability)](https://codeclimate.com/github/emailable/emailable-ruby/maintainability)

This is the official ruby wrapper for the Emailable API.
Expand Down
8 changes: 4 additions & 4 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "emailable/ruby"
require 'bundler/setup'
require 'emailable'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# require 'pry'
# Pry.start

require "irb"
require 'irb'
IRB.start(__FILE__)
26 changes: 5 additions & 21 deletions lib/emailable.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
require 'net/http'
require 'json'

require 'emailable/version'
require 'emailable/errors'
require 'emailable/client'
require 'emailable/response'
require 'emailable/batch'
Expand Down Expand Up @@ -33,9 +37,7 @@ def verify(email, smtp: nil, accept_all: nil, timeout: nil)
response = client.request(:get, 'verify', opts)

if response.status == 249
raise Emailable::TimeoutError.new(
code: response.status, message: response.body
)
raise Emailable::TimeoutError.new(response.body)
else
Verification.new(response.body)
end
Expand All @@ -47,22 +49,4 @@ def account
Account.new(response.body)
end


class Error < StandardError
attr_accessor :code, :message

def initialize(code: nil, message: nil)
@code = code
@message = message
end
end
class BadRequestError < Error; end
class UnauthorizedError < Error; end
class PaymentRequiredError < Error; end
class ForbiddenError < Error; end
class NotFoundError < Error; end
class TooManyRequestsError < Error; end
class InternalServerError < Error; end
class ServiceUnavailableError < Error; end
class TimeoutError < Error; end
end
27 changes: 11 additions & 16 deletions lib/emailable/client.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
module Emailable
class Client
ERRORS = {
400 => BadRequestError,
401 => UnauthorizedError,
402 => PaymentRequiredError,
403 => ForbiddenError,
404 => NotFoundError,
429 => TooManyRequestsError,
500 => InternalServerError,
503 => ServiceUnavailableError
}.freeze

def initialize
@base_url = 'https://api.emailable.com/v1'
Expand Down Expand Up @@ -33,22 +43,7 @@ def request(method, endpoint, params = {})
status = response.status
return response if status.between?(200, 299)

error_attributes = {
message: response.body['message'],
code: status
}
error_map = {
'400' => BadRequestError,
'401' => UnauthorizedError,
'402' => PaymentRequiredError,
'403' => ForbiddenError,
'404' => NotFoundError,
'429' => TooManyRequestsError,
'500' => InternalServerError,
'503' => ServiceUnavailableError
}

raise error_map[status.to_s].new(error_attributes)
raise ERRORS[status].new(response.body['message'])
end

private
Expand Down
12 changes: 12 additions & 0 deletions lib/emailable/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Emailable
class Error < StandardError; end
class BadRequestError < Error; end
class UnauthorizedError < Error; end
class PaymentRequiredError < Error; end
class ForbiddenError < Error; end
class NotFoundError < Error; end
class TooManyRequestsError < Error; end
class InternalServerError < Error; end
class ServiceUnavailableError < Error; end
class TimeoutError < Error; end
end
20 changes: 14 additions & 6 deletions lib/emailable/resources/api_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ def initialize(attributes = {})
end
end

def inspect
ivars = instance_variables.map do |e|
[e.to_s.delete('@'), instance_variable_get(e)]
def to_h
instance_variables.map do |e|
[e.to_s.delete('@').to_sym, instance_variable_get(e)]
end.to_h
fmtted_email = @email ? " #{@email}" : ''
"#<#{self.class}:0x#{(object_id << 1).to_s(16)}#{fmtted_email}> JSON: " +
JSON.pretty_generate(ivars)
end

alias_method :to_hash, :to_h

def to_json
JSON.generate(to_h)
end

def inspect
"#<#{self.class}:0x#{(object_id << 1).to_s(16)}> JSON: " +
JSON.pretty_generate(to_h)
end

end
Expand Down
5 changes: 5 additions & 0 deletions lib/emailable/resources/verification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ class Verification < APIResource
end
end

def inspect
"#<#{self.class}:0x#{(object_id << 1).to_s(16)}#{@email}> JSON: " +
JSON.pretty_generate(to_h)
end

end
end
2 changes: 1 addition & 1 deletion lib/emailable/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Emailable
VERSION = '3.1.0'
VERSION = '3.1.1'
end
1 change: 1 addition & 0 deletions test/email_validator_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'active_model'
require 'test_helper'

class EmailValidatorTest < Minitest::Test
Expand Down
49 changes: 49 additions & 0 deletions test/emailable/resources/api_resource_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'test_helper'

module Emailable
module Resources
class APIResourceTest < Minitest::Test
class Example < APIResource
attr_accessor :foo, :bar, :baz
end

def setup
@e = Example.new(foo: 'abc', bar: 123, baz: true)
end

def test_init
assert_equal 'abc', @e.foo
assert_equal 123, @e.bar
assert_equal true, @e.baz
end

def test_to_h
correct = {
foo: 'abc',
bar: 123,
baz: true,
}
assert_equal correct, @e.to_h
assert_equal correct, @e.to_hash
end

def test_to_json
correct = %q|{"foo":"abc","bar":123,"baz":true}|
assert_equal correct, @e.to_json
end

def test_inspect
correct = <<~STR.chomp
#<> JSON: {
"foo": "abc",
"bar": 123,
"baz": true
}
STR
output = @e.inspect.gsub(/<.*>/, '<>')
assert_equal correct, output
end
end

end
end
2 changes: 0 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
require 'active_model'
require 'emailable'

require 'pry'
Expand Down

0 comments on commit 57b9746

Please sign in to comment.