class BooticClient::Client

Constants

JSON_MIME
USER_AGENT

Attributes

options[R]

Public Class Methods

new(options = {}, &block) click to toggle source
# File lib/bootic_client/client.rb, line 18
def initialize(options = {}, &block)
  @options = {
    logging: false,
    faraday_adapter: [:net_http_persistent],
    user_agent: USER_AGENT
  }.merge(options.dup)

  @options[:cache_store] = @options[:cache_store] || Faraday::HttpCache::MemoryStore.new

  conn &block if block_given?
end

Public Instance Methods

delete(href, _ = {}, headers = {}) click to toggle source
# File lib/bootic_client/client.rb, line 58
def delete(href, _ = {}, headers = {})
  validated_request!(:delete, href) do |req|
    req.headers.update headers
  end
end
get(href, query = {}, headers = {}) click to toggle source
# File lib/bootic_client/client.rb, line 30
def get(href, query = {}, headers = {})
  validated_request!(:get, href) do |req|
    req.headers.update headers
    req.params.update(query)
  end
end
patch(href, payload = {}, headers = {}) click to toggle source
# File lib/bootic_client/client.rb, line 51
def patch(href, payload = {}, headers = {})
  validated_request!(:patch, href) do |req|
    req.headers.update headers
    req.body = JSON.dump(sanitized(payload))
  end
end
post(href, payload = {}, headers = {}) click to toggle source
# File lib/bootic_client/client.rb, line 37
def post(href, payload = {}, headers = {})
  validated_request!(:post, href) do |req|
    req.headers.update headers
    req.body = JSON.dump(sanitized(payload))
  end
end
put(href, payload = {}, headers = {}) click to toggle source
# File lib/bootic_client/client.rb, line 44
def put(href, payload = {}, headers = {})
  validated_request!(:put, href) do |req|
    req.headers.update headers
    req.body = JSON.dump(sanitized(payload))
  end
end

Private Instance Methods

conn() { |f| ... } click to toggle source
# File lib/bootic_client/client.rb, line 84
def conn(&block)
  @conn ||= Faraday.new do |f|
    cache_options = {serializer: SafeCacheSerializer, shared_cache: false, store: options[:cache_store]}
    cache_options[:logger] = options[:logger] if options[:logging]

    f.use :http_cache, **cache_options
    f.response :logger, options[:logger] if options[:logging]
    yield f if block_given?
    f.adapter *Array(options[:faraday_adapter])
  end
end
raise_if_invalid!(resp) click to toggle source
# File lib/bootic_client/client.rb, line 115
def raise_if_invalid!(resp)
  raise ServerError, "Server Error" if resp.status > 499
  raise NotFoundError, "Not Found" if resp.status == 404
  raise UnauthorizedError, "Unauthorized request" if resp.status == 401
  raise AccessForbiddenError, "Access Forbidden" if resp.status == 403
end
request_headers() click to toggle source
# File lib/bootic_client/client.rb, line 96
def request_headers
  {
    'User-Agent' => options[:user_agent],
    'Accept' => JSON_MIME,
    'Content-Type' => JSON_MIME
  }
end
sanitized(payload) click to toggle source
# File lib/bootic_client/client.rb, line 122
def sanitized(payload)
  return payload unless payload.kind_of?(Hash)
  payload.each_with_object({}) do |(k, v), memo|
    memo[k] = if v.kind_of?(Hash)
      sanitized v
    elsif v.respond_to?(:read)
      Base64.encode64 v.read
    else
      v
    end
  end
end
validated_request!(verb, href) { |req| ... } click to toggle source
# File lib/bootic_client/client.rb, line 104
def validated_request!(verb, href, &block)
  resp = conn.send(verb) do |req|
    req.url href
    req.headers.update request_headers
    yield req if block_given?
  end

  raise_if_invalid! resp
  resp
end