Class: ShafClient::BaseResource

Inherits:
Object
  • Object
show all
Defined in:
lib/shaf_client/base_resource.rb

Direct Known Subclasses

Resource

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload) ⇒ BaseResource

Returns a new instance of BaseResource.



9
10
11
12
13
14
15
16
17
18
# File 'lib/shaf_client/base_resource.rb', line 9

def initialize(payload)
  @payload =
    if payload&.is_a? String
      JSON.parse(payload)
    else
      payload
    end

  parse
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



108
109
110
111
# File 'lib/shaf_client/base_resource.rb', line 108

def method_missing(method_name, *args, &block)
  return super unless attributes.key?(method_name)
  attribute(method_name)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



7
8
9
# File 'lib/shaf_client/base_resource.rb', line 7

def attributes
  @attributes
end

#curiesObject (readonly)

Returns the value of attribute curies.



7
8
9
# File 'lib/shaf_client/base_resource.rb', line 7

def curies
  @curies
end

#embedded_resourcesObject (readonly)

Returns the value of attribute embedded_resources.



7
8
9
# File 'lib/shaf_client/base_resource.rb', line 7

def embedded_resources
  @embedded_resources
end

Returns the value of attribute links.



7
8
9
# File 'lib/shaf_client/base_resource.rb', line 7

def links
  @links
end

Instance Method Details

#[](key) ⇒ Object



46
47
48
# File 'lib/shaf_client/base_resource.rb', line 46

def [](key)
  attributes[key]
end

#actionsObject



50
51
52
# File 'lib/shaf_client/base_resource.rb', line 50

def actions
  links.keys
end

#attribute(key) ⇒ Object



30
31
32
# File 'lib/shaf_client/base_resource.rb', line 30

def attribute(key)
  attributes.fetch(key.to_sym)
end

#curie(rel) ⇒ Object



38
39
40
# File 'lib/shaf_client/base_resource.rb', line 38

def curie(rel)
  curies.fetch(rel.to_sym)
end

#embedded(rel) ⇒ Object



42
43
44
# File 'lib/shaf_client/base_resource.rb', line 42

def embedded(rel)
  embedded_resources.fetch(rel.to_sym)
end


34
35
36
# File 'lib/shaf_client/base_resource.rb', line 34

def link(rel)
  links.fetch(rel.to_sym)
end

#to_hObject



20
21
22
23
24
# File 'lib/shaf_client/base_resource.rb', line 20

def to_h
  attributes
    .merge(_links: transform_values_to_s(links))
    .merge(_embedded: transform_values_to_s(embedded_resources))
end

#to_sObject



26
27
28
# File 'lib/shaf_client/base_resource.rb', line 26

def to_s
  JSON.pretty_generate(to_h)
end