class Cangaroo::Webhook::Client

Attributes

connection[RW]
path[RW]

Public Class Methods

new(connection, path) click to toggle source
# File lib/cangaroo/webhook/client.rb, line 10
def initialize(connection, path)
  @connection = connection
  @path = path
end

Public Instance Methods

post(payload, request_id, parameters) click to toggle source
# File lib/cangaroo/webhook/client.rb, line 15
def post(payload, request_id, parameters)
  request_body = body(payload, request_id, parameters).to_json

  request_options = {
    headers: headers,
    body: request_body
  }

  if Rails.configuration.cangaroo.basic_auth
    request_options[:basic_auth] = {
      username: connection.key,
      password: connection.token
    }
  end

  if timeout = Rails.configuration.cangaroo.client_timeout
    request_options[:timeout] = timeout
  end

  req = self.class.post(url, request_options)

  sanitized_response = sanitize_response(req)

  fail Cangaroo::Webhook::Error, sanitized_response unless %w(200 201 202 204).include?(req.response.code)
  sanitized_response
end

Private Instance Methods

body(payload, request_id, parameters) click to toggle source
# File lib/cangaroo/webhook/client.rb, line 58
def body(payload, request_id, parameters)
  { request_id: request_id,
    parameters: connection.parameters.deep_merge(parameters) }.merge(payload)
end
headers() click to toggle source
# File lib/cangaroo/webhook/client.rb, line 50
def headers
  {
    'X_HUB_TOKEN' => connection.token || '',
    'Content-Type' => 'application/json',
    'Accept' => 'application/json'
  }
end
sanitize_response(request) click to toggle source
# File lib/cangaroo/webhook/client.rb, line 63
def sanitize_response(request)
  if %w(200 201 202).include?(request.response.code)
    request.parsed_response
  elsif request.response.code == '204'
    ''
  else
    begin
      (request.parsed_response['summary'] || request.response.body)
    rescue
      request.response.body
    end
  end
end
url() click to toggle source
# File lib/cangaroo/webhook/client.rb, line 44
def url
  URI.parse(
    HTTParty.normalize_base_uri(connection.url)
  ).merge(path.to_s).to_s
end