Skip to content

Commit 463215b

Browse files
authored
feat: Add BillableObjects resource (#22)
1 parent 8ee624a commit 463215b

10 files changed

Lines changed: 127 additions & 0 deletions

File tree

lib/kickplan/adapters/http.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def update_account(key, params)
4343
put("accounts/#{key}", params.to_h).body
4444
end
4545

46+
def upsert_billable_object(params)
47+
post("billable_objects", params.to_h).body
48+
end
49+
4650
# @api private
4751
def connection
4852
memoize do

lib/kickplan/adapters/memory.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ def update_account(key, params)
7171
end
7272
end
7373

74+
def upsert_billable_object(params)
75+
true
76+
end
77+
7478
# @api private
7579
def accounts
7680
memoize { Concurrent::Map.new }

lib/kickplan/request.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Request < Dry::Struct
1212

1313
require_relative "requests/accounts/create"
1414
require_relative "requests/accounts/update"
15+
require_relative "requests/billable_objects/upsert"
1516
require_relative "requests/features/resolve"
1617
require_relative "requests/metrics/set"
1718

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
module Kickplan
4+
module Requests
5+
module BillableObjects
6+
class Upsert < Request
7+
attribute :external_id, Types::String
8+
attribute :external_type, Types::String
9+
attribute :account_key, Types::String
10+
attribute? :properties, Types::Hash
11+
end
12+
end
13+
end
14+
end

lib/kickplan/resource.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def inspect
2323
end
2424

2525
require_relative "resources/accounts"
26+
require_relative "resources/billable_objects"
2627
require_relative "resources/features"
2728
require_relative "resources/metrics"
2829
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
module Kickplan
4+
module Resources
5+
class BillableObjects < Resource
6+
def upsert(options = {})
7+
params = Requests::BillableObjects::Upsert.new(options)
8+
response = adapter.upsert_billable_object(params)
9+
10+
Schemas::BillableObject.wrap(response)
11+
end
12+
end
13+
end
14+
end

lib/kickplan/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ def self.wrap(attributes = {})
2121
end
2222

2323
require_relative "schemas/account"
24+
require_relative "schemas/billable_object"
2425
require_relative "schemas/resolution"
2526
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
module Kickplan
4+
module Schemas
5+
class BillableObject < Schema
6+
attribute :external_id, Types::String
7+
attribute :external_type, Types::String
8+
attribute :account_key, Types::String
9+
attribute :properties, Types::Hash
10+
end
11+
end
12+
end

spec/cassettes/billable_objects/upsert.yml

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/kickplan/adapters/http_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
# Test the adapter through the resource interface
1313
let(:accounts) { client::Accounts }
14+
let(:billable_objects) { client::BillableObjects }
1415
let(:metrics) { client::Metrics }
1516
let(:features) { client::Features }
1617

@@ -207,4 +208,34 @@
207208
expect(response.name).to eq "Acme Inc."
208209
end
209210
end
211+
212+
describe "#upsert_billable_object",
213+
vcr: { cassette_name: "billable_objects/upsert" } do
214+
let(:params) {{
215+
external_id: "1234",
216+
external_type: "license",
217+
account_key: "acme",
218+
properties: {
219+
active: true
220+
}
221+
}}
222+
223+
it "creates a POST request for 'billable_objects'" do
224+
expect(adapter.connection).to receive(:post).
225+
with("billable_objects", hash_including(params)).
226+
and_call_original
227+
228+
billable_objects.upsert(params)
229+
end
230+
231+
it "returns Kickplan::Schemas::BillableObject", :aggregate_failures do
232+
response = billable_objects.upsert(params)
233+
234+
expect(response).to be_a Kickplan::Schemas::BillableObject
235+
expect(response.external_id).to eq "1234"
236+
expect(response.external_type).to eq "license"
237+
expect(response.account_key).to eq "acme"
238+
expect(response.properties).to eq params[:properties].transform_keys(&:to_s)
239+
end
240+
end
210241
end

0 commit comments

Comments
 (0)