Module: SparkPost::Request
- Included in:
- Template, Transmission
- Defined in:
- lib/sparkpost/request.rb
Class Method Summary collapse
- .configure_http(uri) ⇒ Object
- .configure_request(uri, headers, data, verb) ⇒ Object
- .process_response(response) ⇒ Object
- .request(url, api_key, data, verb = 'POST') ⇒ Object
Instance Method Summary collapse
Class Method Details
.configure_http(uri) ⇒ Object
34 35 36 37 38 |
# File 'lib/sparkpost/request.rb', line 34 def configure_http(uri) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http end |
.configure_request(uri, headers, data, verb) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/sparkpost/request.rb', line 40 def configure_request(uri, headers, data, verb) req = case verb when 'GET' Net::HTTP::Get.new(uri.path, headers) when 'PUT' Net::HTTP::Put.new(uri.path, headers) when 'DELETE' Net::HTTP::Delete.new(uri.path, headers) else Net::HTTP::Post.new(uri.path, headers) end req.body = JSON.generate(data) if data.present? req end |
.process_response(response) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/sparkpost/request.rb', line 56 def process_response(response) response = JSON.parse(response.body) if response['errors'] raise SparkPost::DeliveryException, response['errors'] else response['results'] end end |
.request(url, api_key, data, verb = 'POST') ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/sparkpost/request.rb', line 9 def request(url, api_key, data, verb = 'POST') uri = URI.parse(url) http = configure_http(uri) headers = { 'User-Agent' => 'ruby-sparkpost/' + VERSION, 'Content-Type' => 'application/json', 'Authorization' => api_key } req = configure_request(uri, headers, data, verb) process_response(http.request(req)) end |
Instance Method Details
#endpoint(subpath = nil, params = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/sparkpost/request.rb', line 21 def endpoint(subpath = nil, params = {}) url = String.new(@base_endpoint) if subpath url << '/' unless subpath.start_with?('/') url << subpath end if params && params.any? url << '?' url << params.to_a.map { |x| "#{x[0]}=#{x[1]}" }.join('&') end url end |