Class: MailChimp3::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/mailchimp3/endpoint.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oauth_access_token: nil, basic_auth_key: nil, dc: nil, url: nil, version: 3) ⇒ Endpoint

Returns a new instance of Endpoint.



8
9
10
11
12
13
14
15
16
17
# File 'lib/mailchimp3/endpoint.rb', line 8

def initialize(oauth_access_token: nil, basic_auth_key: nil, dc: nil, url: nil, version: 3)
  @oauth_access_token = oauth_access_token
  @basic_auth_key = basic_auth_key
  @dc = dc
  @dc ||= @basic_auth_key.split('-').last if @basic_auth_key
  @url = url || _build_url
  @version = version
  fail Errors::DataCenterRequiredError, 'You must pass dc.' unless @dc || @url
  @cache = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(endpoint_name, *_args) ⇒ Object



19
20
21
# File 'lib/mailchimp3/endpoint.rb', line 19

def method_missing(endpoint_name, *_args)
  _build_endpoint(endpoint_name.to_s)
end

Instance Attribute Details

#last_resultObject (readonly)

Returns the value of attribute last_result.



6
7
8
# File 'lib/mailchimp3/endpoint.rb', line 6

def last_result
  @last_result
end

#urlObject (readonly)

Returns the value of attribute url.



6
7
8
# File 'lib/mailchimp3/endpoint.rb', line 6

def url
  @url
end

Instance Method Details

#[](endpoint_name_or_id) ⇒ Object



23
24
25
# File 'lib/mailchimp3/endpoint.rb', line 23

def [](endpoint_name_or_id)
  _build_endpoint(endpoint_name_or_id.to_s)
end

#deleteObject



58
59
60
61
62
63
64
65
# File 'lib/mailchimp3/endpoint.rb', line 58

def delete
  @last_result = _connection.delete(@url)
  if @last_result.status == 204
    true
  else
    _build_response(@last_result)
  end
end

#get(params = {}) ⇒ Object



27
28
29
30
# File 'lib/mailchimp3/endpoint.rb', line 27

def get(params = {})
  @last_result = _connection.get(@url, params)
  _build_response(@last_result)
end

#patch(body = {}) ⇒ Object



44
45
46
47
48
49
# File 'lib/mailchimp3/endpoint.rb', line 44

def patch(body = {})
  @last_result = _connection.patch(@url) do |req|
    req.body = body.to_json
  end
  _build_response(@last_result)
end

#post(body = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mailchimp3/endpoint.rb', line 32

def post(body = {})
  @last_result = _connection.post(@url) do |req|
    body[:apikey] = @oauth_access_token || @basic_auth_key if @version == 2
    req.body = body.to_json
  end
  if @last_result.status == 204
    true
  else
    _build_response(@last_result)
  end
end

#put(body = {}) ⇒ Object



51
52
53
54
55
56
# File 'lib/mailchimp3/endpoint.rb', line 51

def put(body = {})
  @last_result = _connection.put(@url) do |req|
    req.body = body.to_json
  end
  _build_response(@last_result)
end

#v2Object



67
68
69
70
71
72
73
74
# File 'lib/mailchimp3/endpoint.rb', line 67

def v2
  self.class.new(
    url: _build_v2_url,
    basic_auth_key: @basic_auth_key,
    oauth_access_token: @oauth_access_token,
    version: 2
  )
end