Class: OData::Operation
- Inherits:
-
Object
show all
- Defined in:
- lib/odata/operation.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
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
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
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
|
#operation_body ⇒ Object
36
37
38
|
# File 'lib/odata/operation.rb', line 36
def operation_body
nil
end
|
#operation_callback_name ⇒ Object
40
41
42
|
# File 'lib/odata/operation.rb', line 40
def operation_callback_name
raise NotImplementedError
end
|
44
45
46
47
48
49
|
# File 'lib/odata/operation.rb', line 44
def
{
'Accept' => 'application/json',
'Content-Type' => 'application/json; charset=utf-8'
}
end
|
#operation_method ⇒ Object
51
52
53
|
# File 'lib/odata/operation.rb', line 51
def operation_method
raise NotImplementedError
end
|
#operation_password ⇒ Object
55
56
57
|
# File 'lib/odata/operation.rb', line 55
def operation_password
ODATA_CONFIG[Rails.env]['password']
end
|
#operation_url ⇒ Object
59
60
61
|
# File 'lib/odata/operation.rb', line 59
def operation_url
raise NotImplementedError
end
|
#operation_username ⇒ Object
63
64
65
|
# File 'lib/odata/operation.rb', line 63
def operation_username
ODATA_CONFIG[Rails.env]['username']
end
|
#run ⇒ Object
71
72
73
74
|
# File 'lib/odata/operation.rb', line 71
def run
response = send_odata
handle_operation_response(response)
end
|
#send_odata ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/odata/operation.rb', line 76
def send_odata
@ar.run_callbacks operation_callback_name do
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
67
68
69
|
# File 'lib/odata/operation.rb', line 67
def table_pluralize(name)
name.end_with?('s') ? "#{name}es" : name.pluralize
end
|