Class: Momoapi::CLI
- Inherits:
-
Object
- Object
- Momoapi::CLI
- Defined in:
- lib/momoapi-ruby/cli.rb
Instance Method Summary collapse
- #create_sandbox_user ⇒ Object
- #generate_api_key ⇒ Object
-
#initialize(option) ⇒ CLI
constructor
A new instance of CLI.
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_user ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/momoapi-ruby/cli.rb', line 24 def create_sandbox_user body = { "providerCallbackHost": @host } @url = 'https://sandbox.momodeveloper.mtn.com/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 Error::APIError.new(response.body, response.status) end generate_api_key end |
#generate_api_key ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/momoapi-ruby/cli.rb', line 41 def generate_api_key @url = 'https://sandbox.momodeveloper.mtn.com/v1_0/apiuser/' + @uuid + '/apikey' response = Faraday.post(@url) do |req| req.headers['Ocp-Apim-Subscription-Key'] = @key end unless response.status == 201 raise Error::APIError.new(response.body, response.status) end key = JSON.parse(response.body) puts "\n User ID: #{@uuid} \n API key: #{key['apiKey']} \n\n" end |