Module: Charging::Http

Defined in:
lib/charging/http.rb

Overview

:nodoc:

Defined Under Namespace

Classes: LastResponseError

Class Method Summary collapse

Class Method Details

.basic_credential_for(user, password = nil) ⇒ Object


41
42
43
44
45
46
47
# File 'lib/charging/http.rb', line 41

def basic_credential_for(user, password = nil)
  credential_for = user.to_s
  credential_for << ":#{password}" unless password.nil?

  credential = ::Base64.strict_encode64(credential_for)
  "Basic #{credential}"
end

.charging_path(path) ⇒ Object


74
75
76
# File 'lib/charging/http.rb', line 74

def charging_path(path)
  "#{Charging.configuration.url}#{path}"
end

.common_params(token, etag) ⇒ Object


78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/charging/http.rb', line 78

def common_params(token, etag)
  token = Charging.configuration.application_token if token === :use_application_token
  request_headers = {
    authorization: basic_credential_for('', token),
    content_type: :json,
    accept: :json,
    user_agent: Charging.configuration.user_agent
  }

  request_headers['If-Match'] = etag if etag

  request_headers
end

.delete(path, token, etag) ⇒ Object


25
26
27
# File 'lib/charging/http.rb', line 25

def delete(path, token, etag)
  request_to_api(:delete, path, {etag: etag}, token)
end

.encoded_body(body) ⇒ Object


92
93
94
# File 'lib/charging/http.rb', line 92

def encoded_body(body)
  body.is_a?(Hash) ? MultiJson.encode(body) : body
end

.get(path, token, params = {}) ⇒ Object


21
22
23
# File 'lib/charging/http.rb', line 21

def get(path, token, params = {})
  request_to_api(:get, path, params, token)
end

.patch(path, token, etag, body = {}) ⇒ Object


37
38
39
# File 'lib/charging/http.rb', line 37

def patch(path, token, etag, body = {})
  request_to_api(:patch, path, {etag: etag}, token, body)
end

.post(path, token, body = {}, params = {}) ⇒ Object


29
30
31
# File 'lib/charging/http.rb', line 29

def post(path, token, body = {}, params = {})
  request_to_api(:post, path, params, token, body)
end

.put(path, token, etag, body = {}) ⇒ Object


33
34
35
# File 'lib/charging/http.rb', line 33

def put(path, token, etag, body = {})
  request_to_api(:put, path, {etag: etag}, token, body)
end

.request_to_api(method, path, params, token, body = nil) ⇒ Object


59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/charging/http.rb', line 59

def request_to_api(method, path, params, token, body = nil)
  path = charging_path(path) unless path.start_with?('http')
  etag = params.delete(:etag)
  
  args = [method, path]
  args << encoded_body(body) if body

  RestClient.send(*args,
    {params: params}.merge(common_params(token, etag)),
    &should_follow_redirect
  )
rescue ::RestClient::Exception => exception
  raise LastResponseError.new(exception.response)
end

.should_follow_redirect(follow = true) ⇒ Object


49
50
51
52
53
54
55
56
57
# File 'lib/charging/http.rb', line 49

def should_follow_redirect(follow = true)
  proc { |response, request, result, &block|
    if follow && [301, 302, 307].include?(response.code)
      response.follow_redirection(request, result, &block)
    else
      response.return!(request, result, &block)
    end
  }
end