Class: ActiveRecord::Remote::Base
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
association_klass, has_many, parse_association_name, set_inflection
action, base_element, operation
#as_soap, #soap_options
#as_xml, #xml_options
#_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_data ⇒ Object
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_data ⇒ Object
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
|
#response ⇒ Object
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
.all ⇒ Object
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_module ⇒ Object
71
72
73
|
# File 'lib/active_record/remote/base.rb', line 71
def base_module
self.class.to_s.split('::').first.constantize
end
|
#client ⇒ Object
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_options ⇒ Object
31
32
33
|
# File 'lib/active_record/remote/base.rb', line 31
def custom_options
end
|
#full_module ⇒ Object
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_records ⇒ Object
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_class ⇒ Object
79
80
81
|
# File 'lib/active_record/remote/base.rb', line 79
def record_class
self.class.to_s.split('::').last
end
|
#request ⇒ Object
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
|
#save ⇒ Object
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
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/active_record/remote/base.rb', line 60
def valid?
if response.present?
response.success?
else
super
end
end
|