Class: SendGridWebApi::Base
- Inherits:
-
Object
- Object
- SendGridWebApi::Base
- Defined in:
- lib/base.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
Instance Method Summary collapse
- #builder_options(options) ⇒ Object
-
#initialize(api_key) ⇒ Base
constructor
A new instance of Base.
- #make_request_url(url, options) ⇒ Object
- #query_api(url, options) ⇒ Object
- #query_delete_api(url, options) ⇒ Object
- #query_patch_api(url, options) ⇒ Object
- #query_post_api(url, options) ⇒ Object
- #query_post_json_api(url, options) ⇒ Object
- #session ⇒ Object
- #to_query(options) ⇒ Object
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_key ⇒ Object
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 to_query() end |
#make_request_url(url, options) ⇒ Object
11 12 13 |
# File 'lib/base.rb', line 11 def make_request_url url, "#{url}?#{()}" end |
#query_api(url, options) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/base.rb', line 19 def query_api url, headers = .delete(:headers) (session.get(make_request_url(url, )) 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, headers = .delete(:headers) (session.delete(make_request_url(url, )) 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, headers = .delete(:headers) (session.patch(url) do |req| req.headers[:content_type] = 'application/json' req.headers.merge!(headers) unless headers.nil? req.body = .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, headers = .delete(:headers) (session.post(url, ()) 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, headers = .delete(:headers) (session.post(url) do |req| req.headers[:content_type] = 'application/json' req.headers.merge!(headers) unless headers.nil? req.body = .to_json end).body end |
#session ⇒ Object
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() Faraday::Utils.build_nested_query() end |