Class: TAM::API

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/tam/api.rb,
lib/tam/api/sms.rb,
lib/tam/api/oauth.rb,
lib/tam/api/charging.rb,
lib/tam/api/location.rb
more...

Overview

Note:

All methods have been separated into modules and follow the same grouping used in the telco asset marketplace API Documentation.

Wrapper for the telco asset marketplace REST API

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.charge_by_amount(user, amount) ⇒ Hash

Requests for charging or payment to the subscriber account in the operator billing system by amount.

Returns:

  • (Hash)

    “message”=>“Request was handled succesfully”}

[View source]

12
13
14
15
16
17
18
19
20
# File 'lib/tam/api/charging.rb', line 12

def self.charge_by_amount(user, amount)
  begin
    payload = JSON.generate({'method' => 'AMOUNT', 'amount' => amount.to_s})
    response = dispatch_to_tam(:post, '/api/1/charge/request', user, payload)
    JSON.parse response
  rescue TAM::ServiceUnavailable
    raise TAM::ServiceUnavailable.new 'The charging service is not available. If you are using the service on a persona, i.e.: through the sandbox, then remember to set the balance of that persona'
  end
end

.charge_by_code(user, code) ⇒ Hash

Requests for charging or payment to the subscriber account in the operator billing system byt charging code.

Returns:

  • (Hash)

    “message”=>“Request was handled succesfully”}

[View source]

26
27
28
29
30
31
32
33
34
# File 'lib/tam/api/charging.rb', line 26

def self.charge_by_code(user, code)
  begin
    payload = JSON.generate({'method' => 'CODE', 'charging_code' => code.to_s})
    response = dispatch_to_tam(:post, '/api/1/charge/request', user, payload)
    JSON.parse response
  rescue TAM::ServiceUnavailable
    raise TAM::ServiceUnavailable.new 'The charging service is not available. If you are using the service on a persona, i.e.: through the sandbox, then remember to set the balance of that persona'
  end
end

.dispatch_to_tam(http_method, endpoint, user, payload = '') ⇒ Object

Dispatches the request to the telco asset marketplace REST API

[View source]

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tam/api.rb', line 40

def self.dispatch_to_tam(http_method, endpoint, user, payload='')
  consumer = create_oauth_consumer
  access_token = OAuth::AccessToken.new(consumer, user.access_token, user.token_secret)

  if (http_method == :get)
    response = access_token.send(http_method, endpoint, {'Content-Type' => 'application/json'})
  else
    response = access_token.send(http_method, endpoint, payload, {'Content-Type' => 'application/json'})
  end
  
  if response.class == Net::HTTPOK
    return response.body
  elsif response.class == Net::HTTPUnauthorized
    LOGGER.error 'Request not authorized ' + response.message + ', ' + response.body
    raise RequestNotAuthorized.new(response.message, response.body)
  elsif response.class == Net::HTTPBadRequest
    if response.body.include? 'consumer_key_unknown'
      LOGGER.error 'Configured telco asset marketplace consumer_key is not valid: ' + response.message + ', ' + response.body
      raise InvalidConsumerKey.new(response.message, response.body)
    elsif response.body.include? 'signature_invalid'
      LOGGER.error 'Configured telco asset marketplace consumer_secret is not valid: ' + response.message + ', ' + response.body
      raise InvalidConsumerSecret.new(response.message, response.body)
    else
      raise UnexpectedError.new(response.message, response.body)
    end
  elsif response.class == Net::HTTPServiceUnavailable
    LOGGER.error 'telco asset marketplace service ' + endpoint + ' not available'
    raise ServiceUnavailable.new(response.message, response.body)
  else
    raise UnexpectedError.new(response.message, response.body)
  end
end

.getcoord(user) ⇒ Hash

Find the location (coordinates) of a user

Returns:

  • (Hash)

    {"latitude"=>51.618,
     "timestamp"=>1302185772456,
     "accuracy"=>100,
     "longitude"=>23.9063,
    

    “status”=>{“code”=>0, “message”=>“Request was handled succesfully”}}

[View source]

17
18
19
20
21
22
23
24
# File 'lib/tam/api/location.rb', line 17

def self.getcoord(user)
  begin
    response = dispatch_to_tam(:get, '/api/1/location/getcoord', user)
    JSON.parse response
  rescue TAM::ServiceUnavailable
    raise TAM::ServiceUnavailable.new 'The location service is not available. If you are using the service on a persona, i.e.: through the sandbox, then remember to set the location of the persona'
  end
end

.send_sms(from_app, to_user, body, transaction_id = nil) ⇒ Object

Sends an SMS

[View source]

38
39
40
41
42
43
44
45
# File 'lib/tam/api/sms.rb', line 38

def self.send_sms(from_app, to_user, body, transaction_id = nil)
  payload = {'body' => body, 'from' => from_app}
  if transaction_id
    payload["transaction_id"] = transaction_id
  end
  response = dispatch_to_tam(:post, '/api/1/sms/send', to_user, JSON.generate(payload))
  JSON.parse response
end

Instance Method Details

#dispatch_to_handler(method, *args) ⇒ Object

Dispatches the request to the telco asset marketplace handler configured by this gem client

[View source]

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tam/api.rb', line 22

def dispatch_to_handler(method, *args)
  if TAM.consumer_handler.nil?
    LOGGER.error 'Application has not configured the telco asset marketplace consumer_handler'
    raise InvalidConsumerHandler.new 'Application has not configured the telco asset marketplace consumer_handler'
  end
  
  if TAM.consumer_handler.respond_to?(method)
    begin
      return TAM.consumer_handler.send(method, *args)
    rescue TAM::Error => error
      LOGGER.error 'Application has suffered an internal error: ' + error.message + ', ' + error.body
      raise error
    end
  end
  
end