class Craftar::Base

Public Class Methods

create(options) click to toggle source

create a new object options are the different specific parameters of this object. See readme

# File lib/craftar/base.rb, line 40
def self.create(options)
  obj = self.new(options)
  obj.save
end
find(uuid) click to toggle source

Find an object thanks to its uuid

# File lib/craftar/base.rb, line 31
def self.find(uuid)
  response = parse_response(get("/#{craftar_name}/#{uuid}", basic_options))
  return if response == {}
  self.new(response.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo})
end
list(opts = {}) click to toggle source

options: ‘limit’, ‘offset’and any object specific option (see README) To manage the pagination, all responses have a ‘meta’ field The “meta” fields are:

  • total_count: total number of objects in the underlying object list

  • limit: how many objects are returned per page. By default it’s 20.

  • offset: how many initial objects are skipped in this response

  • previous: if available, provides a URI to the previous page

  • next: if available, provides a URI to the next page

Fetching a page To navigate through pages, use the ‘limit‘ and ‘offset‘ parameters. The objects of the current page are in the ‘objects’ fields

# File lib/craftar/base.rb, line 19
def self.list(opts = {})
  response = parse_response(get("/#{craftar_name}/", basic_options(opts)))
  raise (response ['error']['message']) if response['error']
  objects = []
  response['objects'].each do |object|
    objects << self.new(object.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo})
  end
  response[:objects] =  objects
  response.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
end

Private Class Methods

basic_options(opts = {}) click to toggle source

the basic query option

# File lib/craftar/base.rb, line 53
def self.basic_options(opts = {})
  { query: { api_key: Craftar.api_key }.merge(opts) }
end
parse_response(httparty) click to toggle source
# File lib/craftar/base.rb, line 90
def self.parse_response(httparty)
  response_body = httparty.response.body
  response_body.to_s != '' ? JSON.parse(response_body) : {}
end

Public Instance Methods

destroy() click to toggle source

destroy an object

# File lib/craftar/base.rb, line 46
def destroy
  self.class.delete("/#{self.class.craftar_name}/#{uuid}/", self.class.basic_options)
end

Private Instance Methods

call(method_name, opts) click to toggle source
# File lib/craftar/base.rb, line 57
def call(method_name, opts)
  response = self.class.send(method_name,
                  "/#{self.class.craftar_name}/",
                  query: { api_key: Craftar.api_key }.merge!(opts)
  )
  if response['error']
    error_message = self.class.craftar_name + ': ' + response['error']['message']
    #error_message += response['error']['details'] if response['error']['details']
    raise error_message
  end
  response
end
json_call(method_name, opts) click to toggle source
# File lib/craftar/base.rb, line 70
def json_call(method_name, opts)
  uid = opts.delete(:uuid)
  path = "/#{self.class.craftar_name}/"
  path += "#{uid}/" if uid
  path += "?api_key=#{Craftar.api_key}"
  response = self.class.parse_response( HTTParty.send(
    method_name,
    self.class.base_uri + path,
    body: opts.to_json,
    headers: { 'Content-Type' => 'application/json' }
  ))
  if response['error']

    error_message = self.class.craftar_name + ': ' + response['error']['message']
    error_message += response['error']['details'].to_s if response['error']['details']
    raise error_message
  end
  response
end
prepare_file_from_url(url) click to toggle source
# File lib/craftar/base.rb, line 95
def prepare_file_from_url(url)
  require 'open-uri'
  extname = File.extname(url)
  basename = File.basename(url, extname)

  file = Tempfile.new([basename, extname])
  file.binmode

  open(URI.parse(url)) do |data|
    file.write data.read
  end
  file.rewind
  file
end