Sha256: 466b6afeebe06259b07c05dd761a6f58118361930936355d6d9afce033514af7

Contents?: true

Size: 1.24 KB

Versions: 15

Compression:

Stored size: 1.24 KB

Contents

module ButterCMS
  class ButterResource
    attr_reader :meta, :data

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

      if json["data"].is_a?(Hash)
        json["data"].each do |key, value|
          instance_variable_set("@#{key}", @data.send(key))
          self.class.send(:attr_reader, key)
        end
      end
    end

    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

    def self.endpoint(id = '')
      resource_path + '/' + id.to_s
    end

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

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

      self.create_collection(response)
    end

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

      self.create_object(response)
    end

    private

    def self.create_collection(response)
      ButterCollection.new(self, response)
    end

    def self.create_object(response)
      self.new(response)
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
buttercms-ruby-1.2.0 lib/buttercms/butter_resource.rb
buttercms-ruby-1.1.3 lib/buttercms/butter_resource.rb
buttercms-ruby-1.1.2 lib/buttercms/butter_resource.rb
buttercms-ruby-1.1.1 lib/buttercms/butter_resource.rb
buttercms-ruby-1.1.0 lib/buttercms/butter_resource.rb
buttercms-ruby-1.0.10 lib/buttercms/butter_resource.rb
buttercms-ruby-1.0.9 lib/buttercms/butter_resource.rb
buttercms-ruby-1.0.8 lib/buttercms/butter_resource.rb
buttercms-ruby-1.0.7 lib/buttercms/butter_resource.rb
buttercms-ruby-1.0.6 lib/buttercms/butter_resource.rb
buttercms-ruby-1.0.5 lib/buttercms/butter_resource.rb
buttercms-ruby-1.0.4 lib/buttercms/butter_resource.rb
buttercms-ruby-1.0.3 lib/buttercms/butter_resource.rb
buttercms-ruby-1.0.2 lib/buttercms/butter_resource.rb
buttercms-ruby-1.0.1 lib/buttercms/butter_resource.rb