Skip to content

Commit

Permalink
Merge pull request mailjet#208 from mailjet/develop
Browse files Browse the repository at this point in the history
mailjet v1.6.0
  • Loading branch information
xab3r authored Jan 20, 2021
2 parents 9ea6fb5 + 0fbc57e commit 675f75f
Show file tree
Hide file tree
Showing 73 changed files with 3,435 additions and 864 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ doc
.yardoc
pkg

/spec/cassettes/

/config.yml

.env
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: ruby
rvm:
- 2.2.5
script:
- bundle exec rspec spec/mailjet/mailer_spec.rb spec/mailjet-spec.rb
- bundle exec rake spec
notifications:
slack:
secure: ieql5jZpe5aNhDcP81V/vUBnREfn0Kvzjsa7mtIgalS9eF/zmNAITz84quNYcEIpkdEOqU+jpGD2mstLsunZf3S3RPOFYa0tmEDZkfvbzBc4E/VsW3OA1R972ncorZKJbZdVRXmA/gwcnv0vPcO8VHgxkSV4y4Gpy/g/TW4Isfs=
33 changes: 19 additions & 14 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
#!/usr/bin/env rake
require 'rake/testtask'
require 'bundler'
Bundler::GemHelper.install_tasks
require "rspec/core/rake_task"

$:.push File.expand_path("../lib", __FILE__)

Rake::TestTask.new(:spec) do |t|
t.libs << 'lib'
t.libs << 'spec'
# t.pattern = 'spec/**/*_spec.rb'
t.verbose = true
t.test_files = Dir['spec/**/*_spec.rb']
RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = [
"spec/mailjet/api_error_spec.rb",
"spec/mailjet/apikey_spec.rb",
"spec/mailjet/mailer_spec.rb",
"spec/mailjet/resource_spec.rb",
"spec/configuration_spec.rb",
"spec/mailjet_spec.rb",
"spec/resources/contact_spec.rb",
"spec/resources/contactmetadata_spec.rb",
"spec/resources/messagehistory_spec.rb",
"spec/resources/getcontactslists_spec.rb",
"spec/resources/template_detailcontent_spec.rb",
"spec/resources/integration_spec.rb",
"spec/resources/newsletter_spec.rb",
"spec/resources/statcounters_spec.rb",
]
end


task :default => :spec
task default: [:spec]
39 changes: 19 additions & 20 deletions lib/mailjet/api_error.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
# encoding: utf-8
require 'active_support'
require "json"

module Mailjet
class ApiError < StandardError
attr_reader :code, :reason

attr_accessor :code, :reason
# @param code [Integer] HTTP response status code
# @param body [String] JSON response body
# @param request [Object] any request object
# @param url [String] request URL
# @param params [Hash] request headers and parameters
def initialize(code, body, request, url, params)
@code = code
@reason = begin
resdec = JSON.parse(body)
resdec['ErrorMessage']
rescue JSON::ParserError
body
end

message = "error #{code} while sending #{request.inspect} to #{url} with #{params.inspect}"
error_details = body.inspect
hint = "Please see https://dev.mailjet.com/guides/#status-codes for more informations on error numbers."

def initialize(code, res, request, request_path, params)
self.code = code
self.reason = ""
unless res.blank?
resdec = ActiveSupport::JSON.decode(res)
self.reason = resdec['ErrorMessage']
end
# code is ugly, output is pretty
super("error #{code} while sending #{request.inspect} to #{request_path} with #{params.inspect}\n\n" +
if res.respond_to?(:[]) && res['errors'].present?
[(res['errors'] || [])].flatten.map do |param, text|
[param, text].map(&:to_s).reject(&:blank?).join(': ')
end.join("\n")
else
res.inspect
end + "\n\nPlease see https://dev.mailjet.com/guides/#status-codes for more informations on error numbers.\n\n"
)
super("#{message}\n\n#{error_details}\n\n#{hint}\n\n")
end
end
end
8 changes: 7 additions & 1 deletion lib/mailjet/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,13 @@ def handle_exception(e, additional_headers, payload = {})
formatted_payload = (additional_headers[:content_type] == :json) ? JSON.parse(payload) : payload
params = params.merge(formatted_payload)

raise Mailjet::ApiError.new(e.http_code, e.http_body, @adapter, @adapter.url, params)
http_body = if e.http_headers[:content_type] == "application/json"
e.http_body
else
"{}"
end

