From c07cedec20309a6cc1e9c4e58016ac1741b18336 Mon Sep 17 00:00:00 2001 From: Manuel Frigerio Date: Tue, 6 Feb 2024 19:39:40 +0000 Subject: [PATCH] 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! + +