Class: Drip::Client

Inherits:
Object
  • Object
show all
Includes:
Accounts, Campaigns, Events, Purchases, Subscribers, Tags
Defined in:
lib/drip/client.rb,
lib/drip/client/tags.rb,
lib/drip/client/events.rb,
lib/drip/client/accounts.rb,
lib/drip/client/campaigns.rb,
lib/drip/client/purchases.rb,
lib/drip/client/subscribers.rb

Defined Under Namespace

Modules: Accounts, Campaigns, Events, Purchases, Subscribers, Tags

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Purchases

#create_purchase, #purchase, #purchases

Methods included from Campaigns

#campaigns

Methods included from Events

#track_event, #track_events

Methods included from Tags

#apply_tag, #remove_tag, #tags

Methods included from Subscribers

#create_or_update_subscriber, #create_or_update_subscribers, #delete_subscriber, #subscribe, #subscriber, #subscribers, #unsubscribe

Methods included from Accounts

#accounts

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:

  • _self (Drip::Client)

    the object that the method was called on



23
24
25
26
27
28
# File 'lib/drip/client.rb', line 23

def initialize(options = {})
  @account_id = options[:account_id]
  @access_token = options[:access_token]
  @api_key = options[:api_key]
  yield(self) if block_given?
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



21
22
23
# File 'lib/drip/client.rb', line 21

def access_token
  @access_token
end

#account_idObject

Returns the value of attribute account_id.



21
22
23
# File 'lib/drip/client.rb', line 21

def 
  @account_id
end

#api_keyObject

Returns the value of attribute api_key.



21
22
23
# File 'lib/drip/client.rb', line 21

def api_key
  @api_key
end

Instance Method Details

#build_response(&block) ⇒ Object



68
69
70
71
# File 'lib/drip/client.rb', line 68

def build_response(&block)
  response = yield
  Drip::Response.new(response.status, response.body)
end

#connectionObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/drip/client.rb', line 73

def connection
  @connection ||= Faraday.new do |f|
    f.adapter :net_http
    f.url_prefix = "https://api.getdrip.com/v2/"
    f.headers['User-Agent'] = "Drip Ruby v#{Drip::VERSION}"
    f.headers['Content-Type'] = content_type
    f.headers['Accept'] = "*/*"

    if access_token
      f.headers['Authorization'] = "Bearer #{access_token}"
    else
      f.basic_auth api_key, ""
    end

    f.response :json, :content_type => /\bjson$/
  end
end

#content_typeObject



34
35
36
# File 'lib/drip/client.rb', line 34

def content_type
  'application/vnd.api+json'
end

#delete(url, options = {}) ⇒ Object



50
51
52
# File 'lib/drip/client.rb', line 50

def delete(url, options = {})
  make_request(:delete, url, options)
end

#generate_resource(key, *args) ⇒ Object



30
31
32
# File 'lib/drip/client.rb', line 30

def generate_resource(key, *args)
  { key => args }
end

#get(url, options = {}) ⇒ Object



38
39
40
# File 'lib/drip/client.rb', line 38

def get(url, options = {})
  make_request(:get, url, options)
end

#make_request(verb, url, options) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/drip/client.rb', line 54

def make_request(verb, url, options)
  build_response do
    connection.send(verb) do |req|
      req.url url

      if verb == :get
        req.params = options
      else
        req.body = options.to_json
      end
    end
  end
end

#post(url, options = {}) ⇒ Object



42
43
44
# File 'lib/drip/client.rb', line 42

def post(url, options = {})
  make_request(:post, url, options)
end

#put(url, options = {}) ⇒ Object



46
47
48
# File 'lib/drip/client.rb', line 46

def put(url, options = {})
  make_request(:put, url, options)
end