class Givepulse::Connection

Constants

API_ROOT

Attributes

authorization_token[W]

Public Class Methods

new() click to toggle source
# File lib/givepulse/connection.rb, line 12
def initialize
    @client = Net::HTTP.new(API_ROOT.host, API_ROOT.port)
    @client.use_ssl = true
    @custom_headers = nil
end

Public Instance Methods

delete(path, options = nil) click to toggle source
# File lib/givepulse/connection.rb, line 39
def delete(path, options = nil)
    query_string = options_to_query(options)
    full_path = "#{path}?#{query_string}"
    response = @client.delete(full_path, generate_headers(@custom_headers))
    JSON.parse(response.body)
end
get(path, options = nil) click to toggle source
# File lib/givepulse/connection.rb, line 18
def get(path, options = nil)
    query_string = options_to_query(options)
    full_path = "#{path}?#{query_string}"
    response = @client.get(full_path, generate_headers(@custom_headers))
    JSON.parse(response.body)
end
post(path, data, options = nil) click to toggle source
# File lib/givepulse/connection.rb, line 25
def post(path, data, options = nil)
    query_string = options_to_query(options)
    full_path = "#{path}?#{query_string}"
    response = @client.post(full_path, data, generate_headers(@custom_headers))
    JSON.parse(response.body)
end
put(path, data, options = nil) click to toggle source
# File lib/givepulse/connection.rb, line 32
def put(path, data, options = nil)
    query_string = options_to_query(options)
    full_path = "#{path}?#{query_string}"
    response = @client.put(full_path, data, generate_headers(@custom_headers))
    JSON.parse(response.body)
end
with_headers(headers) { |self| ... } click to toggle source
# File lib/givepulse/connection.rb, line 46
def with_headers(headers)
    @custom_headers = headers
    yield(self)
    @custom_headers = nil
end

Private Instance Methods

generate_headers(custom_headers = nil) click to toggle source
# File lib/givepulse/connection.rb, line 54
def generate_headers(custom_headers = nil)
    headers = custom_headers.nil? ? {} : custom_headers.clone
    headers['Authorization'] = "Bearer #{@authorization_token}" if @authorization_token
    headers
end
options_to_query(options) click to toggle source
# File lib/givepulse/connection.rb, line 60
def options_to_query(options)
    return if options.nil?
    query_parameters = []
    options.each do |key, value|
        query_parameters.push("#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}")
    end
    query_parameters.join('&')
end