class WCC::API::RestClient

Constants

ADAPTERS

Attributes

params[R]
resources[R]
api_url[R]

Public Class Methods

load_adapter(adapter) click to toggle source
# File lib/wcc/api/rest_client.rb, line 80
def self.load_adapter(adapter)
  case adapter
  when nil
    ADAPTERS.each do |a, spec|
      begin
        gem(*spec)
        return load_adapter(a)
      rescue Gem::LoadError
        next
      end
    end
    raise ArgumentError, 'Unable to load adapter!  Please install one of '\
      "#{ADAPTERS.values.map(&:join).join(',')}"
  when :faraday
    require 'faraday'
    ::Faraday.new do |faraday|
      faraday.response :logger, (Rails.logger if defined?(Rails)), { headers: false, bodies: false }
      faraday.adapter :net_http
    end
  when :typhoeus
    require_relative 'rest_client/typhoeus_adapter'
    TyphoeusAdapter.new
  else
    unless adapter.respond_to?(:get)
      raise ArgumentError, "Adapter #{adapter} is not invokeable!  Please "\
        "pass use one of #{ADAPTERS.keys} or create a Faraday-compatible adapter"
    end
    adapter
  end
end
new(api_url:, headers: nil, **options) click to toggle source
# File lib/wcc/api/rest_client.rb, line 22
def initialize(api_url:, headers: nil, **options)
  # normalizes a URL to have a slash on the end
  @api_url = api_url.gsub(/\/+$/, '') + '/'

  @adapter = RestClient.load_adapter(options[:adapter])

  @options = options
  @query_defaults = {}
  @headers = {
    'Accept' => 'application/json'
  }.merge(headers || {}).freeze
  @response_class = options[:response_class] || DefaultResponse
end
rest_client(&block) click to toggle source
# File lib/wcc/api/rest_client.rb, line 15
def rest_client(&block)
  builder = Builder.new(self)
  builder.instance_exec(&block)
  builder.apply
end

Public Instance Methods

delete(path) click to toggle source
# File lib/wcc/api/rest_client.rb, line 67
def delete(path)
  url = URI.join(@api_url, path)

  @response_class.new(self,
    { url: url },
    delete_http(url))
end
get(path, query = {}) click to toggle source

performs an HTTP GET request to the specified path within the configured space and environment. Query parameters are merged with the defaults and appended to the request.

# File lib/wcc/api/rest_client.rb, line 39
def get(path, query = {})
  url = URI.join(@api_url, path)

  @response_class.new(self,
    { url: url, query: query },
    get_http(url, query))
end
post(path, body = {}) click to toggle source
# File lib/wcc/api/rest_client.rb, line 47
def post(path, body = {})
  url = URI.join(@api_url, path)

  @response_class.new(self,
    { url: url },
    post_http(url,
      body.to_json,
      headers: { 'Content-Type': 'application/json' }))
end
put(path, body = {}) click to toggle source
# File lib/wcc/api/rest_client.rb, line 57
def put(path, body = {})
  url = URI.join(@api_url, path)

  @response_class.new(self,
    { url: url },
    put_http(url,
      body.to_json,
      headers: { 'Content-Type': 'application/json' }))
end

Private Instance Methods

delete_http(url, headers: {}) click to toggle source
# File lib/wcc/api/rest_client.rb, line 143
def delete_http(url, headers: {})
  headers = @headers.merge(headers || {})

  resp = @adapter.delete(url, {}, headers)

  resp = get_http(resp.headers['location'], nil, headers) if [301, 302, 307].include?(resp.status) && !@options[:no_follow_redirects]
  resp
end
get_http(url, query, headers = {}) click to toggle source
# File lib/wcc/api/rest_client.rb, line 113
def get_http(url, query, headers = {})
  headers = @headers.merge(headers || {})

  q = @query_defaults.dup
  q = q.merge(query) if query

  resp = @adapter.get(url, q, headers)

  resp = get_http(resp.headers['location'], nil, headers) if [301, 302, 307].include?(resp.status) && !@options[:no_follow_redirects]
  resp
end
post_http(url, body, headers: {}) click to toggle source
# File lib/wcc/api/rest_client.rb, line 125
def post_http(url, body, headers: {})
  headers = @headers.merge(headers || {})

  resp = @adapter.post(url, body, headers)

  resp = get_http(resp.headers['location'], nil, headers) if [301, 302, 307].include?(resp.status) && !@options[:no_follow_redirects]
  resp
end
put_http(url, body, headers: {}) click to toggle source
# File lib/wcc/api/rest_client.rb, line 134
def put_http(url, body, headers: {})
  headers = @headers.merge(headers || {})

  resp = @adapter.put(url, body, headers)

  resp = get_http(resp.headers['location'], nil, headers) if [301, 302, 307].include?(resp.status) && !@options[:no_follow_redirects]
  resp
end