Skip to content

Commit

Permalink
Pass "perform_api_call" as a connection options
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud Breton committed Jun 5, 2017
1 parent ce2877b commit 886a46a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
25 changes: 14 additions & 11 deletions lib/mailjet/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
module Mailjet
class Connection

attr_accessor :adapter, :public_operations, :read_only
attr_accessor :adapter, :public_operations, :read_only, :perform_api_call
alias :read_only? :read_only

delegate :options, :concat_urls, :url, to: :adapter
Expand Down Expand Up @@ -37,36 +37,39 @@ def initialize(end_point, api_key, secret_key, options = {})
self.read_only = options[:read_only]
# self.adapter = adapter_class.new(end_point, options.merge(user: api_key, password: secret_key, :verify_ssl => false, content_type: 'application/json'))
self.adapter = adapter_class.new(end_point, options.merge(user: api_key, password: secret_key, content_type: 'application/json'))
self.perform_api_call = options.key?(:perform_api_call) ? options[:perform_api_call] : true
end

def get(additional_headers = {}, perform_api_call, &block)
handle_api_call(:get, additional_headers, perform_api_call, &block)
def get(additional_headers = {}, &block)
handle_api_call(:get, additional_headers, &block)
end

def post(payload, additional_headers = {}, perform_api_call, &block)
handle_api_call(:post, additional_headers, payload, perform_api_call, &block)
def post(payload, additional_headers = {}, &block)
handle_api_call(:post, additional_headers, payload, &block)
end

def put(payload, additional_headers = {}, perform_api_call, &block)
handle_api_call(:put, additional_headers, payload, perform_api_call, &block)
def put(payload, additional_headers = {}, &block)
handle_api_call(:put, additional_headers, payload, &block)
end

def delete(additional_headers = {}, perform_api_call, &block)
handle_api_call(:delete, additional_headers, perform_api_call, &block)
def delete(additional_headers = {}, &block)
handle_api_call(:delete, additional_headers, &block)
end

private

def handle_api_call(method, additional_headers = {}, payload = {}, perform_api_call, &block)
def handle_api_call(method, additional_headers = {}, payload = {}, &block)
formatted_payload = (additional_headers[:content_type] == :json) ? payload.to_json : payload
raise Mailjet::MethodNotAllowed unless method_allowed(method)

if perform_api_call
if self.perform_api_call
if [:get, :delete].include?(method)
@adapter.send(method, additional_headers, &block)
else
@adapter.send(method, formatted_payload, additional_headers, &block)
end
else
return {'Count': 0, 'Data': [mock_api_call: true], 'Total': 0}.to_json
end
rescue RestClient::Exception => e
handle_exception(e, additional_headers, formatted_payload)
Expand Down
18 changes: 11 additions & 7 deletions lib/mailjet/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def self.default_connection(options = {})
options[:api_key] || Mailjet.config.api_key,
options[:secret_key] || Mailjet.config.secret_key,
public_operations: public_operations,
read_only: read_only)
read_only: read_only,
perform_api_call: options[:perform_api_call])
end

def self.default_headers
Expand All @@ -57,14 +58,14 @@ def first(params = {}, options = {})
def all(params = {}, options = {})
opts = change_resource_path(options)
params = format_params(params)
response = connection(opts).get(default_headers.merge(params: params), opts[:perform_api_call])
response = connection(opts).get(default_headers.merge(params: params))
attribute_array = parse_api_json(response)
attribute_array.map{ |attributes| instanciate_from_api(attributes) }
end

def count(options = {})
opts = change_resource_path(options)
response_json = connection(opts).get(default_headers.merge(params: {limit: 1, countrecords: 1}), opts[:perform_api_call])
response_json = connection(opts).get(default_headers.merge(params: {limit: 1, countrecords: 1}))
response_hash = ActiveSupport::JSON.decode(response_json)
response_hash['Total']
end
Expand All @@ -74,7 +75,7 @@ def find(id, job_id = nil, options = {})
opts = change_resource_path(options)
self.resource_path = create_action_resource_path(id, job_id) if self.action
#
attributes = parse_api_json(connection(opts)[id].get(default_headers, opts[:perform_api_call])).first
attributes = parse_api_json(connection(opts)[id].get(default_headers)).first
instanciate_from_api(attributes)

rescue Mailjet::ApiError => e
Expand Down Expand Up @@ -120,6 +121,7 @@ def instanciate_from_api(attributes = {})

def parse_api_json(response_json)
response_hash = ActiveSupport::JSON.decode(response_json)

#Take the response from the API and put it through a method -- taken from the ActiveSupport library -- which converts
#the date-time from "2014-05-19T15:31:09Z" to "Mon, 19 May 2014 15:31:09 +0000" format.
response_hash = convert_dates_from(response_hash)
Expand Down Expand Up @@ -244,15 +246,17 @@ def persisted?
end

def save(options = {})
opts = self.class.change_resource_path(options)

if persisted?
# case where the entity is updated
response = connection(options)[attributes[:id]].put(formatted_payload, default_headers, options[:perform_api_call])
response = connection(opts)[attributes[:id]].put(formatted_payload, default_headers)
else
# case where the entity is created
response = connection(options).post(formatted_payload, default_headers, options[:perform_api_call])
response = connection(opts).post(formatted_payload, default_headers)
end

if options[:perform_api_call] && !persisted?
if opts[:perform_api_call] && !persisted?
# get attributes only for entity creation
self.attributes = if self.resource_path == 'send'
ActiveSupport::JSON.decode(response)
Expand Down

0 comments on commit 886a46a

Please sign in to comment.