Class: FoundationApi::JsonRPC::Client

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

Class Method Summary collapse

Class Method Details

.authenticateObject



25
26
27
28
# File 'lib/foundation_api/json_rpc/client.rb', line 25

def authenticate
  self.auth_token = nil
  self.auth_token = post_request('APIAuthenticate', :username => uri.user, :password => uri.password)
end

.key_value_array(attributes = {}) ⇒ Object



69
70
71
# File 'lib/foundation_api/json_rpc/client.rb', line 69

def key_value_array(attributes = {})
  attributes.stringify_keys.sort
end

.loggerObject



21
22
23
# File 'lib/foundation_api/json_rpc/client.rb', line 21

def logger
  ::Rails.logger
end

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

call to send an unauthenticated request



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/foundation_api/json_rpc/client.rb', line 46

def post_request(method, params = {})
  result = {}
  Net::HTTP.start(uri.host, uri.port) do |connection|
    logger.debug "Started JsonRPC POST #{uri.path} at #{Time.now}"
    message = {:method => method.to_s, :id => 'jsonrpc', :params => [params]}.to_json
    logger.debug "Parameters: #{password_filter(message)}"
    benchmark "Request time", :level => :info do        
      result = JSON.parse(connection.post(uri.path, message).body)
    end
  end
  if error = result["error"]
    logger.error "FoundationApi::JsonRPC::JsonRPCError: #{method} -> #{result.inspect}"
    raise JsonRPCError.new(error['code']), error["message"]
  else
    logger.debug "FoundationApi::JsonRPC::Client.post_request result: #{result.inspect}"
  end
  result['result']
end

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/foundation_api/json_rpc/client.rb', line 30

def request(method, params = {})
  tries = 0
  begin
    authenticate unless auth_token
    post_request method, params.merge(:authorization => auth_token)
  rescue JsonRPCError => e
    tries += 1
    if e.response == :invalid_security_token && tries == 1
      self.auth_token = nil
      retry
    end
    raise
  end
end

.site=(url) ⇒ Object



17
18
19
# File 'lib/foundation_api/json_rpc/client.rb', line 17

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

.unique_id(seed) ⇒ Object



65
66
67
# File 'lib/foundation_api/json_rpc/client.rb', line 65

def unique_id(seed)
  "#{Time.now.to_f}.#{seed}"
end