Class: Mailjet::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/mailjet/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(end_point, api_key, secret_key, options = {}) ⇒ Connection

Returns a new instance of Connection.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mailjet/connection.rb', line 19

def initialize(end_point, api_key, secret_key, options = {})
  self.options = options
  self.api_key = api_key
  self.secret_key = secret_key
  self.public_operations = options[:public_operations] || []
  self.read_only = options[:read_only]
  self.adapter = Faraday.new(end_point, ssl: { verify: false }) do |conn|
    conn.response :raise_error, include_request: true
    conn.request :authorization, :basic, api_key, secret_key
    conn.headers['Content-Type'] = 'application/json'
  end
  self.perform_api_call = options.key?(:perform_api_call) ? options[:perform_api_call] : true
end

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



7
8
9
# File 'lib/mailjet/connection.rb', line 7

def adapter
  @adapter
end

#api_keyObject

Returns the value of attribute api_key.



7
8
9
# File 'lib/mailjet/connection.rb', line 7

def api_key
  @api_key
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/mailjet/connection.rb', line 7

def options
  @options
end

#perform_api_callObject

Returns the value of attribute perform_api_call.



7
8
9
# File 'lib/mailjet/connection.rb', line 7

def perform_api_call
  @perform_api_call
end

#public_operationsObject

Returns the value of attribute public_operations.



7
8
9
# File 'lib/mailjet/connection.rb', line 7

def public_operations
  @public_operations
end

#read_onlyObject Also known as: read_only?

Returns the value of attribute read_only.



7
8
9
# File 'lib/mailjet/connection.rb', line 7

def read_only
  @read_only
end

#secret_keyObject

Returns the value of attribute secret_key.



7
8
9
# File 'lib/mailjet/connection.rb', line 7

def secret_key
  @secret_key
end

Instance Method Details

#[](suburl, &new_block) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/mailjet/connection.rb', line 10

def [](suburl, &new_block)
  broken_url = uri.path.split("/")
  if broken_url.include?("contactslist") && broken_url.include?("managemanycontacts") && broken_url.last.to_i > 0
    self.class.new(uri, api_key, secret_key, options)
  else
    self.class.new(concat_urls(suburl), api_key, secret_key, options)
  end
end

#concat_urls(suburl) ⇒ Object



49
50
51
# File 'lib/mailjet/connection.rb', line 49

def concat_urls(suburl)
  self.adapter.build_url(suburl.to_s)
end

#delete(additional_headers = {}, &block) ⇒ Object



45
46
47
# File 'lib/mailjet/connection.rb', line 45

def delete(additional_headers = {}, &block)
  handle_api_call(:delete, additional_headers, &block)
end

#get(additional_headers = {}, &block) ⇒ Object



33
34
35
# File 'lib/mailjet/connection.rb', line 33

def get(additional_headers = {}, &block)
  handle_api_call(:get, additional_headers, &block)
end

#post(payload, additional_headers = {}, &block) ⇒ Object



37
38
39
# File 'lib/mailjet/connection.rb', line 37

def post(payload, additional_headers = {}, &block)
  handle_api_call(:post, additional_headers, payload, &block)
end

#put(payload, additional_headers = {}, &block) ⇒ Object



41
42
43
# File 'lib/mailjet/connection.rb', line 41

def put(payload, additional_headers = {}, &block)
  handle_api_call(:put, additional_headers, payload, &block)
end

#uriObject



53
54
55
# File 'lib/mailjet/connection.rb', line 53

def uri
  self.adapter.build_url
end