module PagerDuty::Connection

Attributes

agent[W]

Public Instance Methods

agent() click to toggle source
# File lib/pager_duty/connection.rb, line 25
def agent
  @agent ||= Sawyer::Agent.new(endpoint, sawyer_options) do |http|
    http.headers[:accept]       = default_media_type
    http.headers[:content_type] = "application/json"
    http.headers[:user_agent]   = user_agent
    http.token_auth @api_token
  end
end
delete(url, options = {}) click to toggle source
# File lib/pager_duty/connection.rb, line 21
def delete(url, options = {})
  request :delete, url, options
end
get(url, options = {}) click to toggle source
# File lib/pager_duty/connection.rb, line 5
def get(url, options = {})
  request :get, url, options
end
last_response() click to toggle source

Response for last HTTP request

@return [Sawyer::Response]

# File lib/pager_duty/connection.rb, line 44
def last_response
  @last_response if defined? @last_response
end
patch(url, options = {}) click to toggle source
# File lib/pager_duty/connection.rb, line 17
def patch(url, options = {})
  request :patch, url, options
end
post(url, options = {}) click to toggle source
# File lib/pager_duty/connection.rb, line 9
def post(url, options = {})
  request :post, url, options
end
put(url, options = {}) click to toggle source
# File lib/pager_duty/connection.rb, line 13
def put(url, options = {})
  request :put, url, options
end
root() click to toggle source

Fetch the root resource for the API

@return [Sawyer::Resource]

# File lib/pager_duty/connection.rb, line 37
def root
  get "/"
end

Protected Instance Methods

endpoint() click to toggle source
# File lib/pager_duty/connection.rb, line 50
def endpoint
  api_endpoint
end

Private Instance Methods

boolean_from_response(method, path, options = {}) click to toggle source

Executes the request, checking if it was successful

@return [Boolean] True on success, false otherwise

# File lib/pager_duty/connection.rb, line 75
def boolean_from_response(method, path, options = {})
  request(method, path, options)
  @last_response.status == 204
rescue PagerDuty::NotFound
  false
end
parse_query_and_convenience_headers(options) click to toggle source
# File lib/pager_duty/connection.rb, line 94
def parse_query_and_convenience_headers(options)
  options = options.dup
  headers = options.delete(:headers) { Hash.new }
  CONVENIENCE_HEADERS.each do |h|
    if header = options.delete(h)
      headers[h] = header
    end
  end
  query = options.delete(:query)
  opts = {:query => options}
  opts[:query].merge!(query) if query && query.is_a?(Hash)
  opts[:headers] = headers unless headers.empty?

  opts
end
request(method, path, data, options = {}) click to toggle source
# File lib/pager_duty/connection.rb, line 60
def request(method, path, data, options = {})
  if data.is_a?(Hash)
    options[:query]   = data.delete(:query) || {}
    options[:headers] = data.delete(:headers) || {}
    if accept = data.delete(:accept)
      options[:headers][:accept] = accept
    end
  end
  @last_response = response = agent.call(method, URI::Parser.new.escape(path.to_s), data, options)
  response.data
end
reset_agent() click to toggle source
# File lib/pager_duty/connection.rb, line 56
def reset_agent
  @agent = nil
end
sawyer_options() click to toggle source
# File lib/pager_duty/connection.rb, line 82
def sawyer_options
  opts = {
    :links_parser => Sawyer::LinkParsers::Simple.new
  }
  conn_opts = @connection_options
  
  conn_opts[:builder] = @middleware if @middleware
  conn_opts[:proxy] = @proxy if @proxy
  opts[:faraday] = Faraday.new(conn_opts)
  opts
end