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

#operation_bodyObject



36
37
38
# File 'lib/odata/operation.rb', line 36

def operation_body
  nil
end

#operation_callback_nameObject

Raises:

  • (NotImplementedError)


40
41
42
# File 'lib/odata/operation.rb', line 40

def operation_callback_name
  raise NotImplementedError
end

#operation_headersObject



44
45
46
47
48
49
# File 'lib/odata/operation.rb', line 44

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

#operation_methodObject

Raises:

  • (NotImplementedError)


51
52
53
# File 'lib/odata/operation.rb', line 51

def operation_method
  raise NotImplementedError
end

#operation_passwordObject



55
56
57
# File 'lib/odata/operation.rb', line 55

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

#operation_urlObject

Raises:

  • (NotImplementedError)


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

def operation_url
  raise NotImplementedError
end

#operation_usernameObject



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

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

#runObject



71
72
73
74
# File 'lib/odata/operation.rb', line 71

def run
  response = send_odata
  handle_operation_response(response)
end

#send_odataObject



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