Class: HumanTone::Account

Inherits:
Object
  • Object
show all
Defined in:
lib/humantone/account.rb

Constant Summary collapse

DECODER =
lambda do |body, request_id|
  Coercion.hash!(body[:plan], 'plan')
  Coercion.hash!(body[:credits], 'credits')
  Coercion.hash!(body[:subscription], 'subscription')

  plan = Plan.new(
    id: Coercion.string!(body[:plan][:id], 'plan.id'),
    name: Coercion.string!(body[:plan][:name], 'plan.name'),
    max_words: Coercion.integer!(body[:plan][:max_words], 'plan.max_words'),
    monthly_credits: Coercion.integer!(body[:plan][:monthly_credits], 'plan.monthly_credits'),
    api_access: Coercion.boolean!(body[:plan][:api_access], 'plan.api_access')
  )

  credits = Credits.new(
    trial: Coercion.integer!(body[:credits][:trial], 'credits.trial'),
    subscription: Coercion.integer!(body[:credits][:subscription], 'credits.subscription'),
    extra: Coercion.integer!(body[:credits][:extra], 'credits.extra'),
    total: Coercion.integer!(body[:credits][:total], 'credits.total')
  )

  subscription = Subscription.new(
    active: Coercion.boolean!(body[:subscription][:active], 'subscription.active'),
    expires_at: Coercion.optional_iso8601_time(body[:subscription][:expires_at], 'subscription.expires_at')
  )

  AccountInfo.new(
    plan: plan,
    credits: credits,
    subscription: subscription,
    request_id: request_id
  )
end

Instance Method Summary collapse

Constructor Details

#initialize(http:, retry_layer:) ⇒ Account

Returns a new instance of Account.



42
43
44
45
# File 'lib/humantone/account.rb', line 42

def initialize(http:, retry_layer:)
  @http = http
  @retry_layer = retry_layer
end

Instance Method Details

#getObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/humantone/account.rb', line 47

def get
  @retry_layer.call(method: :get, endpoint: :account) do
    raw = @http.request(method: :get, path: '/v1/account')
    ErrorParser.parse(
      endpoint: :account,
      status: raw[:status],
      headers: raw[:headers],
      body_string: raw[:body_string],
      decoder: DECODER
    )
  end
end