Skip to content

Commit

Permalink
add tags support, plus:
Browse files Browse the repository at this point in the history
- add .env.local.sample file which can be copied to get a working .env.local which is needed for testing purposes
- pull out some of the hard coded values in the tests into those env variables
- accept api_key and api_secret in client initialization
- default api_key and api_secret to nil so that when making a request the string 'api_key' and 'api_secret' aren't sent to convertkit (this is a problem when you dont need/want to specify api_secret, and that string is sent along anyway)
- there was some mixing of tabs and spaces, i think i caught them all and made them spaces
- bump to 0.0.2
- removed the need to make 2 requests per test (use same response to check success and body)
  • Loading branch information
rfunduk committed Jun 10, 2016
1 parent 9483d6a commit 6c94e18
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 55 deletions.
16 changes: 16 additions & 0 deletions .env.local.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# copy this file to .env.local and substitute real values from
# your own convertkit account

API_SECRET='ABC'
API_KEY='DEF'

# a subscriber to retrieve
SUBSCRIBER_ID='GHI'

# a tag to retrieve and add a subscriber to
TAG_ID='JKL'

# a sequence to retrieve and add a subscriber to
SEQUENCE_ID='MNO'
# tags to add to a new subscriber when adding to a sequence
TAGS='PQR,STU'
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ response.status

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
After checking out the repo, run `bin/setup` to install dependencies. Then, copy `.env.local.sample` to `.env.local` and substitute your own real values from your account. Finally, run `rake spec` to run the tests.

You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).

Expand Down
12 changes: 7 additions & 5 deletions lib/convertkit/client.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "convertkit/client/sequences"
require "convertkit/client/subscribers"
require "convertkit/client/tags"
require "faraday"
require "faraday_middleware"
require "json"
Expand All @@ -8,12 +9,13 @@ module Convertkit
class Client
include Subscribers
include Sequences
include Tags

attr_accessor :api_secret, :api_key

def initialize
@api_secret = Convertkit.configuration.api_secret
@api_key = Convertkit.configuration.api_key
def initialize( api_key=nil, api_secret=nil )
@api_secret = api_secret || Convertkit.configuration.api_secret
@api_key = api_key || Convertkit.configuration.api_key
end

def content_type
Expand All @@ -29,8 +31,8 @@ def connection
f.headers['Content-Type'] = content_type
f.headers['Accept'] = "*/*"

f.params['api_secret'] = api_secret
f.params['api_key'] = api_key
f.params['api_secret'] = api_secret if api_secret
f.params['api_key'] = api_key if api_key

f.response :json, content_type: /\bjson$/
end
Expand Down
25 changes: 13 additions & 12 deletions lib/convertkit/client/sequences.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
module Convertkit
class Client
module Sequences
def sequences
connection.get("sequences")
end
module Sequences
def sequences
connection.get("sequences")
end

def add_subscriber_to_sequence(sequence_id, email, options = {})
connection.post("sequences/#{sequence_id}/subscribe?email=#{email}") do |f|
f.params['first_name'] = options[:first_name]
f.params['fields'] = options[:fields]
f.params['tags'] = options[:tags]
end
end
end
def add_subscriber_to_sequence(sequence_id, email, options = {})
connection.post("sequences/#{sequence_id}/subscribe") do |f|
f.params['email'] = email
f.params['first_name'] = options[:first_name]
f.params['fields'] = options[:fields]
f.params['tags'] = options[:tags]
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/convertkit/client/subscribers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def subscribers(options = {})
end

def subscriber(subscriber_id)
connection.get("subscribers/#{subscriber_id}")
connection.get("subscribers/#{subscriber_id}")
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions lib/convertkit/client/tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Convertkit
class Client
module Tags
def tags
connection.get("tags")
end

def add_subscriber_to_tag(tag_id, email, options = {})
connection.post("tags/#{tag_id}/subscribe") do |f|
f.params['email'] = email
f.params['first_name'] = options[:first_name]
f.params['fields'] = options[:fields]
f.params['tags'] = options[:tags]
end
end
end
end
end
8 changes: 4 additions & 4 deletions lib/convertkit/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module Convertkit
class Configuration
attr_accessor :api_secret, :api_key

