Class: Base

Inherits:
Object
  • Object
show all
Defined in:
lib/flutterwave_sdk/flutterwave_objects/base/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flutterwave_object = nil) ⇒ Base

method to initialize this class



12
13
14
15
16
17
# File 'lib/flutterwave_sdk/flutterwave_objects/base/base.rb', line 12

def initialize(flutterwave_object=nil)
  unless !flutterwave_object.nil?
    raise ArgumentError, "Flutterwave Object is required!!!"
  end
  @flutterwave_object = flutterwave_object
end

Instance Attribute Details

#flutterwave_objectObject (readonly)

Returns the value of attribute flutterwave_object.



8
9
10
# File 'lib/flutterwave_sdk/flutterwave_objects/base/base.rb', line 8

def flutterwave_object
  @flutterwave_object
end

Instance Method Details

#check_passed_parameters(required_params, passed_params) ⇒ Object

method to check if the passed parameters is equal to the expected parameters



98
99
100
101
102
103
104
105
106
# File 'lib/flutterwave_sdk/flutterwave_objects/base/base.rb', line 98

def check_passed_parameters(required_params, passed_params)

  # This is used to check if the passed authorization parameters matches the required parameters
  required_params.each do |k, v|
    if !passed_params.key?(k)
      raise IncompleteParameterError, "Parameters Incomplete, Missing Parameter: #{k}, Please pass in the complete parameter."
    end
  end
end

#delete_request(endpoint, data) ⇒ Object

method to make a delete request



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/flutterwave_sdk/flutterwave_objects/base/base.rb', line 80

def delete_request(endpoint, data)
    begin
      response = HTTParty.delete(endpoint, {
        body: data,
        headers: {
          "Content-Type" => "application/json",
          "Authorization" =>  "Bearer #{flutterwave_object.secret_key}"
        }
      })

      unless (response.code == 200 || response.code == 201)
        raise FlutterwaveServerError.new(response), "HTTP Code #{response.code}: #{response.body}"
      end
      return response
  end
end

#get_request(endpoint) ⇒ Object

method to make a get request



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/flutterwave_sdk/flutterwave_objects/base/base.rb', line 21

def get_request(endpoint)
  # headers = {
  #     "Authorization" => "Bearer #{flutterwave_object.secret_key}"
  # }
  begin
    response = HTTParty.get(endpoint, :headers => { "Authorization" => "Bearer #{flutterwave_object.secret_key}" })
    unless (response.code == 200 || response.code == 201)
      raise FlutterwaveServerError.new(response), "HTTP Code #{response.code}: #{response.body}"
    end

    return response

    unless(response.code != 0 )
      raise FlutterwaveServerError.new(response), "Server Message: #{response.message}"
    end

    rescue JSON::ParserError => jsonerr
      raise FlutterwaveServerError.new(response) , "Invalid result data. Could not parse JSON response body \n #{jsonerr.message}"
      return response
    end
end

#post_request(endpoint, data) ⇒ Object

method to make a post request



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/flutterwave_sdk/flutterwave_objects/base/base.rb', line 44

def post_request(endpoint, data)
  begin
    response = HTTParty.post(endpoint, {
      body: data,
      headers: {
        "Content-Type" => "application/json",
        "Authorization" =>  "Bearer #{flutterwave_object.secret_key}"
      }
    })

    unless (response.code == 200 || response.code == 201)
      raise FlutterwaveServerError.new(response), "HTTP Code #{response.code}: #{response.body}"
    end
    return response
  end

end

#put_request(endpoint, data) ⇒ Object

method to make a put request



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/flutterwave_sdk/flutterwave_objects/base/base.rb', line 62

def put_request(endpoint, data)
    begin
      response = HTTParty.put(endpoint, {
        body: data,
        headers: {
          "Content-Type" => "application/json",
          "Authorization" =>  "Bearer #{flutterwave_object.secret_key}"
        }
      })

      unless (response.code == 200 || response.code == 201)
        raise FlutterwaveServerError.new(response), "HTTP Code #{response.code}: #{response.body}"
      end
      return response
  end
end