Class: Drip::Client

Inherits:
Object
  • Object
show all
Includes:
Events, Subscribers, Tags
Defined in:
lib/drip/client.rb,
lib/drip/client/tags.rb,
lib/drip/client/events.rb,
lib/drip/client/subscribers.rb

Defined Under Namespace

Modules: Events, Subscribers, Tags

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Events

#track_event

Methods included from Tags

#apply_tag, #remove_tag

Methods included from Subscribers

#create_or_update_subscriber, #subscribe, #subscriber, #unsubscribe

Constructor Details

#initialize {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:

  • _self (Drip::Client)

    the object that the method was called on



17
18
19
# File 'lib/drip/client.rb', line 17

def initialize
  yield(self) if block_given?
end

Instance Attribute Details

#account_idObject

Returns the value of attribute account_id.



15
16
17
# File 'lib/drip/client.rb', line 15

def 
  @account_id
end

#api_keyObject

Returns the value of attribute api_key.



15
16
17
# File 'lib/drip/client.rb', line 15

def api_key
  @api_key
end

Instance Method Details

#build_response(&block) ⇒ Object



65
66
67
68
# File 'lib/drip/client.rb', line 65

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

#connectionObject



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/drip/client.rb', line 70

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'] = "*/*"
    f.basic_auth api_key, ""
    f.response :json, :content_type => /\bjson$/
  end
end

#content_typeObject



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

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

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



56
57
58
59
60
61
62
63
# File 'lib/drip/client.rb', line 56

def delete(url, options = {})
  build_response do
    connection.delete do |req|
      req.url url
      req.body = options.to_json
    end
  end
end

#generate_resource(key, *args) ⇒ Object



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

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

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



29
30
31
32
33
34
35
36
# File 'lib/drip/client.rb', line 29

def get(url, options = {})
  build_response do
    connection.get do |req|
      req.url url
      req.params = options
    end
  end
end

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



38
39
40
41
42
43
44
45
# File 'lib/drip/client.rb', line 38

def post(url, options = {})
  build_response do
    connection.post do |req|
      req.url url
      req.body = options.to_json
    end
  end
end

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



47
48
49
50
51
52
53
54
# File 'lib/drip/client.rb', line 47

def put(url, options = {})
  build_response do
    connection.put do |req|
      req.url url
      req.body = options.to_json
    end
  end
end