class Plangrade::Client

Attributes

access_token[RW]
client_id[RW]
client_secret[RW]
connection_options[R]
default_headers[R]
site_url[R]

Public Class Methods

new(opts={}) click to toggle source
# File lib/plangrade/client.rb, line 19
def initialize(opts={})
  Plangrade::Configurable.keys.each do |key|
    case key
    when :headers, :connection_options
      value = Plangrade.instance_variable_get(:"@#{key}").merge(opts.fetch(key, {}))
    else
      value = opts.fetch(key, Plangrade.instance_variable_get(:"@#{key}"))
    end
    instance_variable_set(:"@#{key}", value)
  end
end

Public Instance Methods

delete(path, params={}) click to toggle source

makes a DELETE request

# File lib/plangrade/client.rb, line 47
def delete(path, params={})
  request(:delete, path, params)
end
get(path, params={}) click to toggle source

makes a GET request

# File lib/plangrade/client.rb, line 32
def get(path, params={})
  request(:get, path, params)
end
post(path, params={}) click to toggle source

makes a POST request

# File lib/plangrade/client.rb, line 42
def post(path, params={})
  request(:post, path, params)
end
put(path, params={}) click to toggle source

makes a PUT request

# File lib/plangrade/client.rb, line 37
def put(path, params={})
  request(:put, path, params)
end

Private Instance Methods

http_client() click to toggle source

returns an instance of the http adapter if none is specified, the default is Plangrade::HttpConnection @!visibility private

# File lib/plangrade/client.rb, line 56
def http_client
  @http_client ||= @http_adapter.new(@site_url, @connection_options)
end
request(method, path, params={}) click to toggle source

Makes an HTTP request using the provided parameters @raise [Plangrade::Error::Unauthorized] @param method [string] @param path [string] @param params [Hash] @return [Plangrade::ApiResponse] @!visibility private

# File lib/plangrade/client.rb, line 67
def request(method, path, params={})
  headers = @default_headers.merge({'Authorization' => "Bearer #{@access_token}"})
  result = http_client.send_request(method, path, {
    :params  => params,
    :headers => headers
  })
  result
end