Class: FoundationApi::Event::Client

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Benchmarkable
Defined in:
lib/foundation_api/event/client.rb

Class Method Summary collapse

Class Method Details

.authenticateObject



24
25
26
27
28
29
# File 'lib/foundation_api/event/client.rb', line 24

def authenticate
  logger.debug "FoundationApi::Event::Client authenticating"
  self.auth_token = FoundationApi::JsonRPC::Client.authenticate
  logger.debug "auth_token: #{auth_token.inspect}"
  self.auth_token
end

.loggerObject



20
21
22
# File 'lib/foundation_api/event/client.rb', line 20

def logger
  ::Rails.logger
end

.post_request(interface, params = {}) ⇒ Object

call to send an unauthenticated request



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/foundation_api/event/client.rb', line 52

def post_request(interface, params = {})
  result = {}
  logger.info "Initiating FoundationApi::Event::Client POST #{uri.join(interface)} at #{Time.now}"
  benchmark "Connection time", :level => :info do
    connect do |connection|
      message = params.to_json
      logger.info "  Parameters: #{password_filter(message)}"
      benchmark "  Request time", :level => :info do
        result = JSON.parse(connection.post(uri.join(interface).path, message, {'Content-Type' => 'application/json', 'Accept' => '*/*'}).body)
      end
    end
    handle_response result, interface
    logger.info "  FoundationApi::Event::Client.post_request result: #{result.inspect}"
  end
  result['status']
end

.request(interface, params = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/foundation_api/event/client.rb', line 31

def request(interface, params = {})
  interface = interface.to_s
  tries = 0
  begin
    authenticate unless auth_token
    post_request interface, params.merge(:apitoken => auth_token)
  rescue => e
    if (tries += 1) == 1
      case e
      when AuthenticationFailed
        self.auth_token = nil
        retry
      when FoundationApi::JsonRPC::JsonRPCError
        retry if e.response == :invalid_security_token
      end
    end
    raise
  end
end

.site=(url) ⇒ Object



16
17
18
# File 'lib/foundation_api/event/client.rb', line 16

def site=(url)
  self.uri = Addressable::URI.parse(url)
end