Class: StarkInfra::CreditPreview

Inherits:
StarkCore::Utils::SubResource
  • Object
show all
Defined in:
lib/creditpreview/creditpreview.rb

Overview

# CreditPreview object

A CreditPreview is used to get information from a credit before taking it. This resource can be used to preview credit notes.

## Parameters (required):

  • credit [CreditNotePreview object]: Information preview of the informed credit.

  • type [string]: Credit type. ex: “credit-note”

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credit:, type: nil) ⇒ CreditPreview

Returns a new instance of CreditPreview.



17
18
19
20
21
22
# File 'lib/creditpreview/creditpreview.rb', line 17

def initialize(credit:, type: nil)
  credit_info = CreditPreview.parse_credit(credit, type)
  @credit = credit_info['credit']
  @type = credit_info['type']

end

Instance Attribute Details

#creditObject (readonly)

Returns the value of attribute credit.



16
17
18
# File 'lib/creditpreview/creditpreview.rb', line 16

def credit
  @credit
end

#typeObject (readonly)

Returns the value of attribute type.



16
17
18
# File 'lib/creditpreview/creditpreview.rb', line 16

def type
  @type
end

Class Method Details

.create(previews, user: nil) ⇒ Object

# Create CreditPreviews

Send a list of CreditPreview objects for processing in the Stark Infra API

## Parameters (required):

  • previews [list of CreditPreview objects]: list of CreditPreview objects to be created in the API

## Parameters (optional):

  • user [Organization/Project object, default nil]: Organization or Project object. Not necessary if starkinfra.user was set before function call

## Return:

  • list of CreditPreview objects with updated attributes



36
37
38
# File 'lib/creditpreview/creditpreview.rb', line 36

def self.create(previews, user: nil)
  StarkInfra::Utils::Rest.post(entities: previews, user: user, **resource)
end

.parse_credit(credit, type) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/creditpreview/creditpreview.rb', line 40

def self.parse_credit(credit, type)
  resource_maker = { 'credit-note' => StarkInfra::CreditNotePreview.resource[:resource_maker] }
  if credit.is_a?(Hash)
    begin
      parsed_credit = StarkCore::Utils::API.from_api_json(resource_maker[type], credit)
      return { 'credit' => parsed_credit, 'type' => type }

    rescue StandardError
      return { 'credit' => credit, 'type' => type }

    end
  end

  return { 'credit' => credit, 'type' => type } if type

  if credit.class == StarkInfra::CreditNotePreview
    return { 'credit' => credit, 'type' => 'credit-note' }

  end

  raise 'credit must be either ' + 'a dictionary, ' \
          'a CreditNote.CreditNotePreview, but not a ' + credit.class.to_s
end

.resourceObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/creditpreview/creditpreview.rb', line 64

def self.resource
  {
    resource_name: 'CreditPreview',
    resource_maker: proc { |json|
      CreditPreview.new(
        credit: json['credit'],
        type: json['type']
      )
    }
  }
end