Class: Craftar::Base

Inherits:
Object
  • Object
show all
Includes:
HTTMultiParty
Defined in:
lib/craftar/base.rb

Direct Known Subclasses

App, Collection, CollectionBundle, Image, Item, Media, SdkVersion, Token

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.basic_options(opts = {}) ⇒ Object

the basic query option



53
54
55
# File 'lib/craftar/base.rb', line 53

def self.basic_options(opts = {})
  { query: { api_key: Craftar.api_key }.merge(opts) }
end

.create(options) ⇒ Object

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



40
41
42
43
# File 'lib/craftar/base.rb', line 40

def self.create(options)
  obj = self.new(options)
  obj.save
end

.find(uuid) ⇒ Object

Find an object thanks to its uuid



31
32
33
34
35
# 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 = {}) ⇒ Object

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



19
20
21
22
23
24
25
26
27
28
# 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

.parse_response(httparty) ⇒ Object



90
91
92
93
# 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

Instance Method Details

#destroyObject

destroy an object



46
47
48
# File 'lib/craftar/base.rb', line 46

def destroy
  self.class.delete("/#{self.class.craftar_name}/#{uuid}/", self.class.basic_options)
end