Class: Base

Inherits:
Object
  • Object
show all
Defined in:
lib/bitnob/objects/base/base.rb

Direct Known Subclasses

Cards, Checkout, Customer, LNURL, Lightning, Onchain, StableCoin, Swap, Wallets

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(production = false) ⇒ Base

Returns a new instance of Base.

Raises:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bitnob/objects/base/base.rb', line 11

def initialize(production = false)
  # - bitnob api key
  @secret_key = ENV['BITNOB_API_KEY']
  bitnob_sandbox_url = BaseEndpoints::BITNOB_SANDBOX_URL
  bitnob_live_url = BaseEndpoints::BITNOB_LIVE_URL

  # set bitnob url to sandbox or live if we are in production or development
  @url = if production == false
           bitnob_sandbox_url
         else
           bitnob_live_url
         end

  def base_url
    @url
  end

  if @secret_key.nil?
    raise BitnobBadKeyError,
          "No secret key supplied and couldn't find any in environment variables. Make sure to set secret key as an environment variable BITNOB_SECRET_KEY"
  end

  raise BitnobBadKeyError, "Invalid secret key #{@secret_key}" unless @secret_key[0..2] == 'sk.'
end

Instance Attribute Details

#productionObject

Returns the value of attribute production.



9
10
11
# File 'lib/bitnob/objects/base/base.rb', line 9

def production
  @production
end

#secret_keyObject

Returns the value of attribute secret_key.



9
10
11
# File 'lib/bitnob/objects/base/base.rb', line 9

def secret_key
  @secret_key
end

#urlObject

Returns the value of attribute url.



9
10
11
# File 'lib/bitnob/objects/base/base.rb', line 9

def url
  @url
end

Instance Method Details

#base_urlObject



24
25
26
# File 'lib/bitnob/objects/base/base.rb', line 24

def base_url
  @url
end

#check_parameters(required_params, passed_params) ⇒ Object

  • Verify that passed parameter contains required parameters



88
89
90
91
92
93
94
95
96
# File 'lib/bitnob/objects/base/base.rb', line 88

def check_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|
    unless passed_params.key?(k)
      raise IncompleteParameterError,
            "Parameters Incomplete, Missing Parameter: #{k}, Please pass in the complete parameter."
    end
  end
end

#get_request(endpoint) ⇒ Object

make a get request



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bitnob/objects/base/base.rb', line 37

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

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

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

#post_request(endpoint, data) ⇒ Object

method to make a post request



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bitnob/objects/base/base.rb', line 54

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

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

  response
end

#put_request(endpoint, data) ⇒ Object

method to make a put request



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bitnob/objects/base/base.rb', line 71

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

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

  response
end