Class: SendGridWebApi::Base

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

Direct Known Subclasses

Client, SubUser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_user, api_key) ⇒ Base

Returns a new instance of Base.



7
8
9
10
# File 'lib/base.rb', line 7

def initialize api_user, api_key
  @api_user = api_user
  @api_key  = api_key
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



5
6
7
# File 'lib/base.rb', line 5

def api_key
  @api_key
end

#api_userObject

Returns the value of attribute api_user.



5
6
7
# File 'lib/base.rb', line 5

def api_user
  @api_user
end

Instance Method Details

#builder_options(options) ⇒ Object



16
17
18
19
# File 'lib/base.rb', line 16

def builder_options options
  options.merge!(:api_user => @api_user, :api_key => @api_key)
  to_query(options)
end

#make_request_url(url, options) ⇒ Object



12
13
14
# File 'lib/base.rb', line 12

def make_request_url url, options
  "#{url}?#{builder_options(options)}"
end

#query_api(url, options) ⇒ Object



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

def query_api url, options
  session.get(make_request_url(url, options)).body
end

#query_post_api(url, options) ⇒ Object



25
26
27
28
# File 'lib/base.rb', line 25

def query_post_api url, options
  builder_options(options)
  session.post(url, builder_options(options)).body
end

#sessionObject



34
35
36
37
38
39
40
41
42
# File 'lib/base.rb', line 34

def session
  @connection ||= ::Faraday.new base_url do |conn|
    # Forces the connection request and response to be JSON even though
    # Sendgrids API headers do not specify the content type is JSON
    conn.request :url_encoded
    conn.response :json #, :content_type => /\bjson$/
    conn.adapter Faraday.default_adapter
  end
end

#to_query(options) ⇒ Object



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

def to_query(options)
  Faraday::Utils.build_nested_query(options)
end