Class: ButterCMS::ButterResource

Inherits:
Object
  • Object
show all
Defined in:
lib/buttercms/butter_resource.rb

Direct Known Subclasses

Author, Category, Content, Feed, Page, Post, Tag

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ ButterResource

Returns a new instance of ButterResource.



5
6
7
8
9
10
11
# File 'lib/buttercms/butter_resource.rb', line 5

def initialize(json)
  @json = json
  @data = HashToObject.convert(json["data"])
  @meta = HashToObject.convert(json["meta"]) if json["meta"]

  define_attribute_methods(@data)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/buttercms/butter_resource.rb', line 3

def data
  @data
end

#metaObject (readonly)

Returns the value of attribute meta.



3
4
5
# File 'lib/buttercms/butter_resource.rb', line 3

def meta
  @meta
end

Class Method Details

.all(options = {}) ⇒ Object



46
47
48
49
50
# File 'lib/buttercms/butter_resource.rb', line 46

def self.all(options = {})
  response = ButterCMS.request(self.endpoint, options)

  self.create_collection(response)
end

.create(options = {}) ⇒ Object



58
59
60
61
62
63
# File 'lib/buttercms/butter_resource.rb', line 58

def self.create(options = {})
  options[:method] = 'Post'
  response = ButterCMS.write_request(self.endpoint, options)

  self.create_object(response)
end

.endpoint(id = nil) ⇒ Object



30
31
32
33
34
# File 'lib/buttercms/butter_resource.rb', line 30

def self.endpoint(id = nil)
  # Append trailing slash when id is added to path because
  # API expects all endpoints to include trailing slashes
  resource_path + (id ? "#{id}/" : '')
end

.find(id, options = {}) ⇒ Object



52
53
54
55
56
# File 'lib/buttercms/butter_resource.rb', line 52

def self.find(id, options = {})
  response = ButterCMS.request(self.endpoint(id), options)

  self.create_object(response)
end

.patch_endpoint(id) ⇒ Object



36
37
38
39
40
# File 'lib/buttercms/butter_resource.rb', line 36

def self.patch_endpoint(id)
  # Append trailing slash when id is added to path because
  # API expects all endpoints to include trailing slashes
  resource_path + "*/#{id}/"
end

.resource_pathObject



42
43
44
# File 'lib/buttercms/butter_resource.rb', line 42

def self.resource_path
  raise "resource_path must be set"
end

.update(id, options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/buttercms/butter_resource.rb', line 65

def self.update(id, options = {})
  options[:method] = 'Patch'
  _endpoint = if resource_path.include?("/pages/")
    self.patch_endpoint(id)
  else
    self.endpoint(id)
  end
  response = ButterCMS.write_request(_endpoint, options)

  self.create_object(response)
end

Instance Method Details

#inspectObject



25
26
27
28
# File 'lib/buttercms/butter_resource.rb', line 25

def inspect
  id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
  "#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + JSON.pretty_generate(@json)
end

#marshal_dumpObject



13
14
15
# File 'lib/buttercms/butter_resource.rb', line 13

def marshal_dump
  { json: @json, data: @data, meta: @meta }
end

#marshal_load(dump) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/buttercms/butter_resource.rb', line 17

def marshal_load(dump)
  @json = dump[:json]
  @data = dump[:data]
  @meta = dump[:meta]

  define_attribute_methods(@data)
end