Skip to content

Instantly share code, notes, and snippets.

@braidn
Last active August 29, 2015 14:02
Show Gist options
  • Save braidn/0706dd1b0d640f540e8a to your computer and use it in GitHub Desktop.
Save braidn/0706dd1b0d640f540e8a to your computer and use it in GitHub Desktop.
API Test/Code
def find_list_id(name)
@api.lists["data"].detect { |r| r["name"] == name }["id"]
end
def list_id
@list_id ||= find_list_id(@list_name)
end
module Spree::Chimpy
module Interface
class List
delegate :log, to: Spree::Chimpy
def initialize(list_name, segment_name, double_opt_in, list_id)
@api = Spree::Chimpy.api
@list_id = list_id
@segment_name = segment_name
@double_opt_in = double_opt_in
@list_name = list_name
end
def api_call
@api.lists
end
def subscribe(email, merge_vars = {}, options = {})
log "Subscribing #{email} to #{@list_name}"
api_call.subscribe(id: list_id, email_address: email, merge_vars: merge_vars, update_existing: true, double_optin: @double_opt_in, email_type: 'html')
segment([email]) if options[:customer]
end
Failures:
1) Spree::Chimpy::Interface::List subscribes
Failure/Error: interface.subscribe("[email protected]", 'SIZE' => '10')
NoMethodError:
undefined method `subscribe' for {"data"=>[{"name"=>"Members", "id"=>"a3d3"}]}:Hash
# ./lib/spree/chimpy/interface/list.rb:21:in `subscribe'
# ./spec/lib/list_interface_spec.rb:23:in `block (2 levels) in <top (required)>'
2) Spree::Chimpy::Interface::List segments users
Failure/Error: interface.subscribe("[email protected]", {'SIZE' => '10'}, {customer: true})
NoMethodError:
undefined method `subscribe' for {"data"=>[{"name"=>"Members", "id"=>"a3d3"}]}:Hash
# ./lib/spree/chimpy/interface/list.rb:21:in `subscribe'
# ./spec/lib/list_interface_spec.rb:47:in `block
require 'pry-debugger'
describe Spree::Chimpy::Interface::List do
let(:interface) { described_class.new('Members', 'customers', true, nil) }
let(:api) { double(:api) }
let(:api_call) { double(:api_call) }
let(:data) { {"data" => [{"name" => "Members", "id" => "a3d3" }]} }
before do
Spree::Chimpy::Config.key = '1234'
Mailchimp::API.should_receive(:new).with('1234', { timeout: 60 }).and_return(api)
api.stub(:lists).and_return(data)
end
it "subscribes" do
expect{ api.lists }.to receive(:subscribe).
with({:id => 'a3d3',
:email_address => '[email protected]',
:merge_vars => {'SIZE' => '10'},
:email_type => 'html', :double_optin => true,
:update_existing => true})
interface.subscribe("[email protected]", 'SIZE' => '10')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment