Class: Momoapi::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/momoapi-ruby/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(option) ⇒ CLI

Returns a new instance of CLI.



17
18
19
20
21
22
# File 'lib/momoapi-ruby/cli.rb', line 17

def initialize(option)
  @uuid = SecureRandom.uuid
  @host = option[:host]
  @key = option[:key]
  create_sandbox_user
end

Instance Method Details

#create_sandbox_userObject

Create an API user in the sandbox target environment



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/momoapi-ruby/cli.rb', line 25

def create_sandbox_user
  body = { "providerCallbackHost": @host }
  @url = 'https://ericssonbasicapi2.azure-api.net/v1_0/apiuser'
  response = Faraday.post(@url) do |req|
    req.headers['Content-Type'] = 'application/json'
    req.headers['X-Reference-Id'] = @uuid
    req.headers['Ocp-Apim-Subscription-Key'] = @key
    req.body = body.to_json
  end

  unless response.status == 201
    raise Momoapi::Error.new(response.body, response.status)
  end

  generate_api_key
end

#generate_api_keyObject

Generate an API key in the sandbox target environment



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/momoapi-ruby/cli.rb', line 43

def generate_api_key
  @url = 'https://ericssonbasicapi2.azure-api.net/v1_0/apiuser/' +
         @uuid + '/apikey'
  response = Faraday.post(@url) do |req|
    req.headers['Ocp-Apim-Subscription-Key'] = @key
  end

  unless response.status == 201
    raise Momoapi::Error.new(response.body, response.status)
  end

  key = JSON.parse(response.body)
  puts "\n User ID: #{@uuid} \n API key: #{key['apiKey']} \n\n"
end