Class: OData::Operation
- Inherits:
-
Object
show all
- Defined in:
- lib/odata/operation.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Operation.
8
9
10
|
# File 'lib/odata/operation.rb', line 8
def initialize(ar)
@ar = ar
end
|
Instance Attribute Details
#ar ⇒ Object
Returns the value of attribute ar.
6
7
8
|
# File 'lib/odata/operation.rb', line 6
def ar
@ar
end
|
Instance Method Details
#base_url ⇒ Object
12
13
14
|
# File 'lib/odata/operation.rb', line 12
def base_url
OdataConfig.odata_config[Rails.env]['data_url']
end
|
#check_response_errors(response) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/odata/operation.rb', line 16
def check_response_errors(response)
if response.code.to_i >= 400
error_message = begin
JSON.parse(response.body)['error']['message']
rescue
"An error occurred"
end
@ar.errors[:base] << "#{error_message} [http code #{response.code}]"
end
end
|
#entity_name ⇒ Object
28
29
30
|
# File 'lib/odata/operation.rb', line 28
def entity_name
@ar.class.odata_table_reference || table_pluralize(@ar.class.table_name).downcase
end
|
#handle_operation_response(response) ⇒ Object
32
33
34
|
# File 'lib/odata/operation.rb', line 32
def handle_operation_response(response)
raise NotImplementedError
end
|
#many_to_many_class_name(index) ⇒ Object
44
45
46
|
# File 'lib/odata/operation.rb', line 44
def many_to_many_class_name(index)
@ar.class.many_to_many_associated_tables[index].name.demodulize.underscore
end
|
#many_to_many_entity_id(index) ⇒ Object
48
49
50
|
# File 'lib/odata/operation.rb', line 48
def many_to_many_entity_id(index)
@ar.send(many_to_many_class_name(index)).id
end
|
#many_to_many_entity_name(index) ⇒ Object
40
41
42
|
# File 'lib/odata/operation.rb', line 40
def many_to_many_entity_name(index)
@ar.class.many_to_many_associated_tables[index].odata_table_reference || table_pluralize(@ar.class.many_to_many_associated_tables[index].table_name).downcase
end
|
#many_to_many_foreign_key(index) ⇒ Object
52
53
54
|
# File 'lib/odata/operation.rb', line 52
def many_to_many_foreign_key(index)
@ar.class.belongs_to_field_by_name(many_to_many_class_name(index)).foreign_key
end
|
#many_to_many_table? ⇒ Boolean
36
37
38
|
# File 'lib/odata/operation.rb', line 36
def many_to_many_table?
@ar.class.many_to_many_associated_tables.present?
end
|
#many_to_many_table_name ⇒ Object
56
57
58
|
# File 'lib/odata/operation.rb', line 56
def many_to_many_table_name
@ar.class.odata_table_reference || @ar.class.table_name.downcase
end
|
#operation_body ⇒ Object
60
61
62
|
# File 'lib/odata/operation.rb', line 60
def operation_body
nil
end
|
#operation_callback_name ⇒ Object
64
65
66
|
# File 'lib/odata/operation.rb', line 64
def operation_callback_name
raise NotImplementedError
end
|
68
69
70
71
72
73
74
75
|
# File 'lib/odata/operation.rb', line 68
def
{
'Accept' => 'application/json',
'Content-Type' => 'application/json; charset=utf-8',
'OData-MaxVersion' => '4.0',
'OData-Version' => '4.0'
}
end
|
#operation_method ⇒ Object
77
78
79
|
# File 'lib/odata/operation.rb', line 77
def operation_method
raise NotImplementedError
end
|
#operation_password ⇒ Object
81
82
83
|
# File 'lib/odata/operation.rb', line 81
def operation_password
OdataConfig.odata_config[Rails.env]['password']
end
|
#operation_url ⇒ Object
85
86
87
|
# File 'lib/odata/operation.rb', line 85
def operation_url
raise NotImplementedError
end
|
#operation_username ⇒ Object
89
90
91
|
# File 'lib/odata/operation.rb', line 89
def operation_username
OdataConfig.odata_config[Rails.env]['username']
end
|
#run ⇒ Object
97
98
99
100
|
# File 'lib/odata/operation.rb', line 97
def run
response = send_odata
handle_operation_response(response)
end
|
#saved_many_to_many_id ⇒ Object
102
103
104
|
# File 'lib/odata/operation.rb', line 102
def saved_many_to_many_id
@ar.class.where("#{many_to_many_foreign_key(0)} = '#{many_to_many_entity_id(0)}' AND #{many_to_many_foreign_key(1)} = '#{many_to_many_entity_id(1)}'").first.id
end
|
#send_odata ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/odata/operation.rb', line 106
def send_odata
@ar.run_callbacks operation_callback_name do
if Rails.env.development? || Rails.env.test?
Rails.logger.debug "SEND_ODATA URL: #{operation_url}"
Rails.logger.debug "SEND_ODATA METHOD: #{operation_method}"
Rails.logger.debug "SEND_ODATA BODY: #{operation_body}"
end
request = ::Typhoeus::Request.new(
operation_url,
method: operation_method,
body: operation_body,
headers: ,
username: operation_username,
password: operation_password,
httpauth: :ntlm
)
request.run
end
end
|
#table_pluralize(name) ⇒ Object
93
94
95
|
# File 'lib/odata/operation.rb', line 93
def table_pluralize(name)
name.end_with?('s') ? "#{name}es" : name.pluralize
end
|