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
19
20
21
22
23
# File 'lib/shaf_client/base_resource.rb', line 9

def initialize(payload)
  @payload =
    if payload&.is_a? String
      JSON.parse(payload)
    elsif payload.respond_to? :to_h
      payload.to_h
    else
      raise Error, <<~ERR
        Trying to create an instance of #{self.class} with a payload that
        cannot be coerced into a Hash
      ERR
    end

  parse
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



144
145
146
147
# File 'lib/shaf_client/base_resource.rb', line 144

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



64
65
66
# File 'lib/shaf_client/base_resource.rb', line 64

def [](key)
  attributes[key]
end

#actionsObject



68
69
70
# File 'lib/shaf_client/base_resource.rb', line 68

def actions
  links.keys
end

#attribute(key) ⇒ Object



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

def attribute(key)
  _attribute(key) or raise Error, "No attribute for key: #{key}"
end

#curie(rel) ⇒ Object



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

def curie(rel)
  _curie(rel) or raise Error, "No curie with rel: #{rel}"
end

#embedded(rel) ⇒ Object



54
55
56
# File 'lib/shaf_client/base_resource.rb', line 54

def embedded(rel)
  _embedded(rel) or raise Error, "No embedded resources with rel: #{rel}"
end

#inspectObject



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

def inspect
  to_s
end


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

def link(rel)
  _link(rel) or raise Error, "No link with rel: #{rel}"
end

#rel?(rel) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/shaf_client/base_resource.rb', line 58

def rel?(rel)
  !link(rel).nil? || !embedded(rel).nil?
rescue StandardError
  false
end

#to_hObject



25
26
27
28
29
30
31
32
# File 'lib/shaf_client/base_resource.rb', line 25

def to_h
  attributes.dup.tap do |hash|
    hash[:_links] = transform_values_to_h(links)
    hash[:_links].merge!(curies: curies.values.map(&:to_h)) unless curies.empty?
    embedded = transform_values_to_h(embedded_resources)
    hash[:_embedded] = embedded unless embedded.empty?
  end
end

#to_sObject



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

def to_s
  JSON.pretty_generate(to_h)
end