Class: OData::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/odata/operation.rb

Direct Known Subclasses

CreateOperation, DeleteOperation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ar) ⇒ Operation



8
9
10
# File 'lib/odata/operation.rb', line 8

def initialize(ar)
  @ar = ar
end

Instance Attribute Details

#arObject

Returns the value of attribute ar.



6
7
8
# File 'lib/odata/operation.rb', line 6

def ar
  @ar
end

Instance Method Details

#base_urlObject



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)
  # Check for Http error
  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_nameObject



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

Raises:

  • (NotImplementedError)


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_nameObject



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_bodyObject



60
61
62
# File 'lib/odata/operation.rb', line 60

def operation_body
  nil
end

#operation_callback_nameObject

Raises:

  • (NotImplementedError)


64
65
66
# File 'lib/odata/operation.rb', line 64

def operation_callback_name
  raise NotImplementedError
end

#operation_headersObject



68
69
70
71
72
73
74
75
# File 'lib/odata/operation.rb', line 68

def operation_headers
  {
      'Accept' => 'application/json',
      'Content-Type' => 'application/json; charset=utf-8',
      'OData-MaxVersion' => '4.0',
      'OData-Version' => '4.0'
  }
end

#operation_methodObject

Raises:

  • (NotImplementedError)


77
78
79
# File 'lib/odata/operation.rb', line 77

def operation_method
  raise NotImplementedError
end

#operation_passwordObject



81
82
83
# File 'lib/odata/operation.rb', line 81

def operation_password
  ODATA_CONFIG[Rails.env]['password']
end

#operation_urlObject

Raises:

  • (NotImplementedError)


85
86
87
# File 'lib/odata/operation.rb', line 85

def operation_url
  raise NotImplementedError
end

#operation_usernameObject



89
90
91
# File 'lib/odata/operation.rb', line 89

def operation_username
  ODATA_CONFIG[Rails.env]['username']
end

#runObject



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_idObject



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_odataObject



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: operation_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