class Outreach::Request

Public Class Methods

new(access_token=nil) click to toggle source
# File lib/outreach/request.rb, line 11
def initialize(access_token=nil)
  @access_token = access_token
end

Public Instance Methods

delete(url) click to toggle source
# File lib/outreach/request.rb, line 44
def delete(url)
  attrs = {
    headers: auth_header
  }
  attrs[:debug_output] = $stdout if Outreach.debug
  response = HTTParty.delete(url, attrs)
  parse_response(response)
end
get(url, query={}) click to toggle source
# File lib/outreach/request.rb, line 15
def get(url, query={})
  attrs = {
    query: query, headers: auth_header
  }
  attrs[:debug_output] = $stdout if Outreach.debug
  response = HTTParty.get(url, attrs)
  parse_response(response)
end
patch(url, params) click to toggle source
# File lib/outreach/request.rb, line 34
def patch(url, params)
  response_format = params.delete(:response_format) || :json
  attrs = {
    body: params.to_json, headers: auth_header
  }
  attrs[:debug_output] = $stdout if Outreach.debug
  response = HTTParty.patch(url, attrs)
  parse_response(response, response_format)
end
post(url, params) click to toggle source
# File lib/outreach/request.rb, line 24
def post(url, params)
  response_format = params.delete(:response_format) || :json
  attrs = {
    body: params.to_json, headers: auth_header
  }
  attrs[:debug_output] = $stdout if Outreach.debug
  response = HTTParty.post(url, attrs)
  parse_response(response, response_format)
end

Private Instance Methods

auth_header() click to toggle source
# File lib/outreach/request.rb, line 73
def auth_header
  headers = { 'Content-Type' => 'application/json' }
  headers["Authorization"] = "Bearer #{@access_token}" if @access_token
  headers
end
display_debug(response) click to toggle source
# File lib/outreach/request.rb, line 65
def display_debug(response)
  if Outreach.debug
    puts "-" * 20 + " DEBUG " + "-" * 20
    puts response
    puts "-" * 18 + " END DEBUG " + "-" * 18
  end
end
parse_response(response, response_format=:json) click to toggle source
# File lib/outreach/request.rb, line 55
def parse_response(response, response_format=:json)
  check_for_error(response.response.code, response.body)
  display_debug(response.body)
  if response_format == :json
    JSON.parse(response.body.to_s)
  else
    response.body.to_s
  end
end