Class: MmJsonClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mm_json_client/client.rb

Overview

The entry point for using the API client.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
# File 'lib/mm_json_client/client.rb', line 10

def initialize(args = {})
  arg_errors = user_args_errors(args)
  raise ArgumentError, arg_errors.join(', ') unless arg_errors.empty?
  @options = calculate_options(args)
  @session_id = nil
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



8
9
10
# File 'lib/mm_json_client/client.rb', line 8

def connection
  @connection
end

Class Method Details

.define_api_method(method, return_type) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mm_json_client/client.rb', line 106

def define_api_method(method, return_type)
  ruby_method = method.mm_underscore
  class_eval do
    define_method(ruby_method) do |args = {}|
      unless args.class == Hash
        raise ArgumentError, 'argument must be a hash'
      end
      generic_request(method, args, return_type)
    end
  end
end

Instance Method Details

#client_objects_to_h(value) ⇒ Object

Recusively converts found MmJsonClient objects to hashes.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mm_json_client/client.rb', line 50

def client_objects_to_h(value)
  case value.class.to_s
  when /^MmJsonClient/
    client_objects_to_h(value.to_h)
  when 'Hash'
    Hash[value.map { |k, v| [k, client_objects_to_h(v)] }]
  when 'Array'
    value.map { |v| client_objects_to_h(v) }
  else
    value
  end
end

#loginObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mm_json_client/client.rb', line 17

def 
  @options[:proxy].each do |proxy|
    begin
      @rpc_client = JsonRpcHttp::Client.new(base_url(proxy, @options),
                                            @options)
      response = generic_request('Login', , 'LoginResponse', false)
      @session_id = response.session
      return true
    rescue MmJsonClient::ServerError => e
      raise e unless e.code == MmJsonClient::ResponseCode::REQUESTS_DISABLED
    rescue SocketError, Net::OpenTimeout, Net::HTTPServerError
      # Keep trying the next server.
    end

  end

  if @session_id.nil?
    # We got here without a successful connection. Time to give up.
    raise MmJsonClient::ServerConnectionError,
      'Unable to connect to the proxy.'
  else
    true
  end
end

#logoutObject



42
43
44
45
46
47
# File 'lib/mm_json_client/client.rb', line 42

def logout
  return true if @session_id.nil?
  generic_request('Logout', {}, 'LogoutResponse')
  @session_id = nil
  true
end