Module: ClientSuccess::Subscription

Extended by:
Subscription
Included in:
Subscription
Defined in:
lib/client_success/subscription.rb

Defined Under Namespace

Classes: Error, InvalidAttributes

Instance Method Summary collapse

Instance Method Details

#create(attributes:, connection:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/client_success/subscription.rb', line 12

def create(attributes:, connection:)
  body = Schema::Subscription::Create[attributes]
    .deep_transform_keys { |k| k.to_s.camelize(:lower) }
    .to_json

  response = connection.post(
    "/v1/subscriptions", body)

  if response.body.blank?
    raise InvalidAttributes, "subscription has invalid attributes"
  else
    payload = response.body
    DomainModel::Subscription.new(
      payload.deep_transform_keys(&:underscore))
  end
end

#last(client_id:, connection:) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/client_success/subscription.rb', line 40

def last(client_id:, connection:)
  response = connection.get(
    "/v1/subscriptions?clientId=#{client_id}")

  payload = response.body.last.to_h

  DomainModel::Subscription.new(
    payload.deep_transform_keys(&:underscore))
end

#update(attributes:, connection:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/client_success/subscription.rb', line 29

def update(attributes:, connection:)
  body = Schema::Subscription::Update[attributes]
             .deep_transform_keys { |k| k.to_s.camelize(:lower) }
             .to_json

  response = connection.put(
    "/v1/subscriptions/#{attributes[:id]}", body)

  response.body
end