Class: Peperusha::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/peperusha/client.rb

Class Method Summary collapse

Class Method Details

.build_errors_collection(response) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/peperusha/client.rb', line 41

def self.build_errors_collection(response)
  data = JSON.parse(response.body)
  [
    {
      status_code: response.status,
      error: {
        code: data['errorCode'],
        errorMessage: data['errorMessage']
      }
    }
  ]
end

.invoke_post_request(token, domain_url, body) ⇒ Object



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

def self.invoke_post_request(token, domain_url, body)
  base_url = 'https://sandbox.safaricom.co.ke'
  headers = {
    'Accept' => 'application/json',
    'Authorization' => "Bearer #{token}",
    'Content-Type' => 'application/json'
  }

  conn = Faraday.new(url: base_url, headers: headers) do |build|
    build.request :url_encoded
    build.use Peperusha::Logs
    build.adapter  Faraday.default_adapter
  end

  conn.post(domain_url) do |req|
    req.body = body.to_json
  end
end

.invoke_token_request(consumer_key, consumer_secret) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/peperusha/client.rb', line 8

def self.invoke_token_request(consumer_key, consumer_secret)
  conn = Faraday.new(url: 'https://sandbox.safaricom.co.ke') do |build|
    build.request :url_encoded
    build.use Peperusha::Logs
    build.adapter  Faraday.default_adapter
  end

  conn.basic_auth(consumer_key, consumer_secret)

  conn.get('oauth/v1/generate') do |req|
    req.params['grant_type'] = 'client_credentials'
  end
end