class WoerkClient::Client

Acts as a gateway to the woerk.net API

Public Class Methods

call(method: :get, path: '/', payload: nil) click to toggle source

Gateway for API requests

@param method [Symbol] the HTTP verb as a symbol, e.g. :get, :post, :put @param path [String] the path of the URL @param payload [Hash] the body for the request

# File lib/woerk_client/client.rb, line 13
def self.call(method: :get, path: '/', payload: nil)
  new(method, path, payload).call
end
delete(path) click to toggle source

Shorthand for DELETE requests to the API

@param path [String]

# File lib/woerk_client/client.rb, line 47
def self.delete(path)
  new(:put, path, nil).call
end
get(path) click to toggle source

Shorthand for GET requests to the API

@param path [String]

# File lib/woerk_client/client.rb, line 21
def self.get(path)
  new(:get, path, nil).call
end
new(method, path, payload) click to toggle source

@param method [Symbol] the HTTP verb as a symbol, e.g. :get, :post, :put @param path [String] the path of the URL @param payload [Hash] the body for the request

# File lib/woerk_client/client.rb, line 54
def initialize(method, path, payload)
  @method   = method
  @path     = path
  @payload  = payload
end
post(path:, payload:) click to toggle source

Shorthand for POST requests to the API

@param path [String] @param payload [Hash]

# File lib/woerk_client/client.rb, line 30
def self.post(path:, payload:)
  new(:post, path, payload).call
end
put(path:, payload:) click to toggle source

Shorthand for PUT requests to the API

@param path [String] @param payload [Hash]

# File lib/woerk_client/client.rb, line 39
def self.put(path:, payload:)
  new(:put, path, payload).call
end

Public Instance Methods

call() click to toggle source

Uses RestClient to make API requests

# File lib/woerk_client/client.rb, line 61
def call
  RestClient::Request.execute(
    method: @method,
    url: url,
    payload: @payload,
    headers: headers
  )
end

Private Instance Methods

auth_token() click to toggle source

@return [String] the authentication token from the configuration file

# File lib/woerk_client/client.rb, line 97
def auth_token
  WoerkClient.configuration.auth_token
end
base_url() click to toggle source

Base URL from the configuration

@return [String] the base URL, e.g. woerk.net/api

# File lib/woerk_client/client.rb, line 92
def base_url
  WoerkClient.configuration.api_host
end
headers() click to toggle source

All headers for every API call

@return [Hash] the headers that are submitted with every request

# File lib/woerk_client/client.rb, line 82
def headers
  {
    Accept: 'application/json; version=1',
    Authorization: auth_token
  }
end
url() click to toggle source

Puts the url together

@return [String] the final URL for the request

# File lib/woerk_client/client.rb, line 75
def url
  "#{base_url}#{@path}"
end