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
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/odata/operation.rb', line 20
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
32
33
34
|
# File 'lib/odata/operation.rb', line 32
def entity_name
@ar.class.odata_table_reference || table_pluralize(@ar.class.table_name).downcase
end
|
#handle_operation_response(response) ⇒ Object
36
37
38
|
# File 'lib/odata/operation.rb', line 36
def handle_operation_response(response)
raise NotImplementedError
end
|
#many_to_many_associated_table_name(index) ⇒ Object
72
73
74
|
# File 'lib/odata/operation.rb', line 72
def many_to_many_associated_table_name(index)
@ar.class.many_to_many_associated_tables[index].table_name
end
|
#many_to_many_binding_name ⇒ Object
48
49
50
|
# File 'lib/odata/operation.rb', line 48
def many_to_many_binding_name
@ar.class.many_to_many_binding_name || many_to_many_table_name
end
|
#many_to_many_class_name(index) ⇒ Object
56
57
58
|
# File 'lib/odata/operation.rb', line 56
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
60
61
62
|
# File 'lib/odata/operation.rb', line 60
def many_to_many_entity_id(index)
@ar.send(many_to_many_class_name(index)).id
end
|
#many_to_many_entity_name(index) ⇒ Object
52
53
54
|
# File 'lib/odata/operation.rb', line 52
def many_to_many_entity_name(index)
@ar.class.many_to_many_associated_tables[index].odata_table_reference || table_pluralize(many_to_many_associated_table_name(index)).downcase
end
|
#many_to_many_foreign_key(index) ⇒ Object
64
65
66
|
# File 'lib/odata/operation.rb', line 64
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
40
41
42
|
# File 'lib/odata/operation.rb', line 40
def many_to_many_table?
@ar.class.many_to_many_associated_tables.present?
end
|
#many_to_many_table_name ⇒ Object
68
69
70
|
# File 'lib/odata/operation.rb', line 68
def many_to_many_table_name
@ar.class.odata_table_reference || @ar.class.table_name
end
|
#many_to_many_use_old_api? ⇒ Boolean
44
45
46
|
# File 'lib/odata/operation.rb', line 44
def many_to_many_use_old_api?
@ar.class.many_to_many_use_old_api
end
|
#old_base_url ⇒ Object
16
17
18
|
# File 'lib/odata/operation.rb', line 16
def old_base_url
OdataConfig.odata_config[Rails.env]['old_data_url']
end
|
#operation_body ⇒ Object
76
77
78
|
# File 'lib/odata/operation.rb', line 76
def operation_body
nil
end
|
#operation_callback_name ⇒ Object
80
81
82
|
# File 'lib/odata/operation.rb', line 80
def operation_callback_name
raise NotImplementedError
end
|
84
85
86
87
88
89
90
91
|
# File 'lib/odata/operation.rb', line 84
def
{
'Accept' => 'application/json',
'Content-Type' => 'application/json; charset=utf-8',
'OData-MaxVersion' => '4.0',
'OData-Version' => '4.0'
}
end
|
#operation_method ⇒ Object
93
94
95
|
# File 'lib/odata/operation.rb', line 93
def operation_method
raise NotImplementedError
end
|
#operation_password ⇒ Object
97
98
99
|
# File 'lib/odata/operation.rb', line 97
def operation_password
OdataConfig.odata_config[Rails.env]['password']
end
|
#operation_url ⇒ Object
101
102
103
|
# File 'lib/odata/operation.rb', line 101
def operation_url
raise NotImplementedError
end
|
#operation_username ⇒ Object
105
106
107
|
# File 'lib/odata/operation.rb', line 105
def operation_username
OdataConfig.odata_config[Rails.env]['username']
end
|
#run ⇒ Object
113
114
115
116
|
# File 'lib/odata/operation.rb', line 113
def run
response = send_odata
handle_operation_response(response)
end
|
#saved_many_to_many_id ⇒ Object
118
119
120
|
# File 'lib/odata/operation.rb', line 118
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/odata/operation.rb', line 122
def send_odata
@ar.run_callbacks operation_callback_name do
if Rails.env.development? || Rails.env.test? || Rails.env.staging?
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
109
110
111
|
# File 'lib/odata/operation.rb', line 109
def table_pluralize(name)
name.end_with?('s') ? "#{name}es" : name.pluralize
end
|