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)



168
169
170
171
# File 'lib/shaf_client/base_resource.rb', line 168

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



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

def [](key)
  attributes[key]
end

#actionsObject



72
73
74
# File 'lib/shaf_client/base_resource.rb', line 72

def actions
  links.keys
end

#attribute(key, &block) ⇒ Object



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

def attribute(key, &block)
  block ||= proc { raise Error, "No attribute for key: #{key}" }
  _attribute(key, &block)
end

#curie(rel, &block) ⇒ Object



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

def curie(rel, &block)
  block ||= proc { raise Error, "No curie with rel: #{rel}" }
  _curie(rel, &block)
end

#embedded(rel, &block) ⇒ Object



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

def embedded(rel, &block)
  block ||= proc { raise Error, "No embedded resources with rel: #{rel}" }
  _embedded(rel, &block)
end

#inspectObject



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

def inspect
  to_s
end


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

def link(rel, &block)
  block ||= proc { raise Error, "No link with rel: #{rel}" }
  _link(rel, &block)
end

#rel?(rel) ⇒ Boolean

Returns:

  • (Boolean)


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

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