Class: Klarna::Checkout::Resource

Inherits:
Object
  • Object
show all
Extended by:
HasMany, HasOne
Defined in:
lib/klarna/checkout/resource.rb

Direct Known Subclasses

Address, Cart, CartItem, Customer, Gui, Merchant, MerchantReference, Order

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasOne

has_one

Methods included from HasMany

has_many

Constructor Details

#initialize(args = {}) ⇒ Resource

Returns a new instance of Resource.



14
15
16
17
18
19
# File 'lib/klarna/checkout/resource.rb', line 14

def initialize(args = {})
  self.class.defaults.deep_merge(args).each_pair do |attr, value|
    setter = "#{attr.to_s}="
    self.send(setter, value) if respond_to?(setter)
  end
end

Class Method Details

.defaultsObject



44
45
46
# File 'lib/klarna/checkout/resource.rb', line 44

def defaults
  @defaults || {}
end

.defaults=(hash) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/klarna/checkout/resource.rb', line 33

def defaults=(hash)
  if hash
    raise ArgumentError.new unless hash.is_a? Hash

    @defaults ||= {}
    @defaults.deep_merge!(hash)
  else
    @defaults = {}
  end
end

Instance Method Details

#json_sanitize(hash, keys = []) ⇒ Object



26
27
28
29
30
# File 'lib/klarna/checkout/resource.rb', line 26

def json_sanitize(hash, keys = [])
  hash.reject! { |k, v| v.nil? }
  hash.slice!(*Array(keys)) if keys.any?
  hash
end

#to_json(*keys) ⇒ Object



21
22
23
24
# File 'lib/klarna/checkout/resource.rb', line 21

def to_json(*keys)
  sanitized_json = json_sanitize(self.as_json, keys)
  JSON.generate(sanitized_json)
end