class Puree::REST::Base

Requests for a resource

Public Class Methods

new(config) click to toggle source

@param config [Hash] @option config [String] :url URL of the Pure host @option config [String] :username Username of the Pure host account @option config [String] :password Password of the Pure host account @option config [String] :api_key API key of the Pure host account @option config [Float] :http_read_timeout Read timeout in seconds @option config [Float] :http_write_timeout Write timeout in seconds @option config [Float] :http_connection_timeout Connection timeout in seconds

# File lib/puree/rest/base.rb, line 19
def initialize(config)
  config = http_defaults.merge config
  @http_client = HTTP::Client.new
  if config[:username] || config[:password]
    options = {}
    options[:user] = config[:username]
    options[:pass] = config[:password]
    @http_client = @http_client.basic_auth options
  end
  @http_client = @http_client.headers(api_key_header(config[:api_key]))
  @http_client = @http_client.timeout read: config[:http_read_timeout],
                                      write: config[:http_write_timeout],
                                      connect: config[:http_connection_timeout]
  @url = config[:url]
end

Public Instance Methods

all(params: {}, accept: :xml) click to toggle source

@param params [Hash] GET parameters for all records @param accept [Symbol] @return [HTTP::Response]

# File lib/puree/rest/base.rb, line 46
def all(params: {}, accept: :xml)
  get_request_collection params: params,
                         accept: accept
end
all_complex(params: {}, accept: :xml) click to toggle source

@param params [Hash] Combined GET and POST parameters for all records @param accept [Symbol] @return [HTTP::Response]

# File lib/puree/rest/base.rb, line 38
def all_complex(params: {}, accept: :xml)
  post_request_collection params: params,
                          accept: accept
end
find(id:, params: {}, accept: :xml) click to toggle source

@param id [String] @param params [Hash] @param accept [Symbol] @return [HTTP::Response]

# File lib/puree/rest/base.rb, line 55
def find(id:, params: {}, accept: :xml)
  get_request_singleton id: id,
                    params: params,
                    accept: accept
end
orderings(accept: :xml) click to toggle source

@param accept [Symbol] @return (see Puree::REST::Base#all)

# File lib/puree/rest/base.rb, line 63
def orderings(accept: :xml)
  get_request_meta meta_type: 'orderings',
               accept: accept
end
renderings(accept: :xml) click to toggle source

@param accept [Symbol] @return (see Puree::REST::Base#all)

# File lib/puree/rest/base.rb, line 70
def renderings(accept: :xml)
  get_request_meta meta_type: 'renderings',
               accept: accept
end

Private Instance Methods

accept_header(accept) click to toggle source
# File lib/puree/rest/base.rb, line 85
def accept_header(accept)
  case accept
    when :json
      return { 'Accept' => 'application/json' }
    when :xml
      return { 'Accept' => 'application/xml' }
  end
end
api_key_header(key) click to toggle source
# File lib/puree/rest/base.rb, line 103
def api_key_header(key)
  msg = 'API key incomplete in configuration'
  raise msg if !key
  { 'api-key' => key }
end
content_type_header(content_type) click to toggle source
# File lib/puree/rest/base.rb, line 94
def content_type_header(content_type)
  case content_type
  when :json
    return { 'Content-Type' => 'application/json' }
  when :xml
    return { 'Content-Type' => 'application/xml' }
  end
end
get_request_collection(params: {}, accept: :xml) click to toggle source

@return (see Puree::REST::Base#all)

# File lib/puree/rest/base.rb, line 137
def get_request_collection(params: {}, accept: :xml)
  @http_client = @http_client.headers(accept_header(accept))
  @http_client.get url_collection, params: params
end
get_request_collection_subcollection(subcollection:, params: {}, accept: :xml) click to toggle source

@return (see Puree::REST::Base#all)

# File lib/puree/rest/base.rb, line 143
def get_request_collection_subcollection(subcollection:, params: {}, accept: :xml)
  @http_client = @http_client.headers(accept_header(accept))
  @http_client.get url_collection_subcollection(subcollection), params: params
end
get_request_meta(meta_type:, accept: :xml) click to toggle source

@return (see Puree::REST::Base#all)

# File lib/puree/rest/base.rb, line 161
def get_request_meta(meta_type:, accept: :xml)
  @http_client = @http_client.headers(accept_header(accept))
  @http_client.get meta(meta_type)
end
get_request_singleton(id:, params: {}, accept: :xml) click to toggle source

@return (see Puree::REST::Base#all)

# File lib/puree/rest/base.rb, line 149
def get_request_singleton(id:, params: {}, accept: :xml)
  @http_client = @http_client.headers(accept_header(accept))
  @http_client.get url_singleton(id), params: params
end
get_request_singleton_subcollection(id:, subcollection:, params: {}, accept: :xml) click to toggle source

@return (see Puree::REST::Base#all)

# File lib/puree/rest/base.rb, line 155
def get_request_singleton_subcollection(id:, subcollection:, params: {}, accept: :xml)
  @http_client = @http_client.headers(accept_header(accept))
  @http_client.get url_singleton_subcollection(id, subcollection), params: params
end
http_defaults() click to toggle source
# File lib/puree/rest/base.rb, line 77
def http_defaults
  {
    http_read_timeout: 10,
    http_write_timeout: 10,
    http_connection_timeout: 10
  }
end
meta(type) click to toggle source
# File lib/puree/rest/base.rb, line 125
def meta(type)
  File.join "#{url_collection}-meta", type
end
post_request_collection(params: {}, accept: :xml) click to toggle source

@return (see Puree::REST::Base#all_complex)

# File lib/puree/rest/base.rb, line 130
def post_request_collection(params: {}, accept: :xml)
  @http_client = @http_client.headers(accept_header(accept))
  @http_client = @http_client.headers(content_type_header(:json))
  @http_client.post url_collection, json: params
end
url_collection() click to toggle source
# File lib/puree/rest/base.rb, line 109
def url_collection
  File.join @url, collection
end
url_collection_subcollection(subcollection) click to toggle source
# File lib/puree/rest/base.rb, line 113
def url_collection_subcollection(subcollection)
  File.join url_collection, subcollection
end
url_singleton(id) click to toggle source
# File lib/puree/rest/base.rb, line 117
def url_singleton(id)
  File.join url_collection, id
end
url_singleton_subcollection(id, subcollection) click to toggle source
# File lib/puree/rest/base.rb, line 121
def url_singleton_subcollection(id, subcollection)
  File.join url_singleton(id), subcollection
end