def initialize
@api_secret = "api_secret"
@api_key = "api_key"
@api_secret = nil
@api_key = nil
end
end
end
end
4 changes: 2 additions & 2 deletions lib/convertkit/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Convertkit
VERSION = "0.0.1"
end
VERSION = "0.0.2"
end
43 changes: 22 additions & 21 deletions spec/convertkit/client/sequences_spec.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
require "spec_helper"

module Convertkit
class Client
describe Sequences do
before do
Convertkit.configure do |config|
config.api_secret = ENV["API_SECRET"]
config.api_key = ENV["API_KEY"]
class Client
describe Sequences do
before do
Convertkit.configure do |config|
config.api_secret = ENV["API_SECRET"]
config.api_key = ENV["API_KEY"]
end

@client = Convertkit::Client.new
end

describe "#sequences" do
it "sends the right request" do
expect(@client.sequences.success?).to be_truthy

expect(@client.sequences.body).to_not eql({"error"=>"Authorization Failed","message"=>"API Key not present"})
end
it "sends the right request" do
r = @client.sequences
expect(r.success?).to be_truthy
expect(r.body).to_not eql({"error"=>"Authorization Failed","message"=>"API Key not present"})
end
end

describe "#add_subscriber_to_sequence" do
it "sends the right request" do
sequence_id = "15402"
email = "[email protected]"
it "sends the right request" do
sequence_id = ENV['SEQUENCE_ID']
email = "crt-sequences+#{Time.now.to_i}@example.com"
tags = ENV['TAGS']

expect(@client.add_subscriber_to_sequence(sequence_id, email,first_name: "Raymond Cudjoe", tags: "26471,45372")).to be_truthy

expect(@client.add_subscriber_to_sequence(sequence_id, email).body).to_not eql({"error"=>"Missing parameter","message"=>"Subscriber email is required"})
end
r = @client.add_subscriber_to_sequence(sequence_id, email, tags: tags)
expect(r.success?).to be_truthy
expect(r.body).to_not eql({"error"=>"Missing parameter","message"=>"Subscriber email is required"})
end
end
end
end
end
end
end
end
19 changes: 11 additions & 8 deletions spec/convertkit/client/subscribers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,32 @@
module Convertkit
class Client
describe Subscribers do
before do
before do
Convertkit.configure do |config|
config.api_secret = ENV["API_SECRET"]
config.api_key = ENV["API_KEY"]
end

@client = Convertkit::Client.new
end

describe "#subscribers" do
it "sends the right request" do
expect(@client.subscribers.success?).to be_truthy
expect(@client.subscribers.body).to_not eql({"error"=>"Authorization Failed", "message"=>"API Key not present"})
r = @client.subscribers
expect(r.success?).to be_truthy
expect(r.body).to_not eql({"error"=>"Authorization Failed", "message"=>"API Key not present"})
end
end

describe "#subscriber" do
it "sends the right request" do
subscriber_id = "17681944"
expect(@client.subscriber(subscriber_id).success?).to be_truthy
expect(@client.subscriber(subscriber_id).body).to_not eql({"error"=>"Not Found", "message"=>"The entity you were trying to find doesn't exist"})
subscriber_id = ENV['SUBSCRIBER_ID']

r = @client.subscriber(subscriber_id)
expect(r.success?).to be_truthy
expect(r.body).to_not eql({"error"=>"Not Found", "message"=>"The entity you were trying to find doesn't exist"})
end
end
end
end
end
end
35 changes: 35 additions & 0 deletions spec/convertkit/client/tags_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "spec_helper"

module Convertkit
class Client
describe Tags do
before do
Convertkit.configure do |config|
config.api_secret = ENV["API_SECRET"]
config.api_key = ENV["API_KEY"]
end

@client = Convertkit::Client.new
end

describe "#tags" do
it "sends the right request" do
r = @client.tags
expect(r.success?).to be_truthy
expect(r.body).to_not eql({"error"=>"Authorization Failed","message"=>"API Key not present"})
end
end

describe "#add_subscriber_to_tag" do
it "sends the right request" do
tag_id = ENV['TAG_ID']
email = "crt-tags+#{Time.now.to_i}@example.com"

r = @client.add_subscriber_to_tag(tag_id, email)
expect(r.success?).to be_truthy
expect(r.body).to_not eql({"error"=>"Missing parameter","message"=>"Subscriber email is required"})
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'convertkit'
require "dotenv"
Dotenv.load(".env.local")
Dotenv.load(".env.local")

0 comments on commit 6c94e18

Please sign in to comment.