Module: Kippt::Resource

Included in:
Clip, Comment, List, User
Defined in:
lib/kippt/resource.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/kippt/resource.rb', line 4

def self.included(base)
  base.instance_eval do
    extend Forwardable
    attr_reader :attributes, :errors

    def_delegators "self.class", :writable_attribute_names, :attribute_names, :embedded_attribute_names
  end

  base.extend(ClassMethods)
end

Instance Method Details

#destroyObject



118
119
120
# File 'lib/kippt/resource.rb', line 118

def destroy
  collection_resource.destroy_resource(self)
end

#initialize(attributes = {}, client = nil) ⇒ Object



112
113
114
115
116
# File 'lib/kippt/resource.rb', line 112

def initialize(attributes = {}, client = nil)
  @attributes = OpenStruct.new(attributes)
  @errors     = []
  @client     = client
end

#saveObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/kippt/resource.rb', line 135

def save
  @errors = []
  response = collection_resource.save_resource(self)
  if response[:error_message]
    errors << response[:error_message]
  else
    if response[:resource]
      updated_attributes = response[:resource].select do |key, _|
        attribute_names.include?(key.to_sym)
      end
      @attributes = OpenStruct.new(updated_attributes)
    end
  end
  response[:success]
end

#writable_attributes_hashObject



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/kippt/resource.rb', line 122

def writable_attributes_hash
  writable_attribute_names.inject({}) do |parameters, attribute_name|
    value = self.send(attribute_name)
    if embedded_attribute_names.include?(attribute_name) && !value.is_a?(String)
      value = value.resource_uri
    end
    unless value.nil?
      parameters[attribute_name] = value
    end
    parameters
  end
end