|
| 1 | +require 'faraday' |
| 2 | + |
| 3 | +module Flodesk |
| 4 | + # Subscriber status |
| 5 | + ACTIVE = 'active'.freeze |
| 6 | + |
| 7 | + class Client |
| 8 | + API_ENDPOINT = 'https://api.flodesk.com/v1/'.freeze |
| 9 | + DEFAULT_TIMEOUT = 60 |
| 10 | + |
| 11 | + class_attribute :api_key |
| 12 | + class_attribute :complete_timeout |
| 13 | + class_attribute :open_timeout |
| 14 | + |
| 15 | + # Allows for setting these values in `config/initializers/flodesk.rb` |
| 16 | + class << self |
| 17 | + def api_key |
| 18 | + @@api_key |
| 19 | + end |
| 20 | + |
| 21 | + def complete_timeout |
| 22 | + @@complete_timeout |
| 23 | + end |
| 24 | + |
| 25 | + def open_timeout |
| 26 | + @@open_timeout |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + attr_accessor :api_endpoint, :debug, :logger |
| 31 | + |
| 32 | + # We need 3 actions: |
| 33 | + # |
| 34 | + # 1. subscribe --> params(list_id, email, first_name, last_name) |
| 35 | + # Documentation: https://developers.flodesk.com/#tag/subscriber/operation/createOrUpdateSubscriber |
| 36 | + # Endpoint: https://api.flodesk.com/v1/subscribers |
| 37 | + # |
| 38 | + # 2. unsubscribe --> params(list_id, email) |
| 39 | + # Documentation: https://developers.flodesk.com/#tag/subscriber/operation/removeSubscriberFromSegments |
| 40 | + # Endpoint: https://api.flodesk.com/v1/subscribers/{id_or_email}/segments |
| 41 | + # |
| 42 | + # 3. subscribed? --> params(list_id, email) |
| 43 | + # Documentation: https://developers.flodesk.com/#tag/subscriber/operation/retrieveSubscriber |
| 44 | + # Endpoint: https://api.flodesk.com/v1/subscribers/{id_or_email} |
| 45 | + |
| 46 | + def initialize(api_key: nil, complete_timeout: nil, open_timeout: nil) |
| 47 | + @api_key = api_key || self.class.api_key || ENV['FLODESK_KEY'] |
| 48 | + @api_key = @api_key.strip if @api_key |
| 49 | + |
| 50 | + @complete_timeout = complete_timeout || self.class.complete_timeout || DEFAULT_TIMEOUT |
| 51 | + @open_timeout = open_timeout || self.class.open_timeout || DEFAULT_TIMEOUT |
| 52 | + end |
| 53 | + |
| 54 | + def disabled? |
| 55 | + !@api_key |
| 56 | + end |
| 57 | + |
| 58 | + def subscribe(email:, first_name:, last_name:, segment_ids:, double_optin: true) |
| 59 | + body = { email:, first_name:, last_name:, segment_ids:, double_optin: } |
| 60 | + |
| 61 | + request(:post, 'subscribers', body) |
| 62 | + end |
| 63 | + |
| 64 | + def unsubscribe(email:, segment_ids:) |
| 65 | + body = { segment_ids: } |
| 66 | + |
| 67 | + request(:delete, "subscribers/#{email}/segments", body) |
| 68 | + end |
| 69 | + |
| 70 | + def subscribed?(email:, segment_ids:) |
| 71 | + response = request(:get, "subscribers/#{email}") |
| 72 | + response => { status:, body: } |
| 73 | + |
| 74 | + return false if response.is_a?(FlodeskError) && status == 404 |
| 75 | + |
| 76 | + body.symbolize_keys => { status:, segments: } |
| 77 | + |
| 78 | + # If not subscribed, stop here |
| 79 | + is_active = status.to_s.eql?(ACTIVE) |
| 80 | + return false unless is_active |
| 81 | + |
| 82 | + segment_ids.all? do |segment_id| |
| 83 | + segments.any? { |segment| segment_id.to_s.eql?(segment.symbolize_keys[:id]) } |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + private |
| 88 | + |
| 89 | + def connection |
| 90 | + options = { |
| 91 | + headers: { |
| 92 | + user_agent: 'codebar (codebar.io)' |
| 93 | + }, |
| 94 | + request: { |
| 95 | + timeout: @complete_timeout, |
| 96 | + open_timeout: @open_timeout |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + # https://lostisland.github.io/faraday/#/customization/request-options |
| 101 | + @connection ||= Faraday.new(url: API_ENDPOINT, **options) do |config| |
| 102 | + config.request :json |
| 103 | + |
| 104 | + # Beware: the order of these lines matter. Examples: |
| 105 | + # - https://mattbrictson.com/blog/advanced-http-techniques-in-ruby#pitfall-raise_error-and-logger-in-the-wrong-order |
| 106 | + # - https://stackoverflow.com/a/67182791/590525 |
| 107 | + config.response :raise_error |
| 108 | + config.response :logger, Rails.logger, headers: true, bodies: true, log_level: :debug |
| 109 | + config.response :json |
| 110 | + |
| 111 | + # https://developers.flodesk.com/#section/Authentication/api_key |
| 112 | + config.request :authorization, 'Basic', -> { @api_key } |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + def request(http_method, endpoint, body = {}) |
| 117 | + # Faraday's `delete` does not accept body at the time of writing |
| 118 | + response = if http_method == :delete |
| 119 | + connection.run_request(http_method, endpoint, body, nil) |
| 120 | + else |
| 121 | + connection.public_send(http_method, endpoint, body) |
| 122 | + end |
| 123 | + |
| 124 | + { |
| 125 | + status: response.status, |
| 126 | + body: response.body |
| 127 | + } |
| 128 | + rescue Faraday::Error => e |
| 129 | + FlodeskError.new(e.response_body['message'], { |
| 130 | + raw_body: e.response_body, |
| 131 | + status_code: e.response_status |
| 132 | + }) |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + # Inspired by https://github.com/amro/gibbon/blob/master/lib/gibbon/mailchimp_error.rb |
| 137 | + class FlodeskError < StandardError |
| 138 | + attr_reader :status_code, :raw_body |
| 139 | + |
| 140 | + def initialize(message = '', params = {}) |
| 141 | + @status_code = params[:status_code] |
| 142 | + @raw_body = params[:raw_body] |
| 143 | + |
| 144 | + super(message) |
| 145 | + end |
| 146 | + |
| 147 | + def to_s |
| 148 | + "#{super} #{instance_variables_to_s}" |
| 149 | + end |
| 150 | + |
| 151 | + private |
| 152 | + |
| 153 | + def instance_variables_to_s |
| 154 | + %i[status_code raw_body].map do |attr| |
| 155 | + attr_value = send(attr) |
| 156 | + |
| 157 | + "@#{attr}=#{attr_value.inspect}" |
| 158 | + end.join(', ') |
| 159 | + end |
| 160 | + end |
| 161 | +end |
0 commit comments