Class: Licensario::API

Inherits:
Object
  • Object
show all
Defined in:
lib/licensario/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, secret, base_url = 'users.licensario.com', use_ssl = true, debug = false) ⇒ API

Returns a new instance of API.



8
9
10
# File 'lib/licensario/api.rb', line 8

def initialize(key, secret, base_url = 'users.licensario.com', use_ssl = true, debug = false)
  @key, @secret, @base_url, @use_ssl, @debug = key, secret, base_url, use_ssl, debug
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



6
7
8
# File 'lib/licensario/api.rb', line 6

def base_url
  @base_url
end

#debugObject (readonly)

Returns the value of attribute debug.



6
7
8
# File 'lib/licensario/api.rb', line 6

def debug
  @debug
end

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/licensario/api.rb', line 6

def key
  @key
end

#secretObject (readonly)

Returns the value of attribute secret.



6
7
8
# File 'lib/licensario/api.rb', line 6

def secret
  @secret
end

Instance Method Details

#can_increment_resource?(user_id, external_user_id, resource_id, amount) ⇒ Boolean

Checks if Resource can be incremented

Returns:

  • (Boolean)


87
88
89
90
# File 'lib/licensario/api.rb', line 87

def can_increment_resource?(user_id, external_user_id, resource_id, amount)
  res = resource_available?(user_id, external_user_id, resource_id, amount, "increment")
  return (res[:body] =~ /yes/ or res[:body] =~ /true/) != nil
end

#can_set_resource?(user_id, external_user_id, resource_id, amount) ⇒ Boolean

Checks if Resource can be setted

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/licensario/api.rb', line 93

def can_set_resource?(user_id, external_user_id, resource_id, amount)
  res = resource_available?(user_id, external_user_id, resource_id, amount, "set")
  return (res[:body] =~ /yes/ or res[:body] =~ /true/) != nil
end

#check_server_statusObject

Checks the API Server Status - TODO



57
58
59
60
# File 'lib/licensario/api.rb', line 57

def check_server_status
  status = do_request(:get, '/status')
  return 'OK'
end

#do_request(method, resource, params = {}) ⇒ Object

Executes the actual request with the API server and processes the response



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/licensario/api.rb', line 18

def do_request(method, resource, params = {})
  begin
    uri = URI.parse(request_url(resource))
    uri.query = URI.encode_www_form(params) if !params.empty? and method == :get
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = @use_ssl
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @use_ssl
    case method
    when :post
      req_class = Net::HTTP::Post
    when :get
      req_class = Net::HTTP::Get
    when :put
      req_class = Net::HTTP::Put
    when :delete
      req_class = Net::HTTP::Delete
    end
    request = req_class.new(uri.request_uri)
    request.content_type = "application/x-www-form-urlencoded"
    request['ISV_API_KEY'] = @key
    request['ISV_API_SECRET'] = @secret
    request['LCNS_DISABLE_SIGN'] = 'true'
    request.set_form_data(params) if !params.empty? and [:post,:put].include?(method)
    response = http.request(request)
    if @debug
      puts "################################"
      puts "REQUEST: #{method.upcase} #{uri.request_uri}"
      puts "PARAMETERS: #{params}" if !params.empty? and [:post,:put].include?(method)
      puts "RESPONSE CODE: #{response.code}"
      puts "BODY:\n#{response.body}"
      puts "================================"
    end
    return { body: response.body, status: response.code.to_i }
  rescue
    return { body: nil, status: nil }
  end
end

#ensure_external_user_exists(external_user_id, email) ⇒ Object

Ensures the existence of an external user



72
73
74
# File 'lib/licensario/api.rb', line 72

def ensure_external_user_exists(external_user_id, email)
  do_request(:put, "/api/v1/users/external/#{external_user_id}", { email: email })
end

#get_external_user(user_id, user_email) ⇒ Object

Gets an External User



63
64
65
66
67
68
69
# File 'lib/licensario/api.rb', line 63

def get_external_user(user_id, user_email)
  res = ensure_external_user_exists(user_id, user_email)
  params = res[:body]
  user = Licensario::User.new(params)
  user.api = self
  return user
end

#request_url(url) ⇒ Object

Forms the appropriate request URL



13
14
15
# File 'lib/licensario/api.rb', line 13

def request_url(url)
  (@use_ssl ? 'https' : 'http') + '://' + @base_url + url
end

#resource_available?(user_id, external_user_id, resource_id, amount, operation) ⇒ Boolean

Checks the Resource Availability

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
# File 'lib/licensario/api.rb', line 77

def resource_available?(user_id, external_user_id, resource_id, amount, operation)
  url = "/api/userresources/available?userId=" + user_id
  url += "&externalUserId=" + external_user_id
  url += "&resourceId=" + resource_id
  url += "&amount=" + amount
  url += "&operation=" + operation
  do_request(:get, url)
end