Class: Mashery::Client

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

Constant Summary collapse

TEST_HOST =
'api.sandbox.mashery.com'
PRODUCTION_HOST =
'api.mashery.com'

Instance Method Summary collapse

Constructor Details

#initialize(site_id, key, secret) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
# File 'lib/mashery/client.rb', line 11

def initialize(site_id, key, secret)
  host = Mashery.test_mode ? TEST_HOST : PRODUCTION_HOST
  @uri = "http://#{host}/v2/json-rpc/#{site_id}"
  @key = key
  @secret = secret
end

Instance Method Details

#call_remote(method, *params) ⇒ Object

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mashery/client.rb', line 22

def call_remote(method, *params)
  # all calls are synchronous, so id in request and response will always be 1
  if Mashery.logger
    Mashery.logger.debug("Calling method #{method} with params #{params.inspect} on URI #{signed_uri}")
  end
  req = ::JSON[{:version => '1.1', :method => method, :params => params, :id => 1}]
  response = HTTParty.post(signed_uri, :body => req)
  raise HttpException.new(response.headers['x-mashery-error-code']) unless response.code < 300
  res = ::JSON[response.body]
  raise Exception.create(res['error']) if res.include?('error')
  res['result']
end

#echo(value) ⇒ Object



18
19
20
# File 'lib/mashery/client.rb', line 18

def echo(value)
  call_remote('test.echo', value)
end