Class: Representors::Transition
- Inherits:
-
Object
- Object
- Representors::Transition
- 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_KEY =
:links
- METHOD_KEY =
:method
- DESCRIPTORS_KEY =
:descriptors
- DEFAULT_METHOD =
'GET'
- PARAMETER_FIELDS =
'href'
- ATTRIBUTE_FIELDS =
'attribute'
- URL_TEMPLATE =
"%s{?%s}"
Instance Method Summary collapse
-
#[](key) ⇒ String
With the value of the key.
-
#attributes ⇒ Array
The Parameters (i.e. POST variables).
-
#descriptors ⇒ Array
The Parameters (i.e. GET variables).
-
#has_key?(key) ⇒ Bool
False if there is no key.
-
#initialize(transition_hash) ⇒ Transition
constructor
Must contain at least the property :href.
-
#interface_method ⇒ String
Representing the Uniform Interface Method.
-
#meta_links ⇒ Array
Who’s elements are all <Crichton:Transition> objects.
-
#parameters ⇒ Array
The Parameters (i.e. GET variables).
-
#rel ⇒ String
The name of the Relationship.
- #templated? ⇒ Boolean
-
#templated_uri ⇒ String
The URI for the object templated against #parameters.
-
#to_hash ⇒ Hash
Useful in cucumber steps where the feature file provides a hash.
-
#to_s ⇒ String
So the user can ‘puts’ this object.
-
#uri(data = {}) ⇒ String
The URI for the object.
Constructor Details
#initialize(transition_hash) ⇒ Transition
Must contain at least the property :href
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.
49 50 51 |
# File 'lib/representors/transition.rb', line 49 def [](key) retrieve(key) end |
#attributes ⇒ Array
The Parameters (i.e. POST variables)
103 104 105 |
# File 'lib/representors/transition.rb', line 103 def attributes @attributes ||= descriptors_fields.select{|field| field.scope == ATTRIBUTE_FIELDS } end |
#descriptors ⇒ Array
The Parameters (i.e. GET variables)
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.
55 56 57 |
# File 'lib/representors/transition.rb', line 55 def has_key?(key) !retrieve(key).nil? end |
#interface_method ⇒ String
Returns representing the Uniform Interface Method.
78 79 80 |
# File 'lib/representors/transition.rb', line 78 def interface_method retrieve(METHOD_KEY) || DEFAULT_METHOD end |
#meta_links ⇒ Array
Returns who’s elements are all <Crichton:Transition> objects.
71 72 73 74 75 |
# File 'lib/representors/transition.rb', line 71 def @meta_links ||= (retrieve(LINKS_KEY) || []).map do |link_key, link_href| Transition.new({rel: link_key, href: link_href}) end end |
#parameters ⇒ Array
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.
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 |
#rel ⇒ String
Returns The name of the Relationship.
37 38 39 |
# File 'lib/representors/transition.rb', line 37 def rel retrieve(REL_KEY) end |
#templated? ⇒ 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_uri ⇒ String
Returns 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_hash ⇒ Hash
Returns 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_s ⇒ String
Returns 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.
42 43 44 45 |
# File 'lib/representors/transition.rb', line 42 def uri(data={}) template = Addressable::Template.new(retrieve(HREF_KEY)) template.(data).to_str end |