Class: Craftar::Base
- Inherits:
-
Object
- Object
- Craftar::Base
- Includes:
- HTTMultiParty
- Defined in:
- lib/craftar/base.rb
Direct Known Subclasses
App, Collection, CollectionBundle, Image, Item, Media, SdkVersion, Token
Class Method Summary collapse
-
.basic_options(opts = {}) ⇒ Object
the basic query option.
-
.create(options) ⇒ Object
create a new object options are the different specific parameters of this object.
-
.find(uuid) ⇒ Object
Find an object thanks to its uuid.
-
.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.
- .parse_response(httparty) ⇒ Object
Instance Method Summary collapse
-
#destroy ⇒ Object
destroy an object.
Class Method Details
.basic_options(opts = {}) ⇒ Object
the basic query option
53 54 55 |
# File 'lib/craftar/base.rb', line 53 def self.(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() obj = self.new() 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}", )) 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}/", (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
#destroy ⇒ Object
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.) end |