Class: Representors::Transition

Inherits:
Object
  • Object
show all
Defined in:
lib/representors/transition.rb

Overview

Manages the respresentation of link elements for hypermedia messages.

Constant Summary collapse

REL_KEY =
:rel
HREF_KEY =
:href
:links
METHOD_KEY =
:method
DESCRIPTORS_KEY =
:descriptors
DEFAULT_METHOD =
'GET'
PARAMETER_FIELDS =
'href'
ATTRIBUTE_FIELDS =
'attribute'
URL_TEMPLATE =
"%s{?%s}"

Instance Method Summary collapse

Constructor Details

#initialize(transition_hash) ⇒ Transition

Must contain at least the property :href

Examples:

hash =  {rel: "self", href: "http://example.org"}
Transition.new(hash)

Parameters:

  • the (Hash)

    abstract representor hash defining a transition



22
23
24
# File 'lib/representors/transition.rb', line 22

def initialize(transition_hash)
  @transition_hash = transition_hash
end

Instance Method Details

#[](key) ⇒ String

Returns with the value of the key.

Parameters:

  • key (String)

    on the transitions hash to retrieve

Returns:

  • (String)

    with the value of the key



49
50
51
# File 'lib/representors/transition.rb', line 49

def [](key)
  retrieve(key)
end

#attributesArray

The Parameters (i.e. POST variables)

Returns:

  • (Array)

    who’s elements are all <Crichton:Field> objects



103
104
105
# File 'lib/representors/transition.rb', line 103

def attributes
  @attributes ||= descriptors_fields.select{|field| field.scope == ATTRIBUTE_FIELDS }
end

#descriptorsArray

The Parameters (i.e. GET variables)

Returns:

  • (Array)

    who’s elements are all <Crichton:Field> objects



110
111
112
# File 'lib/representors/transition.rb', line 110

def descriptors
  @descriptions ||= (attributes + parameters)
end

#has_key?(key) ⇒ Bool

Returns false if there is no key.

Parameters:

  • key (String)

    on the transitions hash to retrieve

Returns:

  • (Bool)

    false if there is no key



55
56
57
# File 'lib/representors/transition.rb', line 55

def has_key?(key)
  !retrieve(key).nil?
end

#interface_methodString

Returns representing the Uniform Interface Method.

Returns:

  • (String)

    representing the Uniform Interface Method



78
79
80
# File 'lib/representors/transition.rb', line 78

def interface_method
  retrieve(METHOD_KEY) || DEFAULT_METHOD
end

Returns who’s elements are all <Crichton:Transition> objects.

Returns:

  • (Array)

    who’s elements are all <Crichton:Transition> objects



71
72
73
74
75
# File 'lib/representors/transition.rb', line 71

def meta_links
  @meta_links ||= (retrieve(LINKS_KEY) || []).map do |link_key, link_href|
    Transition.new({rel: link_key, href: link_href})
  end
end

#parametersArray

The Parameters (i.e. GET variables)

Variables in the URI template rules this method, we are going to return a field for each of them if we find a field inside the ‘data’ of the document describing that variable, we use that information else we return a field with default information about a variable.

Returns:

  • (Array)

    who’s elements are all <Crichton:Field> objects



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/representors/transition.rb', line 87

def parameters
  data_fields = descriptors_fields.select{|field| field.scope == PARAMETER_FIELDS }
  Addressable::Template.new(retrieve(HREF_KEY)).variables.map do |template_variable_name|
    field_specified = data_fields.find{|field| field.name.to_s == template_variable_name.to_s}
    if field_specified
      field_specified
    else
      Field.new({template_variable_name.to_sym => {type: 'string', scope: 'href'}})
    end
  end
 # descriptors_fields.select{|field| field.scope == PARAMETER_FIELDS }
end

#relString

Returns The name of the Relationship.

Returns:

  • (String)

    The name of the Relationship



37
38
39
# File 'lib/representors/transition.rb', line 37

def rel
  retrieve(REL_KEY)
end

#templated?Boolean

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/representors/transition.rb', line 65

def templated?
  # if we have any variable then it is not a templated url
  !Addressable::Template.new(retrieve(HREF_KEY)).variables.empty?
end

#templated_uriString

Returns The URI for the object templated against #parameters.

Returns:

  • (String)

    The URI for the object templated against #parameters



60
61
62
63
# File 'lib/representors/transition.rb', line 60

def templated_uri
  #URL as it is, it will be the templated URL of the document if it was templated
  retrieve(HREF_KEY)
end

#to_hashHash

Returns useful in cucumber steps where the feature file provides a hash.

Returns:

  • (Hash)

    useful in cucumber steps where the feature file provides a hash



32
33
34
# File 'lib/representors/transition.rb', line 32

def to_hash
  Hash[@transition_hash.map{ |k, v| [k.to_s, v] }]
end

#to_sString

Returns so the user can ‘puts’ this object.

Returns:

  • (String)

    so the user can ‘puts’ this object



27
28
29
# File 'lib/representors/transition.rb', line 27

def to_s
  @transition_hash.inspect
end

#uri(data = {}) ⇒ String

Returns The URI for the object.

Returns:

  • (String)

    The URI for the object



42
43
44
45
# File 'lib/representors/transition.rb', line 42

def uri(data={})
  template = Addressable::Template.new(retrieve(HREF_KEY))
  template.expand(data).to_str
end