Class: KaChing::ApiV1::Bookings

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ka_ching/api_v1/bookings.rb

Overview

Bookings Endpoint for the KaChing API V1

Instance Method Summary collapse

Constructor Details

#initialize(conn:, api_url:) ⇒ Bookings

Returns a new instance of Bookings.



13
14
15
16
# File 'lib/ka_ching/api_v1/bookings.rb', line 13

def initialize(conn:, api_url:)
  @conn = conn
  @api_url = api_url
end

Instance Method Details

#deposit!(tenant_account_id:, amount_cents:, year: Date.today.year, month: Date.today.month, day: Date.today.day, context: {}) {|Faraday::Response| ... } ⇒ Hash

Deposit money to the current tenant account

Parameters:

  • tenant_account_id (String)

    without the tenant database namespace, e.g. ‘user_1’ instead of ‘kaching_tenant_user_1’

  • amount_cents (Integer)

    the amount to deposit in cents

  • year (Integer) (defaults to: Date.today.year)

    the year of the booking

  • month (Integer) (defaults to: Date.today.month)

    the month of the booking

  • day (Integer) (defaults to: Date.today.day)

    the day of the booking

  • context (Hash) (defaults to: {})

    additional context information

Yields:

  • (Faraday::Response)

    The response from the server

Returns:

  • (Hash)

    containing the booking details



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ka_ching/api_v1/bookings.rb', line 32

def deposit!(tenant_account_id:,
             amount_cents:,
             year: Date.today.year,
             month: Date.today.month,
             day: Date.today.day,
             context: {})
  res = create!(tenant_account_id: ,
                booking: {
                  action: :deposit,
                  amount_cents: amount_cents,
                  year: year,
                  month: month,
                  day: day,
                  context: context
                })
  yield res if block_given?
  JSON.parse(res.body)
end

#drop!(tenant_account_id:, booking_id:) {|Faraday::Response| ... } ⇒ Hash

drops/deletes a booking

Parameters:

  • tenant_account_id (String)

    without the tenant database namespace, e.g. ‘user_1’ instead of ‘kaching_tenant_user_1’

  • booking_id (String)

    uuid of the booking

Yields:

  • (Faraday::Response)

    The response from the server

Returns:

  • (Hash)

    containing the booking details



93
94
95
96
97
98
99
100
101
# File 'lib/ka_ching/api_v1/bookings.rb', line 93

def drop!(tenant_account_id:, booking_id:)
  drop_url = "#{build_url(tenant_account_id: )}/#{booking_id}"
  res = delete(drop_url) do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = { id: booking_id }.to_json
  end
  yield res if block_given?
  JSON.parse(res.body)
end

#unlocked(tenant_account_id:) {|Faraday::Response| ... } ⇒ Hash

returns all unlocked bookings for a tenant

Parameters:

  • tenant_account_id (String)

    without the tenant database namespace, e.g. ‘user_1’ instead of ‘kaching_tenant_user_1’

Yields:

  • (Faraday::Response)

    The response from the server

Returns:

  • (Hash)

    containing the booking details



112
113
114
115
116
117
118
119
# File 'lib/ka_ching/api_v1/bookings.rb', line 112

def unlocked(tenant_account_id:)
  unlocked_url = "#{build_url(tenant_account_id: )}/unlocked"
  res = get(unlocked_url) do |req|
    req.headers['Content-Type'] = 'application/json'
  end
  yield res if block_given?
  JSON.parse(res.body)
end

#withdraw!(tenant_account_id:, amount_cents:, year: Date.today.year, month: Date.today.month, day: Date.today.day, context: {}) {|Faraday::Response| ... } ⇒ Hash

Withdraw money to the current tenant account

Parameters:

  • tenant_account_id (String)

    without the tenant database namespace, e.g. ‘user_1’ instead of ‘kaching_tenant_user_1’

  • amount_cents (Integer)

    the amount to deposit in cents

  • year (Integer) (defaults to: Date.today.year)

    the year of the booking

  • month (Integer) (defaults to: Date.today.month)

    the month of the booking

  • day (Integer) (defaults to: Date.today.day)

    the day of the booking

  • context (Hash) (defaults to: {})

    additional context information

Yields:

  • (Faraday::Response)

    The response from the server

Returns:

  • (Hash)

    containing the booking details



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ka_ching/api_v1/bookings.rb', line 65

def withdraw!(tenant_account_id:,
              amount_cents:,
              year: Date.today.year,
              month: Date.today.month,
              day: Date.today.day,
              context: {})
  res = create!(tenant_account_id: , booking: {
                  action: :withdraw,
                  amount_cents: amount_cents,
                  year: year,
                  month: month,
                  day: day,
                  context: context
                })
  yield res if block_given?
  JSON.parse(res.body)
end