Class: SendGridWebApi::Base

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

Direct Known Subclasses

ApiKey, Client, SubUser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Base

Returns a new instance of Base.



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

def initialize api_key
  @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

Instance Method Details

#builder_options(options) ⇒ Object



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

def builder_options options
  to_query(options)
end

#make_request_url(url, options) ⇒ Object



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

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

#query_api(url, options) ⇒ Object



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

def query_api url, options
  headers = options.delete(:headers)
  (session.get(make_request_url(url, options)) do |req|
    req.headers.merge!(headers) unless headers.nil?
  end).body
end

#query_delete_api(url, options) ⇒ Object



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

def query_delete_api url, options
  headers = options.delete(:headers)
  (session.delete(make_request_url(url, options)) do |req|
    req.headers.merge!(headers) unless headers.nil?
  end).body
end

#query_patch_api(url, options) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/base.rb', line 42

def query_patch_api url, options
  headers = options.delete(:headers)
  (session.patch(url) do |req|
    req.headers[:content_type] = 'application/json'
    req.headers.merge!(headers) unless headers.nil?
    req.body = options.to_json
  end).body
end

#query_post_api(url, options) ⇒ Object



26
27
28
29
30
31
# File 'lib/base.rb', line 26

def query_post_api url, options
  headers = options.delete(:headers)
  (session.post(url, builder_options(options)) do |req|
    req.headers.merge!(headers) unless headers.nil?
  end).body
end

#query_post_json_api(url, options) ⇒ Object



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

def query_post_json_api url, options
  headers = options.delete(:headers)
  (session.post(url) do |req|
    req.headers[:content_type] = 'application/json'
    req.headers.merge!(headers) unless headers.nil?
    req.body = options.to_json
  end).body
end

#sessionObject



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/base.rb', line 62

def session
  @connection ||= ::Faraday.new(
    url: base_url,
    headers: {"Authorization" => "Bearer #{@api_key}"}
  ) 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



58
59
60
# File 'lib/base.rb', line 58

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