Module: RESTinPeace

Defined in:
lib/rest_in_peace.rb,
lib/rest_in_peace/errors.rb,
lib/rest_in_peace/api_call.rb,
lib/rest_in_peace/active_model_api.rb,
lib/rest_in_peace/definition_proxy.rb,
lib/rest_in_peace/response_converter.rb,
lib/rest_in_peace/template_sanitizer.rb,
lib/rest_in_peace/faraday/ssl_config_creator.rb,
lib/rest_in_peace/faraday/raise_errors_middleware.rb,
lib/rest_in_peace/definition_proxy/attributes_definitions.rb,
lib/rest_in_peace/definition_proxy/resource_method_definitions.rb,
lib/rest_in_peace/definition_proxy/collection_method_definitions.rb

Defined Under Namespace

Modules: ActiveModelAPI, ClassMethods, Faraday Classes: ApiCall, DefaultError, DefinitionProxy, ResponseConverter, TemplateSanitizer

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
# File 'lib/rest_in_peace.rb', line 7

def self.included(base)
  base.send :extend, ClassMethods
  base.send :include, ActiveModel::Dirty
end

Instance Method Details

#apiObject



12
13
14
# File 'lib/rest_in_peace.rb', line 12

def api
  self.class.api
end

#clear_changesObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rest_in_peace.rb', line 37

def clear_changes
  case
  when respond_to?(:clear_changes_information) # ActiveModel >= 4.2
    clear_changes_information
  when respond_to?(:reset_changes) # ActiveModel >= 4.0 && <= 4.1
    reset_changes
  else # ActiveModel <= 3.2
    return unless @changed_attributes
    @changed_attributes.clear
  end
end

#force_attributes_from_hash(attributes) ⇒ Object



75
76
77
78
# File 'lib/rest_in_peace.rb', line 75

def force_attributes_from_hash(attributes)
  set_attributes attributes
  clear_changes
end

#hash_representation_of_object(object) ⇒ Object



80
81
82
83
84
# File 'lib/rest_in_peace.rb', line 80

def hash_representation_of_object(object)
  return object.payload if object.respond_to?(:payload)
  return object.map { |element| hash_representation_of_object(element) } if object.is_a?(Array)
  object
end

#initialize(attributes = {}) ⇒ Object



16
17
18
# File 'lib/rest_in_peace.rb', line 16

def initialize(attributes = {})
  set_attributes attributes
end

#payload(changes_only = true) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rest_in_peace.rb', line 20

def payload(changes_only = true)
  hash_representation = { id: id }
  if changes_only
    changed.each do |key|
      value = send(key)
      hash_representation[key.to_sym] = hash_representation_of_object(value)
    end
  else
    hash_representation = to_h
  end
  if self.class.rip_namespace
    { id: id, self.class.rip_namespace => hash_representation }
  else
    hash_representation
  end
end

#set_attributes(attributes) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/rest_in_peace.rb', line 64

def set_attributes(attributes)
  attributes.each do |key, value|
    next unless respond_to?(key)
    if respond_to?("#{key}=")
      send("#{key}=", value)
    else
      instance_variable_set("@#{key}", value)
    end
  end
end

#to_hObject



56
57
58
59
60
61
62
# File 'lib/rest_in_peace.rb', line 56

def to_h
  hash_representation = {}
  self.class.rip_attributes.values.flatten.each do |attr|
    hash_representation[attr] = send(attr)
  end
  hash_representation
end

#update_attributes(attributes) ⇒ Object



49
50
51
52
53
54
# File 'lib/rest_in_peace.rb', line 49

def update_attributes(attributes)
  attributes.each do |key, value|
    next unless respond_to?("#{key}=")
    send("#{key}=", value)
  end
end