From 4d6311718551f16647f0ae556238f2cb7d14dda7 Mon Sep 17 00:00:00 2001 From: Petar Date: Tue, 19 Sep 2023 10:27:20 -0400 Subject: [PATCH 01/26] Prepare the namespace for the v4 API --- Gemfile | 2 +- README.md | 26 +++++----- bin/console | 4 +- ...ruby.gemspec => convertkit_v4-ruby.gemspec | 14 +++--- lib/convertkit/client.rb | 31 ------------ lib/{convertkit.rb => convertkit_v4.rb} | 8 +-- lib/convertkit_v4/client.rb | 31 ++++++++++++ .../client/account.rb | 2 +- .../client/custom_fields.rb | 2 +- .../client/forms.rb | 2 +- .../client/sequences.rb | 2 +- .../client/subscribers.rb | 2 +- .../client/tags.rb | 2 +- .../client/webhooks.rb | 2 +- .../configuration.rb | 2 +- .../connection.rb | 10 ++-- lib/{convertkit => convertkit_v4}/errors.rb | 2 +- lib/{convertkit => convertkit_v4}/version.rb | 2 +- spec/convertkit/convertkit_spec.rb | 49 ------------------- .../client/account_spec.rb | 4 +- .../client/custom_fields_spec.rb | 4 +- .../client/forms_spec.rb | 4 +- .../client/sequences_spec.rb | 6 +-- .../client/subscribers_spec.rb | 6 +-- .../client/tags_spec.rb | 6 +-- .../client/webhook_spec.rb | 6 +-- .../client_spec.rb | 4 +- .../connection_spec.rb | 2 +- spec/convertkit_v4/convertkit_spec.rb | 49 +++++++++++++++++++ spec/spec_helper.rb | 6 +-- 30 files changed, 145 insertions(+), 147 deletions(-) rename convertkit-ruby.gemspec => convertkit_v4-ruby.gemspec (77%) delete mode 100644 lib/convertkit/client.rb rename lib/{convertkit.rb => convertkit_v4.rb} (60%) create mode 100644 lib/convertkit_v4/client.rb rename lib/{convertkit => convertkit_v4}/client/account.rb (85%) rename lib/{convertkit => convertkit_v4}/client/custom_fields.rb (96%) rename lib/{convertkit => convertkit_v4}/client/forms.rb (95%) rename lib/{convertkit => convertkit_v4}/client/sequences.rb (95%) rename lib/{convertkit => convertkit_v4}/client/subscribers.rb (98%) rename lib/{convertkit => convertkit_v4}/client/tags.rb (98%) rename lib/{convertkit => convertkit_v4}/client/webhooks.rb (95%) rename lib/{convertkit => convertkit_v4}/configuration.rb (90%) rename lib/{convertkit => convertkit_v4}/connection.rb (90%) rename lib/{convertkit => convertkit_v4}/errors.rb (93%) rename lib/{convertkit => convertkit_v4}/version.rb (53%) delete mode 100644 spec/convertkit/convertkit_spec.rb rename spec/{convertkit => convertkit_v4}/client/account_spec.rb (85%) rename spec/{convertkit => convertkit_v4}/client/custom_fields_spec.rb (95%) rename spec/{convertkit => convertkit_v4}/client/forms_spec.rb (91%) rename spec/{convertkit => convertkit_v4}/client/sequences_spec.rb (90%) rename spec/{convertkit => convertkit_v4}/client/subscribers_spec.rb (96%) rename spec/{convertkit => convertkit_v4}/client/tags_spec.rb (96%) rename spec/{convertkit => convertkit_v4}/client/webhook_spec.rb (91%) rename spec/{convertkit => convertkit_v4}/client_spec.rb (85%) rename spec/{convertkit => convertkit_v4}/connection_spec.rb (98%) create mode 100644 spec/convertkit_v4/convertkit_spec.rb diff --git a/Gemfile b/Gemfile index eacacf1..a99f74c 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ source 'https://rubygems.org' -# Specify your gem's dependencies in convertkit-ruby.gemspec +# Specify your gem's dependencies in convertkit_v4-ruby.gemspec gemspec gem 'dotenv', '~> 2.1', '>= 2.1.1' diff --git a/README.md b/README.md index 9258df4..13e9133 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ -# Convertkit Ruby Client +# Convertkit V4 Ruby Client -A Ruby toolkit for [Convertkit](https://convertkit.com/) API. +A Ruby toolkit for [ConvertkitV4](https://convertkit.com/) API. ## Installation Add this line to your application's Gemfile: ```ruby -gem 'convertkit-ruby', require: 'convertkit' +gem 'convertkit_v4-ruby', require: 'convertkit_v4' ``` And then execute: @@ -16,29 +16,29 @@ And then execute: Or install it yourself as: - $ gem install convertkit-ruby + $ gem install convertkit_v4-ruby ## Authentication -For private integrations, use your personal ``API_KEY`` and ``API_SECRET`` found in [your account settings.](https://app.convertkit.com/account/edit) +For private integrations, use your personal ``API_KEY`` and ``API_SECRET`` found in [your account settings.](https://app.ConvertkitV4.com/account/edit) ```ruby require "dotenv" Dotenv.load(".env.local") -Convertkit.configure do |config| +ConvertkitV4.configure do |config| config.api_secret = ENV["API_SECRET"] config.api_key = ENV["API_KEY"] end -client = Convertkit::Client.new +client = ConvertkitV4::Client.new ``` ## Usage -Calls for Convertkit API v3 are relative to the url [http://api.convertkit.com/v3](http://api.convertkit.com/v3). +Calls for ConvertkitV4 API v3 are relative to the url [developers.convertkit.com](developers.convertkit.com). -API actions are available as methods on the client object. Currently, the Convertkit client has the following methods: +API actions are available as methods on the client object. Currently, the ConvertkitV4 client has the following methods: | Action | Method | @@ -61,9 +61,9 @@ API actions are available as methods on the client object. Currently, the Conver ## Use Cases -Here are some common use cases for the Convertkit v3 API client. +Here are some common use cases for the ConvertkitV4 v4 API client. -First configure the ``convertkit-ruby`` gem with your ``API_KEY`` and ``API_SECRET``, and initialize a new client. After that, you can fetch data from your account. +First configure the ``convertkit_v4-ruby`` gem with your ``API_KEY`` and ``API_SECRET``, and initialize a new client. After that, you can fetch data from your account. ### List subscribers @@ -99,9 +99,9 @@ To install this gem onto your local machine, run `bundle exec rake install`. To ## Contributing -Bug reports and pull requests are welcome on GitHub at https://github.com/Atomoworks/convertkit-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. +Bug reports and pull requests are welcome on GitHub at https://github.com/SparkLoop/convertkit-ruby-v4. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. -1. Fork it ( https://github.com/Atomoworks/convertkit-ruby/fork ) +1. Fork it ( https://github.com/SparkLoop/convertkit-ruby-v4/fork ) 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) diff --git a/bin/console b/bin/console index a2c3428..90b8e5a 100755 --- a/bin/console +++ b/bin/console @@ -1,12 +1,12 @@ #!/usr/bin/env ruby require "bundler/setup" -require "convertkit" +require "convertkit_v4" require "dotenv" Dotenv.load(".env.local") -Convertkit.configure do |config| +ConvertkitV4.configure do |config| config.api_secret = ENV["API_SECRET"] config.api_key = ENV["API_KEY"] end diff --git a/convertkit-ruby.gemspec b/convertkit_v4-ruby.gemspec similarity index 77% rename from convertkit-ruby.gemspec rename to convertkit_v4-ruby.gemspec index 820c518..452e5b5 100644 --- a/convertkit-ruby.gemspec +++ b/convertkit_v4-ruby.gemspec @@ -1,16 +1,16 @@ # coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'convertkit/version' +require 'convertkit_v4/version' Gem::Specification.new do |spec| - spec.name = "convertkit-ruby" - spec.version = Convertkit::VERSION - spec.authors = ["Manuel Frigerio"] - spec.email = ["manuel.frigerio@gmail.com"] + spec.name = "convertkit_v4-ruby" + spec.version = ConvertkitV4::VERSION + spec.authors = ["Manuel Frigerio", "Petar Risteski"] + spec.email = ["manuel.frigerio@gmail.com", "risteskipetar3@gmail.com"] - spec.summary = %q{A Ruby gem for interacting with the ConvertKit API v3} - spec.description = %q{A simple wrapper for the ConvertKit API} + spec.summary = %q{A Ruby gem for interacting with the Convertkit API v4} + spec.description = %q{A simple wrapper for the Convertkit V4 API} spec.homepage = "https://manuel.friger.io" spec.license = "MIT" diff --git a/lib/convertkit/client.rb b/lib/convertkit/client.rb deleted file mode 100644 index 8132d2b..0000000 --- a/lib/convertkit/client.rb +++ /dev/null @@ -1,31 +0,0 @@ -require "convertkit/client/account" -require "convertkit/client/custom_fields" -require "convertkit/client/forms" -require "convertkit/client/sequences" -require "convertkit/client/subscribers" -require "convertkit/client/webhooks" -require "convertkit/client/tags" -require "convertkit/connection" - -module Convertkit - class Client - include Account - include CustomFields - include Forms - include Sequences - include Subscribers - include Webhooks - include Tags - - attr_accessor :api_secret, :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 connection - @connection ||= Connection.new(api_key: api_key, api_secret: api_secret) - end - end -end diff --git a/lib/convertkit.rb b/lib/convertkit_v4.rb similarity index 60% rename from lib/convertkit.rb rename to lib/convertkit_v4.rb index e553f19..c21c0ae 100644 --- a/lib/convertkit.rb +++ b/lib/convertkit_v4.rb @@ -1,8 +1,8 @@ -require "convertkit/version" -require "convertkit/configuration" -require "convertkit/client" +require "convertkit_v4/version" +require "convertkit_v4/configuration" +require "convertkit_v4/client" -module Convertkit +module ConvertkitV4 class << self attr_accessor :configuration end diff --git a/lib/convertkit_v4/client.rb b/lib/convertkit_v4/client.rb new file mode 100644 index 0000000..1008e49 --- /dev/null +++ b/lib/convertkit_v4/client.rb @@ -0,0 +1,31 @@ +require "convertkit_v4/client/account" +require "convertkit_v4/client/custom_fields" +require "convertkit_v4/client/forms" +require "convertkit_v4/client/sequences" +require "convertkit_v4/client/subscribers" +require "convertkit_v4/client/webhooks" +require "convertkit_v4/client/tags" +require "convertkit_v4/connection" + +module ConvertkitV4 + class Client + include Account + include CustomFields + include Forms + include Sequences + include Subscribers + include Webhooks + include Tags + + attr_accessor :api_secret, :api_key + + def initialize( api_key=nil, api_secret=nil ) + @api_secret = api_secret || ConvertkitV4.configuration.api_secret + @api_key = api_key || ConvertkitV4.configuration.api_key + end + + def connection + @connection ||= Connection.new(api_key: api_key, api_secret: api_secret) + end + end +end diff --git a/lib/convertkit/client/account.rb b/lib/convertkit_v4/client/account.rb similarity index 85% rename from lib/convertkit/client/account.rb rename to lib/convertkit_v4/client/account.rb index ee25d3f..4c9401f 100644 --- a/lib/convertkit/client/account.rb +++ b/lib/convertkit_v4/client/account.rb @@ -1,4 +1,4 @@ -module Convertkit +module ConvertkitV4 class Client module Account def account diff --git a/lib/convertkit/client/custom_fields.rb b/lib/convertkit_v4/client/custom_fields.rb similarity index 96% rename from lib/convertkit/client/custom_fields.rb rename to lib/convertkit_v4/client/custom_fields.rb index 6c2fff7..f7a5498 100644 --- a/lib/convertkit/client/custom_fields.rb +++ b/lib/convertkit_v4/client/custom_fields.rb @@ -1,4 +1,4 @@ -module Convertkit +module ConvertkitV4 class Client module CustomFields diff --git a/lib/convertkit/client/forms.rb b/lib/convertkit_v4/client/forms.rb similarity index 95% rename from lib/convertkit/client/forms.rb rename to lib/convertkit_v4/client/forms.rb index fe84c62..7958868 100644 --- a/lib/convertkit/client/forms.rb +++ b/lib/convertkit_v4/client/forms.rb @@ -1,4 +1,4 @@ -module Convertkit +module ConvertkitV4 class Client module Forms def forms diff --git a/lib/convertkit/client/sequences.rb b/lib/convertkit_v4/client/sequences.rb similarity index 95% rename from lib/convertkit/client/sequences.rb rename to lib/convertkit_v4/client/sequences.rb index 2b1bedd..0770b33 100644 --- a/lib/convertkit/client/sequences.rb +++ b/lib/convertkit_v4/client/sequences.rb @@ -1,4 +1,4 @@ -module Convertkit +module ConvertkitV4 class Client module Sequences def sequences diff --git a/lib/convertkit/client/subscribers.rb b/lib/convertkit_v4/client/subscribers.rb similarity index 98% rename from lib/convertkit/client/subscribers.rb rename to lib/convertkit_v4/client/subscribers.rb index 92ecd86..64f66db 100644 --- a/lib/convertkit/client/subscribers.rb +++ b/lib/convertkit_v4/client/subscribers.rb @@ -1,4 +1,4 @@ -module Convertkit +module ConvertkitV4 class Client module Subscribers def subscribers(options = {}) diff --git a/lib/convertkit/client/tags.rb b/lib/convertkit_v4/client/tags.rb similarity index 98% rename from lib/convertkit/client/tags.rb rename to lib/convertkit_v4/client/tags.rb index 282e13c..4bb322b 100644 --- a/lib/convertkit/client/tags.rb +++ b/lib/convertkit_v4/client/tags.rb @@ -1,4 +1,4 @@ -module Convertkit +module ConvertkitV4 class Client module Tags def tags diff --git a/lib/convertkit/client/webhooks.rb b/lib/convertkit_v4/client/webhooks.rb similarity index 95% rename from lib/convertkit/client/webhooks.rb rename to lib/convertkit_v4/client/webhooks.rb index e2d6b5a..09a718d 100644 --- a/lib/convertkit/client/webhooks.rb +++ b/lib/convertkit_v4/client/webhooks.rb @@ -1,4 +1,4 @@ -module Convertkit +module ConvertkitV4 class Client module Webhooks def webhooks diff --git a/lib/convertkit/configuration.rb b/lib/convertkit_v4/configuration.rb similarity index 90% rename from lib/convertkit/configuration.rb rename to lib/convertkit_v4/configuration.rb index 07fe421..57e1784 100644 --- a/lib/convertkit/configuration.rb +++ b/lib/convertkit_v4/configuration.rb @@ -1,4 +1,4 @@ -module Convertkit +module ConvertkitV4 class Configuration attr_accessor :api_secret, :api_key attr_accessor :timeout, :open_timeout diff --git a/lib/convertkit/connection.rb b/lib/convertkit_v4/connection.rb similarity index 90% rename from lib/convertkit/connection.rb rename to lib/convertkit_v4/connection.rb index a3fa832..d38884d 100644 --- a/lib/convertkit/connection.rb +++ b/lib/convertkit_v4/connection.rb @@ -1,9 +1,9 @@ -require "convertkit/errors" +require "convertkit_v4/errors" require "faraday" require "faraday_middleware" require "json" -module Convertkit +module ConvertkitV4 class Connection attr_reader :http_connection @@ -38,10 +38,10 @@ def faraday_connection(api_key, api_secret) f.url_prefix = "https://api.convertkit.com/v3/" f.adapter :net_http - f.options.timeout = Convertkit.configuration.timeout - f.options.open_timeout = Convertkit.configuration.open_timeout + f.options.timeout = ConvertkitV4.configuration.timeout + f.options.open_timeout = ConvertkitV4.configuration.open_timeout - f.headers['User-Agent'] = "Convertkit-Ruby v#{Convertkit::VERSION}" + f.headers['User-Agent'] = "Convertkit-Ruby v#{ConvertkitV4::VERSION}" f.headers['Content-Type'] = content_type f.headers['Accept'] = "*/*" diff --git a/lib/convertkit/errors.rb b/lib/convertkit_v4/errors.rb similarity index 93% rename from lib/convertkit/errors.rb rename to lib/convertkit_v4/errors.rb index d168e78..0b05077 100644 --- a/lib/convertkit/errors.rb +++ b/lib/convertkit_v4/errors.rb @@ -1,4 +1,4 @@ -module Convertkit +module ConvertkitV4 class Error < StandardError; end class AuthorizationError < Error; end diff --git a/lib/convertkit/version.rb b/lib/convertkit_v4/version.rb similarity index 53% rename from lib/convertkit/version.rb rename to lib/convertkit_v4/version.rb index 48d0019..ce4adf1 100644 --- a/lib/convertkit/version.rb +++ b/lib/convertkit_v4/version.rb @@ -1,3 +1,3 @@ -module Convertkit +module ConvertkitV4 VERSION = "1.0.0" end \ No newline at end of file diff --git a/spec/convertkit/convertkit_spec.rb b/spec/convertkit/convertkit_spec.rb deleted file mode 100644 index 875c767..0000000 --- a/spec/convertkit/convertkit_spec.rb +++ /dev/null @@ -1,49 +0,0 @@ -require "spec_helper" - -describe Convertkit do - it 'has a version number' do - expect(Convertkit::VERSION).not_to be nil - end - - describe "#configure" do - before do - @old_configuration = Convertkit.configuration.dup - end - - after do - Convertkit.configuration = @old_configuration - end - - it "sets the api_secret value" do - Convertkit.configure do |config| - config.api_secret = "new_secret" - end - - expect(Convertkit.configuration.api_secret).to eql("new_secret") - end - - it "sets the api_key value" do - Convertkit.configure do |config| - config.api_key = "new_key" - end - - expect(Convertkit.configuration.api_key).to eql("new_key") - end - - it "sets the timeout value" do - Convertkit.configure do |config| - config.timeout = 10 - end - - expect(Convertkit.configuration.timeout).to eql(10) - end - - it "sets the open_timeout value" do - Convertkit.configure do |config| - config.open_timeout = 10 - end - - expect(Convertkit.configuration.open_timeout).to eql(10) - end - end -end diff --git a/spec/convertkit/client/account_spec.rb b/spec/convertkit_v4/client/account_spec.rb similarity index 85% rename from spec/convertkit/client/account_spec.rb rename to spec/convertkit_v4/client/account_spec.rb index d455d8b..2ab6d5c 100644 --- a/spec/convertkit/client/account_spec.rb +++ b/spec/convertkit_v4/client/account_spec.rb @@ -1,10 +1,10 @@ require "spec_helper" -module Convertkit +module ConvertkitV4 class Client describe Account do before do - @client = Convertkit::Client.new + @client = ConvertkitV4::Client.new end describe "#account" do diff --git a/spec/convertkit/client/custom_fields_spec.rb b/spec/convertkit_v4/client/custom_fields_spec.rb similarity index 95% rename from spec/convertkit/client/custom_fields_spec.rb rename to spec/convertkit_v4/client/custom_fields_spec.rb index 6737e1e..3305e01 100644 --- a/spec/convertkit/client/custom_fields_spec.rb +++ b/spec/convertkit_v4/client/custom_fields_spec.rb @@ -1,10 +1,10 @@ require "spec_helper" -module Convertkit +module ConvertkitV4 class Client describe CustomFields do before do - @client = Convertkit::Client.new + @client = ConvertkitV4::Client.new end describe "#custom_fields" do diff --git a/spec/convertkit/client/forms_spec.rb b/spec/convertkit_v4/client/forms_spec.rb similarity index 91% rename from spec/convertkit/client/forms_spec.rb rename to spec/convertkit_v4/client/forms_spec.rb index 7ec1a56..9d3fa1e 100644 --- a/spec/convertkit/client/forms_spec.rb +++ b/spec/convertkit_v4/client/forms_spec.rb @@ -1,10 +1,10 @@ require "spec_helper" -module Convertkit +module ConvertkitV4 class Client describe Forms do before do - @client = Convertkit::Client.new + @client = ConvertkitV4::Client.new end describe "#forms" do diff --git a/spec/convertkit/client/sequences_spec.rb b/spec/convertkit_v4/client/sequences_spec.rb similarity index 90% rename from spec/convertkit/client/sequences_spec.rb rename to spec/convertkit_v4/client/sequences_spec.rb index 5f01b97..5c515c0 100644 --- a/spec/convertkit/client/sequences_spec.rb +++ b/spec/convertkit_v4/client/sequences_spec.rb @@ -1,15 +1,15 @@ require "spec_helper" -module Convertkit +module ConvertkitV4 class Client describe Sequences do before do - Convertkit.configure do |config| + ConvertkitV4.configure do |config| config.api_secret = ENV["API_SECRET"] config.api_key = ENV["API_KEY"] end - @client = Convertkit::Client.new + @client = ConvertkitV4::Client.new end describe "#sequences" do diff --git a/spec/convertkit/client/subscribers_spec.rb b/spec/convertkit_v4/client/subscribers_spec.rb similarity index 96% rename from spec/convertkit/client/subscribers_spec.rb rename to spec/convertkit_v4/client/subscribers_spec.rb index 5657cea..cb12048 100644 --- a/spec/convertkit/client/subscribers_spec.rb +++ b/spec/convertkit_v4/client/subscribers_spec.rb @@ -1,16 +1,16 @@ require "spec_helper" require "securerandom" -module Convertkit +module ConvertkitV4 class Client describe Subscribers do before do - Convertkit.configure do |config| + ConvertkitV4.configure do |config| config.api_secret = ENV["API_SECRET"] config.api_key = ENV["API_KEY"] end - @client = Convertkit::Client.new + @client = ConvertkitV4::Client.new end describe "#subscribers" do diff --git a/spec/convertkit/client/tags_spec.rb b/spec/convertkit_v4/client/tags_spec.rb similarity index 96% rename from spec/convertkit/client/tags_spec.rb rename to spec/convertkit_v4/client/tags_spec.rb index 5e6d0c1..5ca9d6d 100644 --- a/spec/convertkit/client/tags_spec.rb +++ b/spec/convertkit_v4/client/tags_spec.rb @@ -1,16 +1,16 @@ require "spec_helper" require "securerandom" -module Convertkit +module ConvertkitV4 class Client describe Tags do before do - Convertkit.configure do |config| + ConvertkitV4.configure do |config| config.api_secret = ENV["API_SECRET"] config.api_key = ENV["API_KEY"] end - @client = Convertkit::Client.new + @client = ConvertkitV4::Client.new end describe "#tags" do diff --git a/spec/convertkit/client/webhook_spec.rb b/spec/convertkit_v4/client/webhook_spec.rb similarity index 91% rename from spec/convertkit/client/webhook_spec.rb rename to spec/convertkit_v4/client/webhook_spec.rb index 8715b08..45be9a7 100644 --- a/spec/convertkit/client/webhook_spec.rb +++ b/spec/convertkit_v4/client/webhook_spec.rb @@ -1,16 +1,16 @@ require "spec_helper" require "securerandom" -module Convertkit +module ConvertkitV4 class Client describe Webhooks do before do - Convertkit.configure do |config| + ConvertkitV4.configure do |config| config.api_secret = ENV["API_SECRET"] config.api_key = ENV["API_KEY"] end - @client = Convertkit::Client.new + @client = ConvertkitV4::Client.new @url = "https://webhook.site/2bc3b03e-0f34-4a00-b2a9-028eff304f50" @events = {"name": "subscriber.subscriber_activate"} @rule_id = 1175937 diff --git a/spec/convertkit/client_spec.rb b/spec/convertkit_v4/client_spec.rb similarity index 85% rename from spec/convertkit/client_spec.rb rename to spec/convertkit_v4/client_spec.rb index 47e9b4b..d57ce33 100644 --- a/spec/convertkit/client_spec.rb +++ b/spec/convertkit_v4/client_spec.rb @@ -1,10 +1,10 @@ require "spec_helper" require "base64" -module Convertkit +module ConvertkitV4 describe Client do before :each do - @client = Convertkit::Client.new + @client = ConvertkitV4::Client.new end describe "#initialize" do diff --git a/spec/convertkit/connection_spec.rb b/spec/convertkit_v4/connection_spec.rb similarity index 98% rename from spec/convertkit/connection_spec.rb rename to spec/convertkit_v4/connection_spec.rb index 52b41fb..3b74260 100644 --- a/spec/convertkit/connection_spec.rb +++ b/spec/convertkit_v4/connection_spec.rb @@ -1,6 +1,6 @@ require "spec_helper" -module Convertkit +module ConvertkitV4 describe Connection do describe "#initialize" do it "uses api key" do diff --git a/spec/convertkit_v4/convertkit_spec.rb b/spec/convertkit_v4/convertkit_spec.rb new file mode 100644 index 0000000..71b6f07 --- /dev/null +++ b/spec/convertkit_v4/convertkit_spec.rb @@ -0,0 +1,49 @@ +require "spec_helper" + +describe ConvertkitV4 do + it 'has a version number' do + expect(ConvertkitV4::VERSION).not_to be nil + end + + describe "#configure" do + before do + @old_configuration = ConvertkitV4.configuration.dup + end + + after do + ConvertkitV4.configuration = @old_configuration + end + + it "sets the api_secret value" do + ConvertkitV4.configure do |config| + config.api_secret = "new_secret" + end + + expect(ConvertkitV4.configuration.api_secret).to eql("new_secret") + end + + it "sets the api_key value" do + ConvertkitV4.configure do |config| + config.api_key = "new_key" + end + + expect(ConvertkitV4.configuration.api_key).to eql("new_key") + end + + it "sets the timeout value" do + ConvertkitV4.configure do |config| + config.timeout = 10 + end + + expect(ConvertkitV4.configuration.timeout).to eql(10) + end + + it "sets the open_timeout value" do + ConvertkitV4.configure do |config| + config.open_timeout = 10 + end + + expect(ConvertkitV4.configuration.open_timeout).to eql(10) + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1340fdd..30b39b1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,12 +1,12 @@ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) -require 'convertkit' +require 'convertkit_v4' require "dotenv" Dotenv.load(".env.local") require 'webmock/rspec' require 'vcr' -Convertkit.configure do |config| +ConvertkitV4.configure do |config| config.api_secret = ENV["API_SECRET"] config.api_key = ENV["API_KEY"] end @@ -20,5 +20,3 @@ end WebMock.allow_net_connect! - - From 339baaccfb45740be7422bf0cb6562c615942e73 Mon Sep 17 00:00:00 2001 From: Petar Date: Wed, 20 Sep 2023 10:20:20 -0400 Subject: [PATCH 02/26] Changes for api v4 --- bin/console | 5 ++-- fixtures/vcr_cassettes/account.yml | 4 ++-- .../vcr_cassettes/add_subscriber_to_form.yml | 4 ++-- .../vcr_cassettes/delete_custom_field.yml | 4 ++-- fixtures/vcr_cassettes/forms.yml | 4 ++-- fixtures/vcr_cassettes/get_custom_fields.yml | 4 ++-- fixtures/vcr_cassettes/new_custom_field.yml | 4 ++-- .../vcr_cassettes/update_custom_field.yml | 4 ++-- lib/convertkit_v4/client.rb | 23 +++++++++++++++---- lib/convertkit_v4/configuration.rb | 2 +- lib/convertkit_v4/connection.rb | 17 +++++++------- spec/convertkit_v4/client_spec.rb | 12 ++++++---- spec/convertkit_v4/connection_spec.rb | 17 ++++---------- ...nvertkit_spec.rb => convertkit_v4_spec.rb} | 14 +++-------- spec/spec_helper.rb | 6 ++--- 15 files changed, 63 insertions(+), 61 deletions(-) rename spec/convertkit_v4/{convertkit_spec.rb => convertkit_v4_spec.rb} (68%) diff --git a/bin/console b/bin/console index 90b8e5a..94a2fc5 100755 --- a/bin/console +++ b/bin/console @@ -7,8 +7,9 @@ require "dotenv" Dotenv.load(".env.local") ConvertkitV4.configure do |config| - config.api_secret = ENV["API_SECRET"] - config.api_key = ENV["API_KEY"] + config.client_id = ENV["CONVERTKIT_CLIENT_ID"] + config.client_secret = ENV["CONVERTKIT_CLIENT_SECRET"] + config.redirect_uri = ENV["CONVERTKIT_REDIRECT_URI"] end # You can add fixtures and/or initialization code here to make experimenting diff --git a/fixtures/vcr_cassettes/account.yml b/fixtures/vcr_cassettes/account.yml index b0b9f3b..0e29e0e 100644 --- a/fixtures/vcr_cassettes/account.yml +++ b/fixtures/vcr_cassettes/account.yml @@ -2,7 +2,7 @@ http_interactions: - request: method: get - uri: https://api.convertkit.com/v3/account?api_key=&api_secret= + uri: https://api.convertkit.com/v3/account?access_token= body: encoding: US-ASCII string: '' @@ -10,7 +10,7 @@ http_interactions: User-Agent: - Convertkit-Ruby v0.0.2 Content-Type: - - application/vnd.api+json + - application/json Accept: - "*/*" Accept-Encoding: diff --git a/fixtures/vcr_cassettes/add_subscriber_to_form.yml b/fixtures/vcr_cassettes/add_subscriber_to_form.yml index 0455c45..7cf87f3 100644 --- a/fixtures/vcr_cassettes/add_subscriber_to_form.yml +++ b/fixtures/vcr_cassettes/add_subscriber_to_form.yml @@ -2,7 +2,7 @@ http_interactions: - request: method: post - uri: https://api.convertkit.com/v3/forms/175221/subscribe?api_key=&api_secret=&email=test@example.com&fields&first_name&tags + uri: https://api.convertkit.com/v3/forms/175221/subscribe?access_token=&email=test@example.com&fields&first_name&tags body: encoding: UTF-8 string: '' @@ -10,7 +10,7 @@ http_interactions: User-Agent: - Convertkit-Ruby v0.0.2 Content-Type: - - application/vnd.api+json + - application/json Accept: - "*/*" Content-Length: diff --git a/fixtures/vcr_cassettes/delete_custom_field.yml b/fixtures/vcr_cassettes/delete_custom_field.yml index c65def7..6555f65 100644 --- a/fixtures/vcr_cassettes/delete_custom_field.yml +++ b/fixtures/vcr_cassettes/delete_custom_field.yml @@ -2,7 +2,7 @@ http_interactions: - request: method: delete - uri: https://api.convertkit.com/v3/custom_fields/11176?api_key=&api_secret= + uri: https://api.convertkit.com/v3/custom_fields/11176?access_token= body: encoding: US-ASCII string: '' @@ -10,7 +10,7 @@ http_interactions: User-Agent: - Convertkit-Ruby v0.0.2 Content-Type: - - application/vnd.api+json + - application/json Accept: - "*/*" Accept-Encoding: diff --git a/fixtures/vcr_cassettes/forms.yml b/fixtures/vcr_cassettes/forms.yml index 2783691..276a9eb 100644 --- a/fixtures/vcr_cassettes/forms.yml +++ b/fixtures/vcr_cassettes/forms.yml @@ -2,7 +2,7 @@ http_interactions: - request: method: get - uri: https://api.convertkit.com/v3/forms?api_key=&api_secret= + uri: https://api.convertkit.com/v3/forms?access_token= body: encoding: US-ASCII string: '' @@ -10,7 +10,7 @@ http_interactions: User-Agent: - Convertkit-Ruby v0.0.2 Content-Type: - - application/vnd.api+json + - application/json Accept: - "*/*" Accept-Encoding: diff --git a/fixtures/vcr_cassettes/get_custom_fields.yml b/fixtures/vcr_cassettes/get_custom_fields.yml index f5b911d..e4a809c 100644 --- a/fixtures/vcr_cassettes/get_custom_fields.yml +++ b/fixtures/vcr_cassettes/get_custom_fields.yml @@ -2,7 +2,7 @@ http_interactions: - request: method: get - uri: https://api.convertkit.com/v3/custom_fields?api_key=&api_secret= + uri: https://api.convertkit.com/v3/custom_fields?access_token= body: encoding: US-ASCII string: '' @@ -10,7 +10,7 @@ http_interactions: User-Agent: - Convertkit-Ruby v0.0.2 Content-Type: - - application/vnd.api+json + - application/json Accept: - "*/*" Accept-Encoding: diff --git a/fixtures/vcr_cassettes/new_custom_field.yml b/fixtures/vcr_cassettes/new_custom_field.yml index 8d9d555..03a76cd 100644 --- a/fixtures/vcr_cassettes/new_custom_field.yml +++ b/fixtures/vcr_cassettes/new_custom_field.yml @@ -2,7 +2,7 @@ http_interactions: - request: method: post - uri: https://api.convertkit.com/v3/custom_fields?api_key=&api_secret=&label=Occupation + uri: https://api.convertkit.com/v3/custom_fields?access_token=&label=Occupation body: encoding: UTF-8 string: '' @@ -10,7 +10,7 @@ http_interactions: User-Agent: - Convertkit-Ruby v0.0.2 Content-Type: - - application/vnd.api+json + - application/json Accept: - "*/*" Content-Length: diff --git a/fixtures/vcr_cassettes/update_custom_field.yml b/fixtures/vcr_cassettes/update_custom_field.yml index d49e84b..e0489a2 100644 --- a/fixtures/vcr_cassettes/update_custom_field.yml +++ b/fixtures/vcr_cassettes/update_custom_field.yml @@ -2,7 +2,7 @@ http_interactions: - request: method: put - uri: https://api.convertkit.com/v3/custom_fields/11184?api_key=&api_secret=&label=Discount%20Code + uri: https://api.convertkit.com/v3/custom_fields/11184?access_token=&label=Discount%20Code body: encoding: UTF-8 string: '' @@ -10,7 +10,7 @@ http_interactions: User-Agent: - Convertkit-Ruby v0.0.2 Content-Type: - - application/vnd.api+json + - application/json Accept: - "*/*" Content-Length: diff --git a/lib/convertkit_v4/client.rb b/lib/convertkit_v4/client.rb index 1008e49..3b9d104 100644 --- a/lib/convertkit_v4/client.rb +++ b/lib/convertkit_v4/client.rb @@ -17,15 +17,28 @@ class Client include Webhooks include Tags - attr_accessor :api_secret, :api_key + attr_accessor :access_token, :refresh_token, :client_id, :client_secret, :redirect_uri - def initialize( api_key=nil, api_secret=nil ) - @api_secret = api_secret || ConvertkitV4.configuration.api_secret - @api_key = api_key || ConvertkitV4.configuration.api_key + AUTH_URL = "https://app.convertkit.com/oauth/authorize" + + def initialize(access_token: nil, refresh_token: nil, client_id: nil, client_secret: nil, redirect_uri: nil) + @access_token = access_token + @refresh_token = refresh_token + + @client_id = client_id || ConvertkitV4.configuration.client_id + @client_secret = client_secret || ConvertkitV4.configuration.client_secret + @redirect_uri = redirect_uri || ConvertkitV4.configuration.redirect_uri end def connection - @connection ||= Connection.new(api_key: api_key, api_secret: api_secret) + @connection ||= Connection.new(access_token: @access_token) + end + + def authorize + Faraday.new(url: AUTH_URL).get do |req| + req.headers["Content-Type"] = "application/json" + req.params = { client_id: @client_id, redirect_uri: @redirect_uri } + end end end end diff --git a/lib/convertkit_v4/configuration.rb b/lib/convertkit_v4/configuration.rb index 57e1784..74cdfee 100644 --- a/lib/convertkit_v4/configuration.rb +++ b/lib/convertkit_v4/configuration.rb @@ -1,6 +1,6 @@ module ConvertkitV4 class Configuration - attr_accessor :api_secret, :api_key + attr_accessor :client_id, :client_secret, :redirect_uri, :access_token, :refresh_token attr_accessor :timeout, :open_timeout def initialize diff --git a/lib/convertkit_v4/connection.rb b/lib/convertkit_v4/connection.rb index d38884d..502293a 100644 --- a/lib/convertkit_v4/connection.rb +++ b/lib/convertkit_v4/connection.rb @@ -7,12 +7,15 @@ module ConvertkitV4 class Connection attr_reader :http_connection - def initialize(api_key: nil, api_secret: nil) - @http_connection = faraday_connection(api_key, api_secret) + API_URL = "https://api.convertkit.com/" + API_VERSION_PATH = "alpha/" + + def initialize(access_token: nil) + @http_connection = faraday_connection(access_token) end def content_type - "application/vnd.api+json" + "application/json" end def get(*args, &blk) @@ -33,20 +36,18 @@ def delete(*args, &blk) private - def faraday_connection(api_key, api_secret) + def faraday_connection(access_token) Faraday.new do |f| - f.url_prefix = "https://api.convertkit.com/v3/" + f.url_prefix = "#{API_URL}#{API_VERSION_PATH}" f.adapter :net_http f.options.timeout = ConvertkitV4.configuration.timeout f.options.open_timeout = ConvertkitV4.configuration.open_timeout - f.headers['User-Agent'] = "Convertkit-Ruby v#{ConvertkitV4::VERSION}" f.headers['Content-Type'] = content_type f.headers['Accept'] = "*/*" - f.params['api_secret'] = api_secret if api_secret - f.params['api_key'] = api_key if api_key + f.params['access_token'] = access_token if access_token f.response :json, content_type: /\bjson$/ end diff --git a/spec/convertkit_v4/client_spec.rb b/spec/convertkit_v4/client_spec.rb index d57ce33..05eaabc 100644 --- a/spec/convertkit_v4/client_spec.rb +++ b/spec/convertkit_v4/client_spec.rb @@ -8,12 +8,16 @@ module ConvertkitV4 end describe "#initialize" do - it "has a default api_secret value" do - expect(@client.api_secret).to eq(ENV["API_SECRET"]) + it "has a default client_id value" do + expect(@client.client_id).to eq(ENV["CONVERTKIT_CLIENT_ID"]) end - it "has a default api_key value" do - expect(@client.api_key).to eq(ENV["API_KEY"]) + it "has a default client_secret value" do + expect(@client.client_secret).to eq(ENV["CONVERTKIT_CLIENT_SECRET"]) + end + + it "has a default redirect_uri value" do + expect(@client.redirect_uri).to eq(ENV["CONVERTKIT_REDIRECT_URI"]) end end end diff --git a/spec/convertkit_v4/connection_spec.rb b/spec/convertkit_v4/connection_spec.rb index 3b74260..0c2a899 100644 --- a/spec/convertkit_v4/connection_spec.rb +++ b/spec/convertkit_v4/connection_spec.rb @@ -3,24 +3,17 @@ module ConvertkitV4 describe Connection do describe "#initialize" do - it "uses api key" do - api_key = "api_key" - connection = Connection.new(api_key: api_key) + it "uses access_token" do + access_token = "access_token" + connection = Connection.new(access_token: access_token) - expect(connection.http_connection.params["api_key"]).to eq(api_key) - end - - it "uses api secret" do - api_secret = "api_secret" - connection = Connection.new(api_secret: api_secret) - - expect(connection.http_connection.params["api_secret"]).to eq(api_secret) + expect(connection.http_connection.params["access_token"]).to eq(access_token) end end describe "error handling" do before do - @connection = Connection.new(api_key: "api_key") + @connection = Connection.new(access_token: "access_token") end it "handles 401 errors" do diff --git a/spec/convertkit_v4/convertkit_spec.rb b/spec/convertkit_v4/convertkit_v4_spec.rb similarity index 68% rename from spec/convertkit_v4/convertkit_spec.rb rename to spec/convertkit_v4/convertkit_v4_spec.rb index 71b6f07..b04c5a6 100644 --- a/spec/convertkit_v4/convertkit_spec.rb +++ b/spec/convertkit_v4/convertkit_v4_spec.rb @@ -14,20 +14,12 @@ ConvertkitV4.configuration = @old_configuration end - it "sets the api_secret value" do + it "sets the client_id value" do ConvertkitV4.configure do |config| - config.api_secret = "new_secret" + config.client_id = "new_client_id" end - expect(ConvertkitV4.configuration.api_secret).to eql("new_secret") - end - - it "sets the api_key value" do - ConvertkitV4.configure do |config| - config.api_key = "new_key" - end - - expect(ConvertkitV4.configuration.api_key).to eql("new_key") + expect(ConvertkitV4.configuration.client_id).to eql("new_client_id") end it "sets the timeout value" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 30b39b1..da97bce 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,15 +7,13 @@ require 'vcr' ConvertkitV4.configure do |config| - config.api_secret = ENV["API_SECRET"] - config.api_key = ENV["API_KEY"] + config.access_token = ENV["CONVERTKIT_ACCESS_TOKEN"] end VCR.configure do |config| config.cassette_library_dir = "fixtures/vcr_cassettes" config.hook_into :webmock - config.filter_sensitive_data("") { ENV["API_SECRET"] } - config.filter_sensitive_data("") { ENV["API_KEY"] } + config.filter_sensitive_data("") { ENV["CONVERTKIT_ACCESS_TOKEN"] } config.allow_http_connections_when_no_cassette = true end From 4249e1ed49a2b5e7428e90303464093877b8383c Mon Sep 17 00:00:00 2001 From: Petar Date: Wed, 20 Sep 2023 10:35:52 -0400 Subject: [PATCH 03/26] Access token and refresh token --- lib/convertkit_v4/client.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/convertkit_v4/client.rb b/lib/convertkit_v4/client.rb index 3b9d104..8495b5d 100644 --- a/lib/convertkit_v4/client.rb +++ b/lib/convertkit_v4/client.rb @@ -20,6 +20,7 @@ class Client attr_accessor :access_token, :refresh_token, :client_id, :client_secret, :redirect_uri AUTH_URL = "https://app.convertkit.com/oauth/authorize" + ACCESS_TOKEN_URL = "https://app.convertkit.com/oauth/token" def initialize(access_token: nil, refresh_token: nil, client_id: nil, client_secret: nil, redirect_uri: nil) @access_token = access_token @@ -40,5 +41,30 @@ def authorize req.params = { client_id: @client_id, redirect_uri: @redirect_uri } end end + + def get_access_token(code) + Faraday.new(url: ACCESS_TOKEN_URL).post do |req| + req.headers["Content-Type"] = "application/json" + req.params = { + client_id: @client_id, + client_secret: @client_secret, + code: code, + grant_type: "authorization_code", + redirect_uri: @redirect_uri + } + end + end + + def refresh_token + Faraday.new(url: ACCESS_TOKEN_URL).post do |req| + req.headers["Content-Type"] = "application/json" + req.params = { + client_id: @client_id, + client_secret: @client_secret, + refresh_token: @refresh_token, + grant_type: "refresh_token" + } + end + end end end From 71621ba228d19f6856301b4a93c4254cb3390ab3 Mon Sep 17 00:00:00 2001 From: Petar Date: Wed, 20 Sep 2023 10:38:34 -0400 Subject: [PATCH 04/26] Revoke access --- lib/convertkit_v4/client.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/convertkit_v4/client.rb b/lib/convertkit_v4/client.rb index 8495b5d..4a69183 100644 --- a/lib/convertkit_v4/client.rb +++ b/lib/convertkit_v4/client.rb @@ -21,6 +21,7 @@ class Client AUTH_URL = "https://app.convertkit.com/oauth/authorize" ACCESS_TOKEN_URL = "https://app.convertkit.com/oauth/token" + REVOKE_ACCESS_URL = "https://app.convertkit.com/oauth/revoke" def initialize(access_token: nil, refresh_token: nil, client_id: nil, client_secret: nil, redirect_uri: nil) @access_token = access_token @@ -66,5 +67,16 @@ def refresh_token } end end + + def revoke_access + Faraday.new(url: REVOKE_ACCESS_URL).post do |req| + req.headers["Content-Type"] = "application/json" + req.params = { + client_id: @client_id, + client_secret: @client_secret, + token: @access_token + } + end + end end end From 894826527de1dd0820ce14b868cd2b409699d1bf Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Tue, 26 Sep 2023 12:29:43 +0100 Subject: [PATCH 05/26] custom fields --- lib/convertkit_v4/client/custom_fields.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/convertkit_v4/client/custom_fields.rb b/lib/convertkit_v4/client/custom_fields.rb index f7a5498..84ca5ac 100644 --- a/lib/convertkit_v4/client/custom_fields.rb +++ b/lib/convertkit_v4/client/custom_fields.rb @@ -6,9 +6,11 @@ def custom_fields connection.get("custom_fields").body["custom_fields"] end - def add_custom_field(options = {}) + def add_custom_field(label) connection.post("custom_fields") do |f| - f.params['label'] = options[:label] + f.body = JSON.generate({ + label: label + }) end end @@ -16,9 +18,11 @@ def delete_custom_field(custom_field_id) connection.delete("custom_fields/#{custom_field_id}") end - def update_custom_field(custom_field_id, options = {}) + def update_custom_field(custom_field_id, label) connection.put("custom_fields/#{custom_field_id}") do |f| - f.params['label'] = options[:label] + f.body = JSON.generate({ + label: label + }) end end From 8d31572b60355f9d5ab246f0013c0377717ec7cd Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Tue, 26 Sep 2023 12:33:51 +0100 Subject: [PATCH 06/26] subscribers --- lib/convertkit_v4/client/subscribers.rb | 36 ++++++++++++++++++------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/lib/convertkit_v4/client/subscribers.rb b/lib/convertkit_v4/client/subscribers.rb index 64f66db..e96dbe1 100644 --- a/lib/convertkit_v4/client/subscribers.rb +++ b/lib/convertkit_v4/client/subscribers.rb @@ -1,6 +1,7 @@ module ConvertkitV4 class Client module Subscribers + def subscribers(options = {}) connection.get("subscribers", options) end @@ -9,27 +10,42 @@ def subscriber(subscriber_id) connection.get("subscribers/#{subscriber_id}") end - def subscriber_tags(subscriber_id) - connection.get("subscribers/#{subscriber_id}/tags") + def add_subscriber(email_address, options = {}) + response = connection.post("subscribers") do |f| + body = { + email_address: email_address + } + + body[:fields] = options[:fields] if options[:fields] + body[:first_name] = options[:first_name] if options[:first_name] + + f.body = JSON.generate(body) + end + response.body end def update_subscriber(subscriber_id, options = {}) response = connection.put("subscribers/#{subscriber_id}") do |f| - f.params["email_address"] = options[:email_address] if options[:email_address] - f.params["fields"] = options[:fields] if options[:fields] - f.params["first_name"] = options[:first_name] if options[:first_name] + body = {} + body[:email_address] = options[:email_address] if options[:email_address] + body[:fields] = options[:fields] if options[:fields] + body[:first_name] = options[:first_name] if options[:first_name] + + f.body = JSON.generate(body) end response.body end - def unsubscribe(email) - connection.put("unsubscribe") do |f| - f.params['email'] = email + def unsubscribe(subscriber_id) + connection.post("unsubscribe") do |f| + f.body = JSON.generate({ + id: subscriber_id + }) end end - def remove_tag_from_subscriber(subscriber_id, tag_id) - connection.delete("subscribers/#{subscriber_id}/tags/#{tag_id}") + def subscriber_tags(subscriber_id) + connection.get("subscribers/#{subscriber_id}/tags") end end end From 6c012b69aa488fa20dec3abf336014b6ba7f0f4f Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Tue, 26 Sep 2023 12:36:09 +0100 Subject: [PATCH 07/26] tags --- lib/convertkit_v4/client/tags.rb | 43 ++++++++------------------------ 1 file changed, 11 insertions(+), 32 deletions(-) diff --git a/lib/convertkit_v4/client/tags.rb b/lib/convertkit_v4/client/tags.rb index 4bb322b..c797647 100644 --- a/lib/convertkit_v4/client/tags.rb +++ b/lib/convertkit_v4/client/tags.rb @@ -1,50 +1,29 @@ module ConvertkitV4 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 - - def remove_tag_from_subscriber(tag_id, subscriber_id) - connection.delete("subscribers/#{subscriber_id}/tags/#{tag_id}") - end - - def remove_tag_from_subscriber_by_email(tag_id, email) - connection.post("tags/#{tag_id}/unsubscribe") do |f| - f.params['email'] = email - end - end - def create_tag(tag_name) - response = connection.post("tags") do |request| - request.params["tag"] = { name: tag_name } + response = connection.post("tags") do |f| + f.body = JSON.generate({ + name: tag_name + }) end response.body end - def create_tags(tag_names) - response = connection.post("tags") do |request| - request.params["tag"] = tag_names.map { |tag_name| { name: tag_name } } + def add_subscriber_to_tag(tag_id, options = {}) + connection.post("tags/#{tag_id}/subscribers") do |f| + f.body = JSON.generate({ + email: options[:email], + id: options[:id] + }) end - response.body end - def subscriptions_to_tag(tag_id, options = {}) - connection.get("tags/#{tag_id}/subscriptions", options) do |f| - f.params["page"] = options[:page] if options[:page] - f.params["sort_order"] = options[:sort_order] if options[:sort_order] - f.params["subscriber_state"] = options[:subscriber_state] if options[:subscriber_state] - end - end end end end From 18875f5e0a541e7edce19b1512f65550de3395d2 Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Tue, 26 Sep 2023 12:37:28 +0100 Subject: [PATCH 08/26] webhooks --- lib/convertkit_v4/client/webhooks.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/convertkit_v4/client/webhooks.rb b/lib/convertkit_v4/client/webhooks.rb index 09a718d..4ebea41 100644 --- a/lib/convertkit_v4/client/webhooks.rb +++ b/lib/convertkit_v4/client/webhooks.rb @@ -1,21 +1,21 @@ module ConvertkitV4 class Client module Webhooks + def webhooks - connection.get("automations/hooks/").body + connection.get("webhooks").body end - def create_webhook(url, events) - response = connection.post("automations/hooks") do |f| - f.params['target_url'] = url - f.params['event'] = events + def create_webhook(url, event) + response = connection.post("webhooks") do |f| + f.body = JSON.generate({ + target_url: url, + event: event + }) end response.body end - def remove_webhook(rule_id) - connection.delete("automations/hooks/#{rule_id}").body - end end end end From 1ae2a5b97982efe263f8b25cdcaa9b969989039c Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Tue, 26 Sep 2023 12:37:44 +0100 Subject: [PATCH 09/26] updated version --- lib/convertkit_v4/version.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/convertkit_v4/version.rb b/lib/convertkit_v4/version.rb index ce4adf1..4e29082 100644 --- a/lib/convertkit_v4/version.rb +++ b/lib/convertkit_v4/version.rb @@ -1,3 +1,3 @@ module ConvertkitV4 - VERSION = "1.0.0" -end \ No newline at end of file + VERSION = "1.0.5" +end From abad0f33a4aef2f888d80ecc61d8e694d4a727c7 Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Tue, 26 Sep 2023 13:57:55 +0100 Subject: [PATCH 10/26] changes --- lib/convertkit_v4/client/sequences.rb | 2 +- lib/convertkit_v4/client/subscribers.rb | 2 +- lib/convertkit_v4/client/tags.rb | 2 +- lib/convertkit_v4/client/webhooks.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/convertkit_v4/client/sequences.rb b/lib/convertkit_v4/client/sequences.rb index 0770b33..6314b34 100644 --- a/lib/convertkit_v4/client/sequences.rb +++ b/lib/convertkit_v4/client/sequences.rb @@ -2,7 +2,7 @@ module ConvertkitV4 class Client module Sequences def sequences - connection.get("sequences") + connection.get("sequences").body["sequences"] end def add_subscriber_to_sequence(sequence_id, email, options = {}) diff --git a/lib/convertkit_v4/client/subscribers.rb b/lib/convertkit_v4/client/subscribers.rb index e96dbe1..0b4c6a6 100644 --- a/lib/convertkit_v4/client/subscribers.rb +++ b/lib/convertkit_v4/client/subscribers.rb @@ -3,7 +3,7 @@ class Client module Subscribers def subscribers(options = {}) - connection.get("subscribers", options) + connection.get("subscribers", options).body end def subscriber(subscriber_id) diff --git a/lib/convertkit_v4/client/tags.rb b/lib/convertkit_v4/client/tags.rb index c797647..9773d88 100644 --- a/lib/convertkit_v4/client/tags.rb +++ b/lib/convertkit_v4/client/tags.rb @@ -3,7 +3,7 @@ class Client module Tags def tags - connection.get("tags") + connection.get("tags").body["tags"] end def create_tag(tag_name) diff --git a/lib/convertkit_v4/client/webhooks.rb b/lib/convertkit_v4/client/webhooks.rb index 4ebea41..d6363ce 100644 --- a/lib/convertkit_v4/client/webhooks.rb +++ b/lib/convertkit_v4/client/webhooks.rb @@ -3,7 +3,7 @@ class Client module Webhooks def webhooks - connection.get("webhooks").body + connection.get("webhooks").body["webhooks"] end def create_webhook(url, event) From 487040d780a326e5c706f277bce5e5a2e54932da Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Tue, 26 Sep 2023 13:58:12 +0100 Subject: [PATCH 11/26] changed version --- lib/convertkit_v4/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/convertkit_v4/version.rb b/lib/convertkit_v4/version.rb index 4e29082..ebe3ea1 100644 --- a/lib/convertkit_v4/version.rb +++ b/lib/convertkit_v4/version.rb @@ -1,3 +1,3 @@ module ConvertkitV4 - VERSION = "1.0.5" + VERSION = "1.0.8" end From d559976c6f690bdcd6be4d92b7f4888db3c95a77 Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Tue, 26 Sep 2023 14:35:12 +0100 Subject: [PATCH 12/26] values --- lib/convertkit_v4/client/custom_fields.rb | 2 ++ lib/convertkit_v4/client/subscribers.rb | 8 ++++---- lib/convertkit_v4/client/tags.rb | 1 + lib/convertkit_v4/client/webhooks.rb | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/convertkit_v4/client/custom_fields.rb b/lib/convertkit_v4/client/custom_fields.rb index 84ca5ac..0999581 100644 --- a/lib/convertkit_v4/client/custom_fields.rb +++ b/lib/convertkit_v4/client/custom_fields.rb @@ -12,6 +12,7 @@ def add_custom_field(label) label: label }) end + response.body["custom_field"] end def delete_custom_field(custom_field_id) @@ -24,6 +25,7 @@ def update_custom_field(custom_field_id, label) label: label }) end + response.body["custom_field"] end end diff --git a/lib/convertkit_v4/client/subscribers.rb b/lib/convertkit_v4/client/subscribers.rb index 0b4c6a6..41915ad 100644 --- a/lib/convertkit_v4/client/subscribers.rb +++ b/lib/convertkit_v4/client/subscribers.rb @@ -7,7 +7,7 @@ def subscribers(options = {}) end def subscriber(subscriber_id) - connection.get("subscribers/#{subscriber_id}") + connection.get("subscribers/#{subscriber_id}").body["subscriber"] end def add_subscriber(email_address, options = {}) @@ -21,7 +21,7 @@ def add_subscriber(email_address, options = {}) f.body = JSON.generate(body) end - response.body + response.body["subscriber"] end def update_subscriber(subscriber_id, options = {}) @@ -33,7 +33,7 @@ def update_subscriber(subscriber_id, options = {}) f.body = JSON.generate(body) end - response.body + response.body["subscriber"] end def unsubscribe(subscriber_id) @@ -45,7 +45,7 @@ def unsubscribe(subscriber_id) end def subscriber_tags(subscriber_id) - connection.get("subscribers/#{subscriber_id}/tags") + connection.get("subscribers/#{subscriber_id}/tags").body["tags"] end end end diff --git a/lib/convertkit_v4/client/tags.rb b/lib/convertkit_v4/client/tags.rb index 9773d88..8fe1f12 100644 --- a/lib/convertkit_v4/client/tags.rb +++ b/lib/convertkit_v4/client/tags.rb @@ -22,6 +22,7 @@ def add_subscriber_to_tag(tag_id, options = {}) id: options[:id] }) end + response.body end end diff --git a/lib/convertkit_v4/client/webhooks.rb b/lib/convertkit_v4/client/webhooks.rb index d6363ce..4cd8a6c 100644 --- a/lib/convertkit_v4/client/webhooks.rb +++ b/lib/convertkit_v4/client/webhooks.rb @@ -13,7 +13,7 @@ def create_webhook(url, event) event: event }) end - response.body + response.body["webhook"] end end From 4bb631d3c970bed5cb9889935c7bc0553c981fa0 Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Tue, 26 Sep 2023 14:35:24 +0100 Subject: [PATCH 13/26] version --- lib/convertkit_v4/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/convertkit_v4/version.rb b/lib/convertkit_v4/version.rb index ebe3ea1..4fb9c00 100644 --- a/lib/convertkit_v4/version.rb +++ b/lib/convertkit_v4/version.rb @@ -1,3 +1,3 @@ module ConvertkitV4 - VERSION = "1.0.8" + VERSION = "1.0.11" end From fae775812bc56ff68af08a65135ba66cbc6f322a Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Tue, 26 Sep 2023 15:24:31 +0100 Subject: [PATCH 14/26] changes --- lib/convertkit_v4/client/custom_fields.rb | 4 ++-- lib/convertkit_v4/client/tags.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/convertkit_v4/client/custom_fields.rb b/lib/convertkit_v4/client/custom_fields.rb index 0999581..8dc9ffc 100644 --- a/lib/convertkit_v4/client/custom_fields.rb +++ b/lib/convertkit_v4/client/custom_fields.rb @@ -7,7 +7,7 @@ def custom_fields end def add_custom_field(label) - connection.post("custom_fields") do |f| + response = connection.post("custom_fields") do |f| f.body = JSON.generate({ label: label }) @@ -20,7 +20,7 @@ def delete_custom_field(custom_field_id) end def update_custom_field(custom_field_id, label) - connection.put("custom_fields/#{custom_field_id}") do |f| + response = connection.put("custom_fields/#{custom_field_id}") do |f| f.body = JSON.generate({ label: label }) diff --git a/lib/convertkit_v4/client/tags.rb b/lib/convertkit_v4/client/tags.rb index 8fe1f12..be45066 100644 --- a/lib/convertkit_v4/client/tags.rb +++ b/lib/convertkit_v4/client/tags.rb @@ -16,7 +16,7 @@ def create_tag(tag_name) end def add_subscriber_to_tag(tag_id, options = {}) - connection.post("tags/#{tag_id}/subscribers") do |f| + response = connection.post("tags/#{tag_id}/subscribers") do |f| f.body = JSON.generate({ email: options[:email], id: options[:id] From ea780bfa144d2fc43d3434875f399591a6379172 Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Tue, 26 Sep 2023 15:24:44 +0100 Subject: [PATCH 15/26] version --- lib/convertkit_v4/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/convertkit_v4/version.rb b/lib/convertkit_v4/version.rb index 4fb9c00..2c78fcd 100644 --- a/lib/convertkit_v4/version.rb +++ b/lib/convertkit_v4/version.rb @@ -1,3 +1,3 @@ module ConvertkitV4 - VERSION = "1.0.11" + VERSION = "1.0.13" end From 2613b2a19626d2e87e413590efa19a6400fe6547 Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Tue, 26 Sep 2023 22:15:05 +0100 Subject: [PATCH 16/26] ExpiredTokenError --- lib/convertkit_v4/errors.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/convertkit_v4/errors.rb b/lib/convertkit_v4/errors.rb index 0b05077..b73e83c 100644 --- a/lib/convertkit_v4/errors.rb +++ b/lib/convertkit_v4/errors.rb @@ -2,6 +2,7 @@ module ConvertkitV4 class Error < StandardError; end class AuthorizationError < Error; end + class ExpiredTokenError < Error; end class ConnectionError < Error; end class NotFoundError < Error; end class ServerError < Error; end From 2705ad1ad2b6a0516da86cef54911dd225d875de Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Tue, 26 Sep 2023 22:15:10 +0100 Subject: [PATCH 17/26] raise error --- lib/convertkit_v4/connection.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/convertkit_v4/connection.rb b/lib/convertkit_v4/connection.rb index 502293a..5fd26d8 100644 --- a/lib/convertkit_v4/connection.rb +++ b/lib/convertkit_v4/connection.rb @@ -70,7 +70,15 @@ def request(method, *args, &blk) def handle_error_response(response) case response.status when 401 - raise AuthorizationError.new(response.body) + authenticate_header = response.headers['WWW-Authenticate'] + if authenticate_header && authenticate_header.include?('invalid_token') + error_description = authenticate_header.match(/error_description="(.+)"/)[1] + if error_description == 'The access token expired' + raise ExpiredTokenError.new(response.body) + else + raise AuthorizationError.new(response.body) + end + end when 404 raise NotFoundError.new(response.body) when 422 From 254474d5ffd326cb14862a45c6199a0dcbfe8383 Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Tue, 26 Sep 2023 22:15:56 +0100 Subject: [PATCH 18/26] version --- lib/convertkit_v4/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/convertkit_v4/version.rb b/lib/convertkit_v4/version.rb index 2c78fcd..a5378e3 100644 --- a/lib/convertkit_v4/version.rb +++ b/lib/convertkit_v4/version.rb @@ -1,3 +1,3 @@ module ConvertkitV4 - VERSION = "1.0.13" + VERSION = "1.0.14" end From a68d744121ab93125720f9824d6ddbb8e94611c5 Mon Sep 17 00:00:00 2001 From: bilal Date: Tue, 6 Feb 2024 13:35:16 +0400 Subject: [PATCH 19/26] Fixes error handling --- lib/convertkit_v4/connection.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/convertkit_v4/connection.rb b/lib/convertkit_v4/connection.rb index 5fd26d8..cd39a23 100644 --- a/lib/convertkit_v4/connection.rb +++ b/lib/convertkit_v4/connection.rb @@ -74,11 +74,10 @@ def handle_error_response(response) if authenticate_header && authenticate_header.include?('invalid_token') error_description = authenticate_header.match(/error_description="(.+)"/)[1] if error_description == 'The access token expired' - raise ExpiredTokenError.new(response.body) - else - raise AuthorizationError.new(response.body) + raise ExpiredTokenError.new(response.body) end end + raise AuthorizationError.new(response.body) when 404 raise NotFoundError.new(response.body) when 422 From 98f42391ab62e4c76f9a92d1f724855f4adb6660 Mon Sep 17 00:00:00 2001 From: bilal Date: Tue, 6 Feb 2024 15:10:33 +0400 Subject: [PATCH 20/26] Updates version --- lib/convertkit_v4/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/convertkit_v4/version.rb b/lib/convertkit_v4/version.rb index a5378e3..df04e30 100644 --- a/lib/convertkit_v4/version.rb +++ b/lib/convertkit_v4/version.rb @@ -1,3 +1,3 @@ module ConvertkitV4 - VERSION = "1.0.14" + VERSION = "1.0.15" end From 716437f1a59cb99e0a7048f4c04fcf43cdc55ce3 Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Tue, 6 Feb 2024 19:34:17 +0000 Subject: [PATCH 21/26] endpoints --- lib/convertkit_v4/client/broadcasts.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 lib/convertkit_v4/client/broadcasts.rb diff --git a/lib/convertkit_v4/client/broadcasts.rb b/lib/convertkit_v4/client/broadcasts.rb new file mode 100644 index 0000000..b8bd979 --- /dev/null +++ b/lib/convertkit_v4/client/broadcasts.rb @@ -0,0 +1,19 @@ +module ConvertkitV4 + class Client + module Broadcasts + + def broadcasts + connection.get("broadcasts").body["broadcasts"] + end + + def broadcast(broadcast_id) + connection.get("broadcasts/#{broadcast_id}").body["broadcast"] + end + + def broadcast_stats(broadcast_id) + connection.get("broadcasts/#{broadcast_id}/stats").body["broadcast"] + end + + end + end +end From a19e361adf362e06e6143794efe12a42d26f8acf Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Tue, 6 Feb 2024 19:34:20 +0000 Subject: [PATCH 22/26] specs --- spec/convertkit_v4/client/broadcasts_spec.rb | 43 ++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 spec/convertkit_v4/client/broadcasts_spec.rb diff --git a/spec/convertkit_v4/client/broadcasts_spec.rb b/spec/convertkit_v4/client/broadcasts_spec.rb new file mode 100644 index 0000000..94fed14 --- /dev/null +++ b/spec/convertkit_v4/client/broadcasts_spec.rb @@ -0,0 +1,43 @@ +require "spec_helper" +require "securerandom" + +module ConvertkitV4 + class Client + describe Broadcasts do + before do + ConvertkitV4.configure do |config| + config.api_secret = ENV["API_SECRET"] + config.api_key = ENV["API_KEY"] + end + + @client = ConvertkitV4::Client.new + end + + describe "#broadcasts" do + it "sends the right request" do + r = @client.broadcasts + expect(r.success?).to be_truthy + expect(r.body).to_not eql({"error"=>"Authorization Failed", "message"=>"API Key not present"}) + end + end + + describe "#broadcast" do + it "sends the right request" do + broadcast_id = ENV['BROADCAST_ID'] + + r = @client.broadcast(broadcast_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 + + describe "#broadcast_stats" do + it "sends the right request" do + broadcast_id = ENV['BROADCAST_ID'] + + r = @client.broadcast_stats(broadcast_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 From c044a65e9f23a3ff839016bcb9199f2cf82aa60c Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Tue, 6 Feb 2024 19:34:27 +0000 Subject: [PATCH 23/26] updated version --- lib/convertkit_v4/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/convertkit_v4/version.rb b/lib/convertkit_v4/version.rb index df04e30..05bdeba 100644 --- a/lib/convertkit_v4/version.rb +++ b/lib/convertkit_v4/version.rb @@ -1,3 +1,3 @@ module ConvertkitV4 - VERSION = "1.0.15" + VERSION = "1.0.16" end From c07cedec20309a6cc1e9c4e58016ac1741b18336 Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Tue, 6 Feb 2024 19:39:40 +0000 Subject: [PATCH 24/26] Revert "Broadcasts" --- Gemfile | 2 +- README.md | 26 +++--- bin/console | 9 +- ...v4-ruby.gemspec => convertkit-ruby.gemspec | 14 ++-- fixtures/vcr_cassettes/account.yml | 4 +- .../vcr_cassettes/add_subscriber_to_form.yml | 4 +- .../vcr_cassettes/delete_custom_field.yml | 4 +- fixtures/vcr_cassettes/forms.yml | 4 +- fixtures/vcr_cassettes/get_custom_fields.yml | 4 +- fixtures/vcr_cassettes/new_custom_field.yml | 4 +- .../vcr_cassettes/update_custom_field.yml | 4 +- lib/{convertkit_v4.rb => convertkit.rb} | 8 +- lib/convertkit/client.rb | 31 +++++++ .../client/account.rb | 2 +- lib/convertkit/client/custom_fields.rb | 27 ++++++ .../client/forms.rb | 2 +- .../client/sequences.rb | 4 +- lib/convertkit/client/subscribers.rb | 36 ++++++++ lib/convertkit/client/tags.rb | 50 +++++++++++ lib/convertkit/client/webhooks.rb | 21 +++++ .../configuration.rb | 4 +- .../connection.rb | 32 +++----- lib/{convertkit_v4 => convertkit}/errors.rb | 3 +- lib/convertkit/version.rb | 3 + lib/convertkit_v4/client.rb | 82 ------------------- lib/convertkit_v4/client/broadcasts.rb | 19 ----- lib/convertkit_v4/client/custom_fields.rb | 33 -------- lib/convertkit_v4/client/subscribers.rb | 52 ------------ lib/convertkit_v4/client/tags.rb | 30 ------- lib/convertkit_v4/client/webhooks.rb | 21 ----- lib/convertkit_v4/version.rb | 3 - .../client/account_spec.rb | 4 +- .../client/custom_fields_spec.rb | 4 +- .../client/forms_spec.rb | 4 +- .../client/sequences_spec.rb | 6 +- .../client/subscribers_spec.rb | 6 +- .../client/tags_spec.rb | 6 +- .../client/webhook_spec.rb | 6 +- spec/convertkit/client_spec.rb | 20 +++++ .../connection_spec.rb | 19 +++-- spec/convertkit/convertkit_spec.rb | 49 +++++++++++ spec/convertkit_v4/client/broadcasts_spec.rb | 43 ---------- spec/convertkit_v4/client_spec.rb | 24 ------ spec/convertkit_v4/convertkit_v4_spec.rb | 41 ---------- spec/spec_helper.rb | 12 ++- 45 files changed, 338 insertions(+), 448 deletions(-) rename convertkit_v4-ruby.gemspec => convertkit-ruby.gemspec (77%) rename lib/{convertkit_v4.rb => convertkit.rb} (60%) create mode 100644 lib/convertkit/client.rb rename lib/{convertkit_v4 => convertkit}/client/account.rb (85%) create mode 100644 lib/convertkit/client/custom_fields.rb rename lib/{convertkit_v4 => convertkit}/client/forms.rb (95%) rename lib/{convertkit_v4 => convertkit}/client/sequences.rb (85%) create mode 100644 lib/convertkit/client/subscribers.rb create mode 100644 lib/convertkit/client/tags.rb create mode 100644 lib/convertkit/client/webhooks.rb rename lib/{convertkit_v4 => convertkit}/configuration.rb (56%) rename lib/{convertkit_v4 => convertkit}/connection.rb (61%) rename lib/{convertkit_v4 => convertkit}/errors.rb (83%) create mode 100644 lib/convertkit/version.rb delete mode 100644 lib/convertkit_v4/client.rb delete mode 100644 lib/convertkit_v4/client/broadcasts.rb delete mode 100644 lib/convertkit_v4/client/custom_fields.rb delete mode 100644 lib/convertkit_v4/client/subscribers.rb delete mode 100644 lib/convertkit_v4/client/tags.rb delete mode 100644 lib/convertkit_v4/client/webhooks.rb delete mode 100644 lib/convertkit_v4/version.rb rename spec/{convertkit_v4 => convertkit}/client/account_spec.rb (85%) rename spec/{convertkit_v4 => convertkit}/client/custom_fields_spec.rb (95%) rename spec/{convertkit_v4 => convertkit}/client/forms_spec.rb (91%) rename spec/{convertkit_v4 => convertkit}/client/sequences_spec.rb (90%) rename spec/{convertkit_v4 => convertkit}/client/subscribers_spec.rb (96%) rename spec/{convertkit_v4 => convertkit}/client/tags_spec.rb (96%) rename spec/{convertkit_v4 => convertkit}/client/webhook_spec.rb (91%) create mode 100644 spec/convertkit/client_spec.rb rename spec/{convertkit_v4 => convertkit}/connection_spec.rb (75%) create mode 100644 spec/convertkit/convertkit_spec.rb delete mode 100644 spec/convertkit_v4/client/broadcasts_spec.rb delete mode 100644 spec/convertkit_v4/client_spec.rb delete mode 100644 spec/convertkit_v4/convertkit_v4_spec.rb diff --git a/Gemfile b/Gemfile index a99f74c..eacacf1 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ source 'https://rubygems.org' -# Specify your gem's dependencies in convertkit_v4-ruby.gemspec +# Specify your gem's dependencies in convertkit-ruby.gemspec gemspec gem 'dotenv', '~> 2.1', '>= 2.1.1' diff --git a/README.md b/README.md index 13e9133..9258df4 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ -# Convertkit V4 Ruby Client +# Convertkit Ruby Client -A Ruby toolkit for [ConvertkitV4](https://convertkit.com/) API. +A Ruby toolkit for [Convertkit](https://convertkit.com/) API. ## Installation Add this line to your application's Gemfile: ```ruby -gem 'convertkit_v4-ruby', require: 'convertkit_v4' +gem 'convertkit-ruby', require: 'convertkit' ``` And then execute: @@ -16,29 +16,29 @@ And then execute: Or install it yourself as: - $ gem install convertkit_v4-ruby + $ gem install convertkit-ruby ## Authentication -For private integrations, use your personal ``API_KEY`` and ``API_SECRET`` found in [your account settings.](https://app.ConvertkitV4.com/account/edit) +For private integrations, use your personal ``API_KEY`` and ``API_SECRET`` found in [your account settings.](https://app.convertkit.com/account/edit) ```ruby require "dotenv" Dotenv.load(".env.local") -ConvertkitV4.configure do |config| +Convertkit.configure do |config| config.api_secret = ENV["API_SECRET"] config.api_key = ENV["API_KEY"] end -client = ConvertkitV4::Client.new +client = Convertkit::Client.new ``` ## Usage -Calls for ConvertkitV4 API v3 are relative to the url [developers.convertkit.com](developers.convertkit.com). +Calls for Convertkit API v3 are relative to the url [http://api.convertkit.com/v3](http://api.convertkit.com/v3). -API actions are available as methods on the client object. Currently, the ConvertkitV4 client has the following methods: +API actions are available as methods on the client object. Currently, the Convertkit client has the following methods: | Action | Method | @@ -61,9 +61,9 @@ API actions are available as methods on the client object. Currently, the Conver ## Use Cases -Here are some common use cases for the ConvertkitV4 v4 API client. +Here are some common use cases for the Convertkit v3 API client. -First configure the ``convertkit_v4-ruby`` gem with your ``API_KEY`` and ``API_SECRET``, and initialize a new client. After that, you can fetch data from your account. +First configure the ``convertkit-ruby`` gem with your ``API_KEY`` and ``API_SECRET``, and initialize a new client. After that, you can fetch data from your account. ### List subscribers @@ -99,9 +99,9 @@ To install this gem onto your local machine, run `bundle exec rake install`. To ## Contributing -Bug reports and pull requests are welcome on GitHub at https://github.com/SparkLoop/convertkit-ruby-v4. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. +Bug reports and pull requests are welcome on GitHub at https://github.com/Atomoworks/convertkit-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. -1. Fork it ( https://github.com/SparkLoop/convertkit-ruby-v4/fork ) +1. Fork it ( https://github.com/Atomoworks/convertkit-ruby/fork ) 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) diff --git a/bin/console b/bin/console index 94a2fc5..a2c3428 100755 --- a/bin/console +++ b/bin/console @@ -1,15 +1,14 @@ #!/usr/bin/env ruby require "bundler/setup" -require "convertkit_v4" +require "convertkit" require "dotenv" Dotenv.load(".env.local") -ConvertkitV4.configure do |config| - config.client_id = ENV["CONVERTKIT_CLIENT_ID"] - config.client_secret = ENV["CONVERTKIT_CLIENT_SECRET"] - config.redirect_uri = ENV["CONVERTKIT_REDIRECT_URI"] +Convertkit.configure do |config| + config.api_secret = ENV["API_SECRET"] + config.api_key = ENV["API_KEY"] end # You can add fixtures and/or initialization code here to make experimenting diff --git a/convertkit_v4-ruby.gemspec b/convertkit-ruby.gemspec similarity index 77% rename from convertkit_v4-ruby.gemspec rename to convertkit-ruby.gemspec index 452e5b5..820c518 100644 --- a/convertkit_v4-ruby.gemspec +++ b/convertkit-ruby.gemspec @@ -1,16 +1,16 @@ # coding: utf-8 lib = File.expand_path('../lib', __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'convertkit_v4/version' +require 'convertkit/version' Gem::Specification.new do |spec| - spec.name = "convertkit_v4-ruby" - spec.version = ConvertkitV4::VERSION - spec.authors = ["Manuel Frigerio", "Petar Risteski"] - spec.email = ["manuel.frigerio@gmail.com", "risteskipetar3@gmail.com"] + spec.name = "convertkit-ruby" + spec.version = Convertkit::VERSION + spec.authors = ["Manuel Frigerio"] + spec.email = ["manuel.frigerio@gmail.com"] - spec.summary = %q{A Ruby gem for interacting with the Convertkit API v4} - spec.description = %q{A simple wrapper for the Convertkit V4 API} + spec.summary = %q{A Ruby gem for interacting with the ConvertKit API v3} + spec.description = %q{A simple wrapper for the ConvertKit API} spec.homepage = "https://manuel.friger.io" spec.license = "MIT" diff --git a/fixtures/vcr_cassettes/account.yml b/fixtures/vcr_cassettes/account.yml index 0e29e0e..b0b9f3b 100644 --- a/fixtures/vcr_cassettes/account.yml +++ b/fixtures/vcr_cassettes/account.yml @@ -2,7 +2,7 @@ http_interactions: - request: method: get - uri: https://api.convertkit.com/v3/account?access_token= + uri: https://api.convertkit.com/v3/account?api_key=&api_secret= body: encoding: US-ASCII string: '' @@ -10,7 +10,7 @@ http_interactions: User-Agent: - Convertkit-Ruby v0.0.2 Content-Type: - - application/json + - application/vnd.api+json Accept: - "*/*" Accept-Encoding: diff --git a/fixtures/vcr_cassettes/add_subscriber_to_form.yml b/fixtures/vcr_cassettes/add_subscriber_to_form.yml index 7cf87f3..0455c45 100644 --- a/fixtures/vcr_cassettes/add_subscriber_to_form.yml +++ b/fixtures/vcr_cassettes/add_subscriber_to_form.yml @@ -2,7 +2,7 @@ http_interactions: - request: method: post - uri: https://api.convertkit.com/v3/forms/175221/subscribe?access_token=&email=test@example.com&fields&first_name&tags + uri: https://api.convertkit.com/v3/forms/175221/subscribe?api_key=&api_secret=&email=test@example.com&fields&first_name&tags body: encoding: UTF-8 string: '' @@ -10,7 +10,7 @@ http_interactions: User-Agent: - Convertkit-Ruby v0.0.2 Content-Type: - - application/json + - application/vnd.api+json Accept: - "*/*" Content-Length: diff --git a/fixtures/vcr_cassettes/delete_custom_field.yml b/fixtures/vcr_cassettes/delete_custom_field.yml index 6555f65..c65def7 100644 --- a/fixtures/vcr_cassettes/delete_custom_field.yml +++ b/fixtures/vcr_cassettes/delete_custom_field.yml @@ -2,7 +2,7 @@ http_interactions: - request: method: delete - uri: https://api.convertkit.com/v3/custom_fields/11176?access_token= + uri: https://api.convertkit.com/v3/custom_fields/11176?api_key=&api_secret= body: encoding: US-ASCII string: '' @@ -10,7 +10,7 @@ http_interactions: User-Agent: - Convertkit-Ruby v0.0.2 Content-Type: - - application/json + - application/vnd.api+json Accept: - "*/*" Accept-Encoding: diff --git a/fixtures/vcr_cassettes/forms.yml b/fixtures/vcr_cassettes/forms.yml index 276a9eb..2783691 100644 --- a/fixtures/vcr_cassettes/forms.yml +++ b/fixtures/vcr_cassettes/forms.yml @@ -2,7 +2,7 @@ http_interactions: - request: method: get - uri: https://api.convertkit.com/v3/forms?access_token= + uri: https://api.convertkit.com/v3/forms?api_key=&api_secret= body: encoding: US-ASCII string: '' @@ -10,7 +10,7 @@ http_interactions: User-Agent: - Convertkit-Ruby v0.0.2 Content-Type: - - application/json + - application/vnd.api+json Accept: - "*/*" Accept-Encoding: diff --git a/fixtures/vcr_cassettes/get_custom_fields.yml b/fixtures/vcr_cassettes/get_custom_fields.yml index e4a809c..f5b911d 100644 --- a/fixtures/vcr_cassettes/get_custom_fields.yml +++ b/fixtures/vcr_cassettes/get_custom_fields.yml @@ -2,7 +2,7 @@ http_interactions: - request: method: get - uri: https://api.convertkit.com/v3/custom_fields?access_token= + uri: https://api.convertkit.com/v3/custom_fields?api_key=&api_secret= body: encoding: US-ASCII string: '' @@ -10,7 +10,7 @@ http_interactions: User-Agent: - Convertkit-Ruby v0.0.2 Content-Type: - - application/json + - application/vnd.api+json Accept: - "*/*" Accept-Encoding: diff --git a/fixtures/vcr_cassettes/new_custom_field.yml b/fixtures/vcr_cassettes/new_custom_field.yml index 03a76cd..8d9d555 100644 --- a/fixtures/vcr_cassettes/new_custom_field.yml +++ b/fixtures/vcr_cassettes/new_custom_field.yml @@ -2,7 +2,7 @@ http_interactions: - request: method: post - uri: https://api.convertkit.com/v3/custom_fields?access_token=&label=Occupation + uri: https://api.convertkit.com/v3/custom_fields?api_key=&api_secret=&label=Occupation body: encoding: UTF-8 string: '' @@ -10,7 +10,7 @@ http_interactions: User-Agent: - Convertkit-Ruby v0.0.2 Content-Type: - - application/json + - application/vnd.api+json Accept: - "*/*" Content-Length: diff --git a/fixtures/vcr_cassettes/update_custom_field.yml b/fixtures/vcr_cassettes/update_custom_field.yml index e0489a2..d49e84b 100644 --- a/fixtures/vcr_cassettes/update_custom_field.yml +++ b/fixtures/vcr_cassettes/update_custom_field.yml @@ -2,7 +2,7 @@ http_interactions: - request: method: put - uri: https://api.convertkit.com/v3/custom_fields/11184?access_token=&label=Discount%20Code + uri: https://api.convertkit.com/v3/custom_fields/11184?api_key=&api_secret=&label=Discount%20Code body: encoding: UTF-8 string: '' @@ -10,7 +10,7 @@ http_interactions: User-Agent: - Convertkit-Ruby v0.0.2 Content-Type: - - application/json + - application/vnd.api+json Accept: - "*/*" Content-Length: diff --git a/lib/convertkit_v4.rb b/lib/convertkit.rb similarity index 60% rename from lib/convertkit_v4.rb rename to lib/convertkit.rb index c21c0ae..e553f19 100644 --- a/lib/convertkit_v4.rb +++ b/lib/convertkit.rb @@ -1,8 +1,8 @@ -require "convertkit_v4/version" -require "convertkit_v4/configuration" -require "convertkit_v4/client" +require "convertkit/version" +require "convertkit/configuration" +require "convertkit/client" -module ConvertkitV4 +module Convertkit class << self attr_accessor :configuration end diff --git a/lib/convertkit/client.rb b/lib/convertkit/client.rb new file mode 100644 index 0000000..8132d2b --- /dev/null +++ b/lib/convertkit/client.rb @@ -0,0 +1,31 @@ +require "convertkit/client/account" +require "convertkit/client/custom_fields" +require "convertkit/client/forms" +require "convertkit/client/sequences" +require "convertkit/client/subscribers" +require "convertkit/client/webhooks" +require "convertkit/client/tags" +require "convertkit/connection" + +module Convertkit + class Client + include Account + include CustomFields + include Forms + include Sequences + include Subscribers + include Webhooks + include Tags + + attr_accessor :api_secret, :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 connection + @connection ||= Connection.new(api_key: api_key, api_secret: api_secret) + end + end +end diff --git a/lib/convertkit_v4/client/account.rb b/lib/convertkit/client/account.rb similarity index 85% rename from lib/convertkit_v4/client/account.rb rename to lib/convertkit/client/account.rb index 4c9401f..ee25d3f 100644 --- a/lib/convertkit_v4/client/account.rb +++ b/lib/convertkit/client/account.rb @@ -1,4 +1,4 @@ -module ConvertkitV4 +module Convertkit class Client module Account def account diff --git a/lib/convertkit/client/custom_fields.rb b/lib/convertkit/client/custom_fields.rb new file mode 100644 index 0000000..6c2fff7 --- /dev/null +++ b/lib/convertkit/client/custom_fields.rb @@ -0,0 +1,27 @@ +module Convertkit + class Client + module CustomFields + + def custom_fields + connection.get("custom_fields").body["custom_fields"] + end + + def add_custom_field(options = {}) + connection.post("custom_fields") do |f| + f.params['label'] = options[:label] + end + end + + def delete_custom_field(custom_field_id) + connection.delete("custom_fields/#{custom_field_id}") + end + + def update_custom_field(custom_field_id, options = {}) + connection.put("custom_fields/#{custom_field_id}") do |f| + f.params['label'] = options[:label] + end + end + + end + end +end diff --git a/lib/convertkit_v4/client/forms.rb b/lib/convertkit/client/forms.rb similarity index 95% rename from lib/convertkit_v4/client/forms.rb rename to lib/convertkit/client/forms.rb index 7958868..fe84c62 100644 --- a/lib/convertkit_v4/client/forms.rb +++ b/lib/convertkit/client/forms.rb @@ -1,4 +1,4 @@ -module ConvertkitV4 +module Convertkit class Client module Forms def forms diff --git a/lib/convertkit_v4/client/sequences.rb b/lib/convertkit/client/sequences.rb similarity index 85% rename from lib/convertkit_v4/client/sequences.rb rename to lib/convertkit/client/sequences.rb index 6314b34..2b1bedd 100644 --- a/lib/convertkit_v4/client/sequences.rb +++ b/lib/convertkit/client/sequences.rb @@ -1,8 +1,8 @@ -module ConvertkitV4 +module Convertkit class Client module Sequences def sequences - connection.get("sequences").body["sequences"] + connection.get("sequences") end def add_subscriber_to_sequence(sequence_id, email, options = {}) diff --git a/lib/convertkit/client/subscribers.rb b/lib/convertkit/client/subscribers.rb new file mode 100644 index 0000000..92ecd86 --- /dev/null +++ b/lib/convertkit/client/subscribers.rb @@ -0,0 +1,36 @@ +module Convertkit + class Client + module Subscribers + def subscribers(options = {}) + connection.get("subscribers", options) + end + + def subscriber(subscriber_id) + connection.get("subscribers/#{subscriber_id}") + end + + def subscriber_tags(subscriber_id) + connection.get("subscribers/#{subscriber_id}/tags") + end + + def update_subscriber(subscriber_id, options = {}) + response = connection.put("subscribers/#{subscriber_id}") do |f| + f.params["email_address"] = options[:email_address] if options[:email_address] + f.params["fields"] = options[:fields] if options[:fields] + f.params["first_name"] = options[:first_name] if options[:first_name] + end + response.body + end + + def unsubscribe(email) + connection.put("unsubscribe") do |f| + f.params['email'] = email + end + end + + def remove_tag_from_subscriber(subscriber_id, tag_id) + connection.delete("subscribers/#{subscriber_id}/tags/#{tag_id}") + end + end + end +end diff --git a/lib/convertkit/client/tags.rb b/lib/convertkit/client/tags.rb new file mode 100644 index 0000000..282e13c --- /dev/null +++ b/lib/convertkit/client/tags.rb @@ -0,0 +1,50 @@ +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 + + def remove_tag_from_subscriber(tag_id, subscriber_id) + connection.delete("subscribers/#{subscriber_id}/tags/#{tag_id}") + end + + def remove_tag_from_subscriber_by_email(tag_id, email) + connection.post("tags/#{tag_id}/unsubscribe") do |f| + f.params['email'] = email + end + end + + def create_tag(tag_name) + response = connection.post("tags") do |request| + request.params["tag"] = { name: tag_name } + end + response.body + end + + def create_tags(tag_names) + response = connection.post("tags") do |request| + request.params["tag"] = tag_names.map { |tag_name| { name: tag_name } } + end + response.body + end + + def subscriptions_to_tag(tag_id, options = {}) + connection.get("tags/#{tag_id}/subscriptions", options) do |f| + f.params["page"] = options[:page] if options[:page] + f.params["sort_order"] = options[:sort_order] if options[:sort_order] + f.params["subscriber_state"] = options[:subscriber_state] if options[:subscriber_state] + end + end + end + end +end diff --git a/lib/convertkit/client/webhooks.rb b/lib/convertkit/client/webhooks.rb new file mode 100644 index 0000000..e2d6b5a --- /dev/null +++ b/lib/convertkit/client/webhooks.rb @@ -0,0 +1,21 @@ +module Convertkit + class Client + module Webhooks + def webhooks + connection.get("automations/hooks/").body + end + + def create_webhook(url, events) + response = connection.post("automations/hooks") do |f| + f.params['target_url'] = url + f.params['event'] = events + end + response.body + end + + def remove_webhook(rule_id) + connection.delete("automations/hooks/#{rule_id}").body + end + end + end +end diff --git a/lib/convertkit_v4/configuration.rb b/lib/convertkit/configuration.rb similarity index 56% rename from lib/convertkit_v4/configuration.rb rename to lib/convertkit/configuration.rb index 74cdfee..07fe421 100644 --- a/lib/convertkit_v4/configuration.rb +++ b/lib/convertkit/configuration.rb @@ -1,6 +1,6 @@ -module ConvertkitV4 +module Convertkit class Configuration - attr_accessor :client_id, :client_secret, :redirect_uri, :access_token, :refresh_token + attr_accessor :api_secret, :api_key attr_accessor :timeout, :open_timeout def initialize diff --git a/lib/convertkit_v4/connection.rb b/lib/convertkit/connection.rb similarity index 61% rename from lib/convertkit_v4/connection.rb rename to lib/convertkit/connection.rb index cd39a23..a3fa832 100644 --- a/lib/convertkit_v4/connection.rb +++ b/lib/convertkit/connection.rb @@ -1,21 +1,18 @@ -require "convertkit_v4/errors" +require "convertkit/errors" require "faraday" require "faraday_middleware" require "json" -module ConvertkitV4 +module Convertkit class Connection attr_reader :http_connection - API_URL = "https://api.convertkit.com/" - API_VERSION_PATH = "alpha/" - - def initialize(access_token: nil) - @http_connection = faraday_connection(access_token) + def initialize(api_key: nil, api_secret: nil) + @http_connection = faraday_connection(api_key, api_secret) end def content_type - "application/json" + "application/vnd.api+json" end def get(*args, &blk) @@ -36,18 +33,20 @@ def delete(*args, &blk) private - def faraday_connection(access_token) + def faraday_connection(api_key, api_secret) Faraday.new do |f| - f.url_prefix = "#{API_URL}#{API_VERSION_PATH}" + f.url_prefix = "https://api.convertkit.com/v3/" f.adapter :net_http - f.options.timeout = ConvertkitV4.configuration.timeout - f.options.open_timeout = ConvertkitV4.configuration.open_timeout + f.options.timeout = Convertkit.configuration.timeout + f.options.open_timeout = Convertkit.configuration.open_timeout + f.headers['User-Agent'] = "Convertkit-Ruby v#{Convertkit::VERSION}" f.headers['Content-Type'] = content_type f.headers['Accept'] = "*/*" - f.params['access_token'] = access_token if access_token + 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 @@ -70,13 +69,6 @@ def request(method, *args, &blk) def handle_error_response(response) case response.status when 401 - authenticate_header = response.headers['WWW-Authenticate'] - if authenticate_header && authenticate_header.include?('invalid_token') - error_description = authenticate_header.match(/error_description="(.+)"/)[1] - if error_description == 'The access token expired' - raise ExpiredTokenError.new(response.body) - end - end raise AuthorizationError.new(response.body) when 404 raise NotFoundError.new(response.body) diff --git a/lib/convertkit_v4/errors.rb b/lib/convertkit/errors.rb similarity index 83% rename from lib/convertkit_v4/errors.rb rename to lib/convertkit/errors.rb index b73e83c..d168e78 100644 --- a/lib/convertkit_v4/errors.rb +++ b/lib/convertkit/errors.rb @@ -1,8 +1,7 @@ -module ConvertkitV4 +module Convertkit class Error < StandardError; end class AuthorizationError < Error; end - class ExpiredTokenError < Error; end class ConnectionError < Error; end class NotFoundError < Error; end class ServerError < Error; end diff --git a/lib/convertkit/version.rb b/lib/convertkit/version.rb new file mode 100644 index 0000000..48d0019 --- /dev/null +++ b/lib/convertkit/version.rb @@ -0,0 +1,3 @@ +module Convertkit + VERSION = "1.0.0" +end \ No newline at end of file diff --git a/lib/convertkit_v4/client.rb b/lib/convertkit_v4/client.rb deleted file mode 100644 index 4a69183..0000000 --- a/lib/convertkit_v4/client.rb +++ /dev/null @@ -1,82 +0,0 @@ -require "convertkit_v4/client/account" -require "convertkit_v4/client/custom_fields" -require "convertkit_v4/client/forms" -require "convertkit_v4/client/sequences" -require "convertkit_v4/client/subscribers" -require "convertkit_v4/client/webhooks" -require "convertkit_v4/client/tags" -require "convertkit_v4/connection" - -module ConvertkitV4 - class Client - include Account - include CustomFields - include Forms - include Sequences - include Subscribers - include Webhooks - include Tags - - attr_accessor :access_token, :refresh_token, :client_id, :client_secret, :redirect_uri - - AUTH_URL = "https://app.convertkit.com/oauth/authorize" - ACCESS_TOKEN_URL = "https://app.convertkit.com/oauth/token" - REVOKE_ACCESS_URL = "https://app.convertkit.com/oauth/revoke" - - def initialize(access_token: nil, refresh_token: nil, client_id: nil, client_secret: nil, redirect_uri: nil) - @access_token = access_token - @refresh_token = refresh_token - - @client_id = client_id || ConvertkitV4.configuration.client_id - @client_secret = client_secret || ConvertkitV4.configuration.client_secret - @redirect_uri = redirect_uri || ConvertkitV4.configuration.redirect_uri - end - - def connection - @connection ||= Connection.new(access_token: @access_token) - end - - def authorize - Faraday.new(url: AUTH_URL).get do |req| - req.headers["Content-Type"] = "application/json" - req.params = { client_id: @client_id, redirect_uri: @redirect_uri } - end - end - - def get_access_token(code) - Faraday.new(url: ACCESS_TOKEN_URL).post do |req| - req.headers["Content-Type"] = "application/json" - req.params = { - client_id: @client_id, - client_secret: @client_secret, - code: code, - grant_type: "authorization_code", - redirect_uri: @redirect_uri - } - end - end - - def refresh_token - Faraday.new(url: ACCESS_TOKEN_URL).post do |req| - req.headers["Content-Type"] = "application/json" - req.params = { - client_id: @client_id, - client_secret: @client_secret, - refresh_token: @refresh_token, - grant_type: "refresh_token" - } - end - end - - def revoke_access - Faraday.new(url: REVOKE_ACCESS_URL).post do |req| - req.headers["Content-Type"] = "application/json" - req.params = { - client_id: @client_id, - client_secret: @client_secret, - token: @access_token - } - end - end - end -end diff --git a/lib/convertkit_v4/client/broadcasts.rb b/lib/convertkit_v4/client/broadcasts.rb deleted file mode 100644 index b8bd979..0000000 --- a/lib/convertkit_v4/client/broadcasts.rb +++ /dev/null @@ -1,19 +0,0 @@ -module ConvertkitV4 - class Client - module Broadcasts - - def broadcasts - connection.get("broadcasts").body["broadcasts"] - end - - def broadcast(broadcast_id) - connection.get("broadcasts/#{broadcast_id}").body["broadcast"] - end - - def broadcast_stats(broadcast_id) - connection.get("broadcasts/#{broadcast_id}/stats").body["broadcast"] - end - - end - end -end diff --git a/lib/convertkit_v4/client/custom_fields.rb b/lib/convertkit_v4/client/custom_fields.rb deleted file mode 100644 index 8dc9ffc..0000000 --- a/lib/convertkit_v4/client/custom_fields.rb +++ /dev/null @@ -1,33 +0,0 @@ -module ConvertkitV4 - class Client - module CustomFields - - def custom_fields - connection.get("custom_fields").body["custom_fields"] - end - - def add_custom_field(label) - response = connection.post("custom_fields") do |f| - f.body = JSON.generate({ - label: label - }) - end - response.body["custom_field"] - end - - def delete_custom_field(custom_field_id) - connection.delete("custom_fields/#{custom_field_id}") - end - - def update_custom_field(custom_field_id, label) - response = connection.put("custom_fields/#{custom_field_id}") do |f| - f.body = JSON.generate({ - label: label - }) - end - response.body["custom_field"] - end - - end - end -end diff --git a/lib/convertkit_v4/client/subscribers.rb b/lib/convertkit_v4/client/subscribers.rb deleted file mode 100644 index 41915ad..0000000 --- a/lib/convertkit_v4/client/subscribers.rb +++ /dev/null @@ -1,52 +0,0 @@ -module ConvertkitV4 - class Client - module Subscribers - - def subscribers(options = {}) - connection.get("subscribers", options).body - end - - def subscriber(subscriber_id) - connection.get("subscribers/#{subscriber_id}").body["subscriber"] - end - - def add_subscriber(email_address, options = {}) - response = connection.post("subscribers") do |f| - body = { - email_address: email_address - } - - body[:fields] = options[:fields] if options[:fields] - body[:first_name] = options[:first_name] if options[:first_name] - - f.body = JSON.generate(body) - end - response.body["subscriber"] - end - - def update_subscriber(subscriber_id, options = {}) - response = connection.put("subscribers/#{subscriber_id}") do |f| - body = {} - body[:email_address] = options[:email_address] if options[:email_address] - body[:fields] = options[:fields] if options[:fields] - body[:first_name] = options[:first_name] if options[:first_name] - - f.body = JSON.generate(body) - end - response.body["subscriber"] - end - - def unsubscribe(subscriber_id) - connection.post("unsubscribe") do |f| - f.body = JSON.generate({ - id: subscriber_id - }) - end - end - - def subscriber_tags(subscriber_id) - connection.get("subscribers/#{subscriber_id}/tags").body["tags"] - end - end - end -end diff --git a/lib/convertkit_v4/client/tags.rb b/lib/convertkit_v4/client/tags.rb deleted file mode 100644 index be45066..0000000 --- a/lib/convertkit_v4/client/tags.rb +++ /dev/null @@ -1,30 +0,0 @@ -module ConvertkitV4 - class Client - module Tags - - def tags - connection.get("tags").body["tags"] - end - - def create_tag(tag_name) - response = connection.post("tags") do |f| - f.body = JSON.generate({ - name: tag_name - }) - end - response.body - end - - def add_subscriber_to_tag(tag_id, options = {}) - response = connection.post("tags/#{tag_id}/subscribers") do |f| - f.body = JSON.generate({ - email: options[:email], - id: options[:id] - }) - end - response.body - end - - end - end -end diff --git a/lib/convertkit_v4/client/webhooks.rb b/lib/convertkit_v4/client/webhooks.rb deleted file mode 100644 index 4cd8a6c..0000000 --- a/lib/convertkit_v4/client/webhooks.rb +++ /dev/null @@ -1,21 +0,0 @@ -module ConvertkitV4 - class Client - module Webhooks - - def webhooks - connection.get("webhooks").body["webhooks"] - end - - def create_webhook(url, event) - response = connection.post("webhooks") do |f| - f.body = JSON.generate({ - target_url: url, - event: event - }) - end - response.body["webhook"] - end - - end - end -end diff --git a/lib/convertkit_v4/version.rb b/lib/convertkit_v4/version.rb deleted file mode 100644 index 05bdeba..0000000 --- a/lib/convertkit_v4/version.rb +++ /dev/null @@ -1,3 +0,0 @@ -module ConvertkitV4 - VERSION = "1.0.16" -end diff --git a/spec/convertkit_v4/client/account_spec.rb b/spec/convertkit/client/account_spec.rb similarity index 85% rename from spec/convertkit_v4/client/account_spec.rb rename to spec/convertkit/client/account_spec.rb index 2ab6d5c..d455d8b 100644 --- a/spec/convertkit_v4/client/account_spec.rb +++ b/spec/convertkit/client/account_spec.rb @@ -1,10 +1,10 @@ require "spec_helper" -module ConvertkitV4 +module Convertkit class Client describe Account do before do - @client = ConvertkitV4::Client.new + @client = Convertkit::Client.new end describe "#account" do diff --git a/spec/convertkit_v4/client/custom_fields_spec.rb b/spec/convertkit/client/custom_fields_spec.rb similarity index 95% rename from spec/convertkit_v4/client/custom_fields_spec.rb rename to spec/convertkit/client/custom_fields_spec.rb index 3305e01..6737e1e 100644 --- a/spec/convertkit_v4/client/custom_fields_spec.rb +++ b/spec/convertkit/client/custom_fields_spec.rb @@ -1,10 +1,10 @@ require "spec_helper" -module ConvertkitV4 +module Convertkit class Client describe CustomFields do before do - @client = ConvertkitV4::Client.new + @client = Convertkit::Client.new end describe "#custom_fields" do diff --git a/spec/convertkit_v4/client/forms_spec.rb b/spec/convertkit/client/forms_spec.rb similarity index 91% rename from spec/convertkit_v4/client/forms_spec.rb rename to spec/convertkit/client/forms_spec.rb index 9d3fa1e..7ec1a56 100644 --- a/spec/convertkit_v4/client/forms_spec.rb +++ b/spec/convertkit/client/forms_spec.rb @@ -1,10 +1,10 @@ require "spec_helper" -module ConvertkitV4 +module Convertkit class Client describe Forms do before do - @client = ConvertkitV4::Client.new + @client = Convertkit::Client.new end describe "#forms" do diff --git a/spec/convertkit_v4/client/sequences_spec.rb b/spec/convertkit/client/sequences_spec.rb similarity index 90% rename from spec/convertkit_v4/client/sequences_spec.rb rename to spec/convertkit/client/sequences_spec.rb index 5c515c0..5f01b97 100644 --- a/spec/convertkit_v4/client/sequences_spec.rb +++ b/spec/convertkit/client/sequences_spec.rb @@ -1,15 +1,15 @@ require "spec_helper" -module ConvertkitV4 +module Convertkit class Client describe Sequences do before do - ConvertkitV4.configure do |config| + Convertkit.configure do |config| config.api_secret = ENV["API_SECRET"] config.api_key = ENV["API_KEY"] end - @client = ConvertkitV4::Client.new + @client = Convertkit::Client.new end describe "#sequences" do diff --git a/spec/convertkit_v4/client/subscribers_spec.rb b/spec/convertkit/client/subscribers_spec.rb similarity index 96% rename from spec/convertkit_v4/client/subscribers_spec.rb rename to spec/convertkit/client/subscribers_spec.rb index cb12048..5657cea 100644 --- a/spec/convertkit_v4/client/subscribers_spec.rb +++ b/spec/convertkit/client/subscribers_spec.rb @@ -1,16 +1,16 @@ require "spec_helper" require "securerandom" -module ConvertkitV4 +module Convertkit class Client describe Subscribers do before do - ConvertkitV4.configure do |config| + Convertkit.configure do |config| config.api_secret = ENV["API_SECRET"] config.api_key = ENV["API_KEY"] end - @client = ConvertkitV4::Client.new + @client = Convertkit::Client.new end describe "#subscribers" do diff --git a/spec/convertkit_v4/client/tags_spec.rb b/spec/convertkit/client/tags_spec.rb similarity index 96% rename from spec/convertkit_v4/client/tags_spec.rb rename to spec/convertkit/client/tags_spec.rb index 5ca9d6d..5e6d0c1 100644 --- a/spec/convertkit_v4/client/tags_spec.rb +++ b/spec/convertkit/client/tags_spec.rb @@ -1,16 +1,16 @@ require "spec_helper" require "securerandom" -module ConvertkitV4 +module Convertkit class Client describe Tags do before do - ConvertkitV4.configure do |config| + Convertkit.configure do |config| config.api_secret = ENV["API_SECRET"] config.api_key = ENV["API_KEY"] end - @client = ConvertkitV4::Client.new + @client = Convertkit::Client.new end describe "#tags" do diff --git a/spec/convertkit_v4/client/webhook_spec.rb b/spec/convertkit/client/webhook_spec.rb similarity index 91% rename from spec/convertkit_v4/client/webhook_spec.rb rename to spec/convertkit/client/webhook_spec.rb index 45be9a7..8715b08 100644 --- a/spec/convertkit_v4/client/webhook_spec.rb +++ b/spec/convertkit/client/webhook_spec.rb @@ -1,16 +1,16 @@ require "spec_helper" require "securerandom" -module ConvertkitV4 +module Convertkit class Client describe Webhooks do before do - ConvertkitV4.configure do |config| + Convertkit.configure do |config| config.api_secret = ENV["API_SECRET"] config.api_key = ENV["API_KEY"] end - @client = ConvertkitV4::Client.new + @client = Convertkit::Client.new @url = "https://webhook.site/2bc3b03e-0f34-4a00-b2a9-028eff304f50" @events = {"name": "subscriber.subscriber_activate"} @rule_id = 1175937 diff --git a/spec/convertkit/client_spec.rb b/spec/convertkit/client_spec.rb new file mode 100644 index 0000000..47e9b4b --- /dev/null +++ b/spec/convertkit/client_spec.rb @@ -0,0 +1,20 @@ +require "spec_helper" +require "base64" + +module Convertkit + describe Client do + before :each do + @client = Convertkit::Client.new + end + + describe "#initialize" do + it "has a default api_secret value" do + expect(@client.api_secret).to eq(ENV["API_SECRET"]) + end + + it "has a default api_key value" do + expect(@client.api_key).to eq(ENV["API_KEY"]) + end + end + end +end diff --git a/spec/convertkit_v4/connection_spec.rb b/spec/convertkit/connection_spec.rb similarity index 75% rename from spec/convertkit_v4/connection_spec.rb rename to spec/convertkit/connection_spec.rb index 0c2a899..52b41fb 100644 --- a/spec/convertkit_v4/connection_spec.rb +++ b/spec/convertkit/connection_spec.rb @@ -1,19 +1,26 @@ require "spec_helper" -module ConvertkitV4 +module Convertkit describe Connection do describe "#initialize" do - it "uses access_token" do - access_token = "access_token" - connection = Connection.new(access_token: access_token) + it "uses api key" do + api_key = "api_key" + connection = Connection.new(api_key: api_key) - expect(connection.http_connection.params["access_token"]).to eq(access_token) + expect(connection.http_connection.params["api_key"]).to eq(api_key) + end + + it "uses api secret" do + api_secret = "api_secret" + connection = Connection.new(api_secret: api_secret) + + expect(connection.http_connection.params["api_secret"]).to eq(api_secret) end end describe "error handling" do before do - @connection = Connection.new(access_token: "access_token") + @connection = Connection.new(api_key: "api_key") end it "handles 401 errors" do diff --git a/spec/convertkit/convertkit_spec.rb b/spec/convertkit/convertkit_spec.rb new file mode 100644 index 0000000..875c767 --- /dev/null +++ b/spec/convertkit/convertkit_spec.rb @@ -0,0 +1,49 @@ +require "spec_helper" + +describe Convertkit do + it 'has a version number' do + expect(Convertkit::VERSION).not_to be nil + end + + describe "#configure" do + before do + @old_configuration = Convertkit.configuration.dup + end + + after do + Convertkit.configuration = @old_configuration + end + + it "sets the api_secret value" do + Convertkit.configure do |config| + config.api_secret = "new_secret" + end + + expect(Convertkit.configuration.api_secret).to eql("new_secret") + end + + it "sets the api_key value" do + Convertkit.configure do |config| + config.api_key = "new_key" + end + + expect(Convertkit.configuration.api_key).to eql("new_key") + end + + it "sets the timeout value" do + Convertkit.configure do |config| + config.timeout = 10 + end + + expect(Convertkit.configuration.timeout).to eql(10) + end + + it "sets the open_timeout value" do + Convertkit.configure do |config| + config.open_timeout = 10 + end + + expect(Convertkit.configuration.open_timeout).to eql(10) + end + end +end diff --git a/spec/convertkit_v4/client/broadcasts_spec.rb b/spec/convertkit_v4/client/broadcasts_spec.rb deleted file mode 100644 index 94fed14..0000000 --- a/spec/convertkit_v4/client/broadcasts_spec.rb +++ /dev/null @@ -1,43 +0,0 @@ -require "spec_helper" -require "securerandom" - -module ConvertkitV4 - class Client - describe Broadcasts do - before do - ConvertkitV4.configure do |config| - config.api_secret = ENV["API_SECRET"] - config.api_key = ENV["API_KEY"] - end - - @client = ConvertkitV4::Client.new - end - - describe "#broadcasts" do - it "sends the right request" do - r = @client.broadcasts - expect(r.success?).to be_truthy - expect(r.body).to_not eql({"error"=>"Authorization Failed", "message"=>"API Key not present"}) - end - end - - describe "#broadcast" do - it "sends the right request" do - broadcast_id = ENV['BROADCAST_ID'] - - r = @client.broadcast(broadcast_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 - - describe "#broadcast_stats" do - it "sends the right request" do - broadcast_id = ENV['BROADCAST_ID'] - - r = @client.broadcast_stats(broadcast_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 diff --git a/spec/convertkit_v4/client_spec.rb b/spec/convertkit_v4/client_spec.rb deleted file mode 100644 index 05eaabc..0000000 --- a/spec/convertkit_v4/client_spec.rb +++ /dev/null @@ -1,24 +0,0 @@ -require "spec_helper" -require "base64" - -module ConvertkitV4 - describe Client do - before :each do - @client = ConvertkitV4::Client.new - end - - describe "#initialize" do - it "has a default client_id value" do - expect(@client.client_id).to eq(ENV["CONVERTKIT_CLIENT_ID"]) - end - - it "has a default client_secret value" do - expect(@client.client_secret).to eq(ENV["CONVERTKIT_CLIENT_SECRET"]) - end - - it "has a default redirect_uri value" do - expect(@client.redirect_uri).to eq(ENV["CONVERTKIT_REDIRECT_URI"]) - end - end - end -end diff --git a/spec/convertkit_v4/convertkit_v4_spec.rb b/spec/convertkit_v4/convertkit_v4_spec.rb deleted file mode 100644 index b04c5a6..0000000 --- a/spec/convertkit_v4/convertkit_v4_spec.rb +++ /dev/null @@ -1,41 +0,0 @@ -require "spec_helper" - -describe ConvertkitV4 do - it 'has a version number' do - expect(ConvertkitV4::VERSION).not_to be nil - end - - describe "#configure" do - before do - @old_configuration = ConvertkitV4.configuration.dup - end - - after do - ConvertkitV4.configuration = @old_configuration - end - - it "sets the client_id value" do - ConvertkitV4.configure do |config| - config.client_id = "new_client_id" - end - - expect(ConvertkitV4.configuration.client_id).to eql("new_client_id") - end - - it "sets the timeout value" do - ConvertkitV4.configure do |config| - config.timeout = 10 - end - - expect(ConvertkitV4.configuration.timeout).to eql(10) - end - - it "sets the open_timeout value" do - ConvertkitV4.configure do |config| - config.open_timeout = 10 - end - - expect(ConvertkitV4.configuration.open_timeout).to eql(10) - end - end -end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index da97bce..1340fdd 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,20 +1,24 @@ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) -require 'convertkit_v4' +require 'convertkit' require "dotenv" Dotenv.load(".env.local") require 'webmock/rspec' require 'vcr' -ConvertkitV4.configure do |config| - config.access_token = ENV["CONVERTKIT_ACCESS_TOKEN"] +Convertkit.configure do |config| + config.api_secret = ENV["API_SECRET"] + config.api_key = ENV["API_KEY"] end VCR.configure do |config| config.cassette_library_dir = "fixtures/vcr_cassettes" config.hook_into :webmock - config.filter_sensitive_data("") { ENV["CONVERTKIT_ACCESS_TOKEN"] } + config.filter_sensitive_data("") { ENV["API_SECRET"] } + config.filter_sensitive_data("") { ENV["API_KEY"] } config.allow_http_connections_when_no_cassette = true end WebMock.allow_net_connect! + + From 58c2eb85251696e231b2a6b886d3a3e718987b81 Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Sat, 2 Aug 2025 21:24:27 +0200 Subject: [PATCH 25/26] endpoint --- lib/convertkit/client/subscribers.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/convertkit/client/subscribers.rb b/lib/convertkit/client/subscribers.rb index 92ecd86..1cfd338 100644 --- a/lib/convertkit/client/subscribers.rb +++ b/lib/convertkit/client/subscribers.rb @@ -13,6 +13,29 @@ def subscriber_tags(subscriber_id) connection.get("subscribers/#{subscriber_id}/tags") end + # returns an hash with the following keys: + # { + # "subscriber": { + # "id": 3436990966, + # "stats": { + # "sent": 2, + # "opened": 2, + # "clicked": 2, + # "bounced": 0, + # "open_rate": 1, + # "click_rate": 1, + # "last_sent": "2025-06-25 16:44:37.000", + # "last_opened": "2025-06-25 16:45:36.000", + # "last_clicked": "2025-06-25 16:45:42.000", + # "sent_since_last_open": 0, + # "sent_since_last_click": 0 + # } + # } + # } + def subscriber_stats(subscriber_id) + connection.get("subscribers/#{subscriber_id}/stats") + end + def update_subscriber(subscriber_id, options = {}) response = connection.put("subscribers/#{subscriber_id}") do |f| f.params["email_address"] = options[:email_address] if options[:email_address] From 78377ef80ca7f9e7f5c15ee73d1a227a12c6d7b3 Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Sat, 2 Aug 2025 21:24:30 +0200 Subject: [PATCH 26/26] specs --- spec/convertkit/client/subscribers_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/convertkit/client/subscribers_spec.rb b/spec/convertkit/client/subscribers_spec.rb index 5657cea..aad43ea 100644 --- a/spec/convertkit/client/subscribers_spec.rb +++ b/spec/convertkit/client/subscribers_spec.rb @@ -31,6 +31,14 @@ class Client end end + describe "#subscriber_stats" do + it "sends the right request" do + subscriber_id = ENV['SUBSCRIBER_ID'] + r = @client.subscriber_stats(subscriber_id) + expect(r.success?).to be_truthy + end + end + describe "#update_subscriber" do it "updates a subscriber" do old_email = "old-email-#{Time.now.to_i}@example.com"