Created
April 11, 2016 20:43
-
-
Save sergiosouzalima/711a0f2eb5dfebba0012e29899066f3d to your computer and use it in GitHub Desktop.
Refactoring: Usando o design pattern Extract Method de Martin Fowler (http://www.refactoring.com/catalog/extractMethod.html)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Emailservices | |
module Adapters | |
class Mailchimp | |
def self.subscribe! config={}, parameters={} | |
new(config, parameters).subscribe! | |
end | |
def initialize config={}, parameters={} | |
@config = config | |
@parameters = parameters | |
end | |
def subscribe! | |
result = inline_rescue do | |
connector(api_key).lists(list_id).members.create(body: body) | |
end | |
return success?(result) ? {status: :ok, redirect: target} : {status: :error, result: result} | |
end | |
private | |
def inline_rescue | |
result = begin | |
yield | |
rescue => e | |
e.message | |
end | |
end | |
def connector auth_config | |
Gibbon::Request.new(api_key: auth_config) | |
end | |
def api_key | |
@config[:auth_config] | |
end | |
def list_id | |
JSON.parse(@config[:list_config])["list"] | |
end | |
def body | |
body = {email_address: @parameters['klickpages-email-service-integration-email'], status: 'subscribed'} | |
if (fields = @parameters['klickpages-email-service-integration-fields']) | |
body.merge!({merge_fields: fields}) | |
end | |
end | |
def success? result | |
result.class == Hash rescue false | |
end | |
def target | |
before_confirm = @config[:list_config]['before_confirm'] | |
return list_config['after_confirm'] if before_confirm.blank? | |
return before_confirm | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment