Class: Creatary::API

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

Overview

Note:

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

Wrapper for the Creatary 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”}



12
13
14
15
16
17
18
19
20
# File 'lib/creatary/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_server(:post, '/api/1/charge/request', user, payload)
    JSON.parse response
  rescue Creatary::ServiceUnavailable
    raise Creatary::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”}



26
27
28
29
30
31
32
33
34
# File 'lib/creatary/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_server(:post, '/api/1/charge/request', user, payload)
    JSON.parse response
  rescue Creatary::ServiceUnavailable
    raise Creatary::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_server(http_method, endpoint, user, payload = '') ⇒ Object

Dispatches the request to the Creatary REST API



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
72
# File 'lib/creatary/api.rb', line 41

def self.dispatch_to_server(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 or http_method == :delete)
    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 Creatary 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 Creatary 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 'Creatary 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”}}



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

def self.getcoord(user)
  begin
    response = dispatch_to_server(:get, '/api/1/location/getcoord', user)
    JSON.parse response
  rescue Creatary::ServiceUnavailable
    raise Creatary::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



38
39
40
41
42
43
44
45
# File 'lib/creatary/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_server(:post, '/api/1/sms/send', to_user, JSON.generate(payload))
  JSON.parse response
end

.unregister(user) ⇒ Hash

Unregisters a user

Returns:

  • (Hash)

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



41
42
43
44
45
46
# File 'lib/creatary/api/lifecycle.rb', line 41

def self.unregister(user)
  puts "here1"
  response = dispatch_to_server(:delete, '/api/1/subscriberlifecycle/unsubscribe', user)
  puts "here2"
  JSON.parse response
end

Instance Method Details

#dispatch_to_handler(method, *args) ⇒ Object

Dispatches the request to the Creatary handler configured by this gem client



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

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