raise Mailjet::ApiError.new(e.http_code, http_body, @adapter, @adapter.url, params)
end

end
Expand Down
50 changes: 26 additions & 24 deletions lib/mailjet/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,17 @@ def count(options = {})
end

def find(id, job_id = nil, options = {})
normalized_id = if id.is_a? String
URI.encode_www_form_component(id)
else
id
end

# if action method, ammend url to appropriate id
opts = define_options(options)
self.resource_path = create_action_resource_path(id, job_id) if self.action
self.resource_path = create_action_resource_path(normalized_id, job_id) if self.action
#
attributes = parse_api_json(connection(opts)[id].get(default_headers)).first
attributes = parse_api_json(connection(opts)[normalized_id].get(default_headers)).first
instanciate_from_api(attributes)

rescue Mailjet::ApiError => e
Expand Down Expand Up @@ -108,7 +114,7 @@ def create(attributes = {}, options = {})

self.new(attributes).tap do |resource|
resource.save!(opts)
resource.persisted = true
resource.attributes[:persisted] = true
end

end
Expand Down Expand Up @@ -140,17 +146,17 @@ def parse_api_json(response_json)
end

def create_action_resource_path(id, job_id = nil)
url_elements = self.resource_path.split("/")
url_elements.delete_at(url_elements.length-1) if url_elements.last.to_i > 0 #if there is a trailing number for the job id from last call, delete it
url_elements = self.resource_path.split("/")
url_elements.delete_at(url_elements.length-1) if url_elements.last.to_i > 0 #if there is a trailing number for the job id from last call, delete it

if !(url_elements[1] == "contacts" && self.action == "managemanycontacts")
url_elements[2] = id.to_s
end
if !(url_elements[1] == "contacts" && self.action == "managemanycontacts")
url_elements[2] = id.to_s
end

url_elements << job_id.to_s if job_id #if job_id exists, amend it to end of the URI
url = url_elements.join("/")
url_elements << job_id.to_s if job_id #if job_id exists, amend it to end of the URI
url = url_elements.join("/")

return url
return url
end


Expand Down Expand Up @@ -339,20 +345,16 @@ def convert_dates_from(data)

def method_missing(method_symbol, *arguments) #:nodoc:
method_name = method_symbol.to_s
if method_name =~ /(=|\?)$/
case $1
when "="
attributes[$`] = arguments.first
when "?"
attributes[$`]
end
else
return attributes[method_name] if attributes.include?(method_name)
# not set right now but we know about it
# return nil if known_attributes.include?(method_name)
super
if method_name.end_with?("=")
attributes[method_name.chop] = arguments.first
return
end
end

if attributes.include?(method_name)
return attributes[method_name]
end

super
end
end
end
6 changes: 1 addition & 5 deletions lib/mailjet/resources/contact_getcontactslists.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ def self.find(id, job_id = nil, options = {})
opts = define_options(options)
self.resource_path = create_action_resource_path(id, job_id) if self.action

raw_data = parse_api_json(connection(opts)[id].get(default_headers))

if raw_data.count == 1
return instanciate_from_api(raw_data.first)
end
raw_data = parse_api_json(connection(opts).get(default_headers))

raw_data.map do |entity|
instanciate_from_api(entity)
Expand Down
16 changes: 16 additions & 0 deletions lib/mailjet/resources/messagehistory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,21 @@ class Messagehistory

self.read_only = true

def self.find(id, job_id = nil, options = {})
opts = define_options(options)
self.resource_path = create_action_resource_path(id, job_id) if self.action

raw_data = parse_api_json(connection(opts)[id].get(default_headers))

raw_data.map do |entity|
instanciate_from_api(entity)
end
rescue Mailjet::ApiError => e
if e.code == 404
nil
else
raise e
end
end
end
end
3 changes: 1 addition & 2 deletions lib/mailjet/resources/newsletter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class Newsletter
self.resource_path = 'REST/newsletter'
self.public_operations = [:get, :put, :post]
self.filters = [:campaign, :contacts_list, :delivered_at, :edit_mode, :is_archived, :is_campaign, :is_deleted, :is_handled, :is_starred, :modified, :news_letter_template, :segmentation, :status, :subject]
self.resourceprop = [:callback, :campaign, :contacts_list, :created_at, :delivered_at, :edit_mode, :edit_type, :footer, :footer_address, :footer_wysiwyg_type, :header_filename, :header_link, :header_text, :header_url, :id, :ip, :is_handled, :is_starred, :is_text_part_included, :locale, :modified_at, :permalink, :permalink_host, :permalink_wysiwyg_type, :politeness_mode, :reply_email, :segmentation, :sender, :sender_email, :sender_name, :status, :subject, :template, :test_address, :title, :url, 'CampaignID', 'CampaignALT', 'ContactsListID', 'ContactsListALT', 'SegmentationID', 'SegmentationALT', :contacts_list_id]

