Class: ActiveRecord::Remote::Base

Inherits:
Object
  • Object
show all
Extended by:
Helpers::AssociationHelper, Helpers::RequestHelper
Includes:
ActiveModel::Validations, Helpers::SOAPHelper, Helpers::SerializationHelper, Helpers::XMLHelper
Defined in:
lib/active_record/remote/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::AssociationHelper

association_klass, has_many, parse_association_name, set_inflection

Methods included from Helpers::RequestHelper

action, base_element, operation

Methods included from Helpers::SOAPHelper

#as_soap, #soap_options

Methods included from Helpers::XMLHelper

#as_xml, #xml_options

Methods included from Helpers::SerializationHelper

#_attribute_name, #_serialize, #serializable_hash, #serialize_attribute

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



27
28
29
# File 'lib/active_record/remote/base.rb', line 27

def initialize(options = {})
  super(options.merge(custom_options))
end

Instance Attribute Details

#parsed_dataObject

Returns the value of attribute parsed_data.



21
22
23
# File 'lib/active_record/remote/base.rb', line 21

def parsed_data
  @parsed_data
end

#raw_dataObject

Returns the value of attribute raw_data.



21
22
23
# File 'lib/active_record/remote/base.rb', line 21

def raw_data
  @raw_data
end

#responseObject

Returns the value of attribute response.



21
22
23
# File 'lib/active_record/remote/base.rb', line 21

def response
  @response
end

Class Method Details

.allObject



46
47
48
# File 'lib/active_record/remote/base.rb', line 46

def self.all
  where({})
end

.api_type(type) ⇒ Object



23
24
25
# File 'lib/active_record/remote/base.rb', line 23

def self.api_type(type)
  self.api_type = type
end

.where(attrs) ⇒ Object



40
41
42
43
44
# File 'lib/active_record/remote/base.rb', line 40

def self.where(attrs)
  instance = new(attrs)
  instance.response = instance.handle_response(instance.request)
  instance.parse_records rescue []
end

Instance Method Details

#base_moduleObject



71
72
73
# File 'lib/active_record/remote/base.rb', line 71

def base_module
  self.class.to_s.split('::').first.constantize
end

#clientObject



106
107
108
# File 'lib/active_record/remote/base.rb', line 106

def client
  base_module.const_get("Client").new(self.class.action_path)
end

#custom_optionsObject



31
32
33
# File 'lib/active_record/remote/base.rb', line 31

def custom_options
  # overwrite in subclass to provide custom options to initalizer
end

#full_moduleObject



75
76
77
# File 'lib/active_record/remote/base.rb', line 75

def full_module
  self.class.to_s.split('::')[0..-2].join('::').constantize
end

#handle_response(response) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/active_record/remote/base.rb', line 98

def handle_response(response)
  base_module.const_get("Response").new(
    operation:    self.class.operation_path,
    raw_response: response,
    instance:     self
  )
end

#parse_recordsObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/active_record/remote/base.rb', line 83

def parse_records
  data = response.data.is_a?(Array) ? response.data : [response.data]
  return [] if data.compact.empty?
  data.flat_map do |data_item|
    instance = self.class.new
    read_attributes = "#{record_class}ReadAttributes"
    if full_module.const_defined?(read_attributes)
      instance.extend(read_attributes.constantize)
    end
    attrs = data_item.transform_keys! {|k| k.downcase.to_sym }
    instance.update_attributes(attrs)
    instance
  end
end

#record_classObject



79
80
81
# File 'lib/active_record/remote/base.rb', line 79

def record_class
  self.class.to_s.split('::').last
end

#requestObject



50
51
52
53
54
# File 'lib/active_record/remote/base.rb', line 50

def request
  request_body    = send("as_#{api_type}")
  client.api_type = api_type
  client.request(request_body)
end

#saveObject



35
36
37
38
# File 'lib/active_record/remote/base.rb', line 35

def save
  @response = handle_response(request)
  valid?
end

#update_attributes(attrs) ⇒ Object



56
57
58
# File 'lib/active_record/remote/base.rb', line 56

def update_attributes(attrs)
  attrs.each { |k, v| send("#{k}=", v) }
end

#valid?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
# File 'lib/active_record/remote/base.rb', line 60

def valid?
  if response.present?
    # all model validations may pass, but response
    # may have contained an error message
    response.success?
  else
    # use model validations
    super
  end
end