class ButterCMS::ButterResource

Attributes

data[R]
meta[R]

Public Class Methods

all(options = {}) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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
new(json) click to toggle source
# 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
patch_endpoint(id) click to toggle source
# 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_path() click to toggle source
# File lib/buttercms/butter_resource.rb, line 42
def self.resource_path
  raise "resource_path must be set"
end
update(id, options = {}) click to toggle source
# 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

Private Class Methods

create_collection(response) click to toggle source
# File lib/buttercms/butter_resource.rb, line 79
def self.create_collection(response)
  ButterCollection.new(self, response)
end
create_object(response) click to toggle source
# File lib/buttercms/butter_resource.rb, line 83
def self.create_object(response)
  self.new(response)
end

Public Instance Methods

inspect() click to toggle source
# 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_dump() click to toggle source
# File lib/buttercms/butter_resource.rb, line 13
def marshal_dump
  { json: @json, data: @data, meta: @meta }
end
marshal_load(dump) click to toggle source
# 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

Private Instance Methods

define_attribute_methods(methods) click to toggle source
# File lib/buttercms/butter_resource.rb, line 87
def define_attribute_methods(methods)
  return unless methods.respond_to?(:each_pair)

  methods.each_pair do |key, value|
    instance_variable_set("@#{key}", value)
    self.class.send(:attr_reader, key)
  end
end