self.resourceprop = [:callback, :campaign, :contacts_list, :created_at, :delivered_at, :edit_mode, :edit_type, :footer, :footer_address, :footer_wysiwyg_type, :header_filename, :header_link, :header_text, :header_url, :id, :ip, :is_handled, :is_starred, :is_text_part_included, :locale, :modified_at, :permalink, :permalink_host, :permalink_wysiwyg_type, :politeness_mode, :reply_email, :segmentation, :segmentation_id, :sender, :sender_email, :sender_name, :status, :subject, :template, :test_address, :title, :url, 'CampaignID', 'CampaignALT', 'ContactsListID', 'ContactsListALT', 'SegmentationID', 'SegmentationALT', :contacts_list_id]
end
end
33 changes: 33 additions & 0 deletions lib/mailjet/resources/statcounters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Mailjet
class Statcounters
include Mailjet::Resource
self.resource_path = 'REST/statcounters'
self.public_operations = [:get]
self.filters = [:source_id, :counter_resolution, :counter_source, :counter_timing]
self.resourceprop = [
:api_key_id,
:event_click_delay,
:event_clicked_count,
:event_open_delay,
:event_opened_count,
:event_spam_count,
:event_unsubscribed_count,
:event_workflow_exited_count,
:message_blocked_count,
:message_clicked_count,
:message_deferred_count,
:message_hard_bounced_count,
:message_opened_count,
:message_queued_count,
:message_sent_count,
:message_soft_bounced_count,
:message_spam_count,
:message_unsubscribed_count,
:message_work_flow_exited_count,
:source_id,
:timeslice,
:total,
]
self.read_only = true
end
end
15 changes: 15 additions & 0 deletions lib/mailjet/resources/template_detailcontent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,20 @@ class Template_detailcontent
self.filters = [:api_key, :categories, :categories_selection_method, :edit_mode, :name, :owner_type, :purposes, :purposes_selection_method, :user]
self.resourceprop = [:author, :categories, :copyright, :description, :edit_mode, :is_starred, :name, :owner_type, :presets, :purposes]

def self.find(id, options = {})
self.resource_path = create_action_resource_path(id)

opts = define_options(options)
response = connection(opts).get(default_headers)
attributes = parse_api_json(response).first

instanciate_from_api(attributes)
rescue Mailjet::ApiError => e
if e.code == 404
nil
else
raise e
end
end
end
end
2 changes: 1 addition & 1 deletion lib/mailjet/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Mailjet
VERSION = "1.5.4"
VERSION = "1.6.0"
end
17 changes: 4 additions & 13 deletions mailjet.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,19 @@ Gem::Specification.new do |s|
}


s.files = Dir["{lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.md"]
s.test_files = Dir["test/**/*"]

s.add_dependency "activesupport", ">= 3.1.0"
s.add_dependency "rack", ">= 1.4.0"
s.add_dependency "rest-client"
s.add_dependency "rest-client", ">= 2.0.0"
s.add_development_dependency "actionmailer", ">= 3.0.9"
s.add_development_dependency "minitest"
s.add_development_dependency "minitest-matchers"
s.add_development_dependency "minitest-spec-context"
s.add_development_dependency "turn"
s.add_development_dependency "rake"
s.add_development_dependency "json"
s.add_development_dependency "mocha"
s.add_development_dependency "webmock"
s.add_development_dependency "vcr"
s.add_development_dependency 'guard'
s.add_development_dependency 'guard-minitest'
# s.add_development_dependency 'debugger' #removed for compatability reasons
s.add_development_dependency "rspec" #added this
s.add_development_dependency "rspec-expectations" #added this
s.add_development_dependency "dotenv" #added this
s.add_development_dependency "rspec"
s.add_development_dependency "dotenv"

s.required_ruby_version = ">= 2.2.0"
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 675f75f

Please sign in to comment.