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)



128
129
130
131
# File 'lib/shaf_client/base_resource.rb', line 128

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



66
67
68
# File 'lib/shaf_client/base_resource.rb', line 66

def [](key)
  attributes[key]
end

#actionsObject



70
71
72
# File 'lib/shaf_client/base_resource.rb', line 70

def actions
  links.keys
end

#attribute(key) ⇒ Object

Raises:



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

def attribute(key)
  raise Error, "No attribute for key: #{key}" unless attributes.key? key
  attributes.fetch(key.to_sym)
end

#curie(rel) ⇒ Object

Raises:



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

def curie(rel)
  raise Error, "No curie with rel: #{rel}" unless curies.key? rel.to_sym
  curies[rel.to_sym]
end

#embedded(rel) ⇒ Object



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

def embedded(rel)
  rewritten_rel = best_match(embedded_resources.keys, rel)
  unless embedded_resources.key? rewritten_rel
    raise Error, "No embedded resources with rel: #{rel}"
  end
  embedded_resources[rewritten_rel]
end

#inspectObject



32
33
34
# File 'lib/shaf_client/base_resource.rb', line 32

def inspect
  to_s
end

Raises:



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

def link(rel)
  rewritten_rel = best_match(links.keys, rel)
  raise Error, "No link with rel: #{rel}" unless links.key? rewritten_rel
  links[rewritten_rel]
end

#rel?(rel) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/shaf_client/base_resource.rb', line 60

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

#to_hObject



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

def to_h
  attributes.dup.tap do |hash|
    hash[:_links] = transform_values_to_s(links)
    embedded = transform_values_to_s(embedded_resources)
    hash[:_embedded] = embedded unless embedded.empty?
  end
end

#to_sObject



28
29
30
# File 'lib/shaf_client/base_resource.rb', line 28

def to_s
  JSON.pretty_generate(to_h)
end