Class: Restify::Resource

Inherits:
Object
  • Object
show all
Includes:
Relations
Defined in:
lib/restify/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Relations

#rel, #rel?, #relations

Constructor Details

#initialize(client, relations = {}, attributes = {}, response = nil) ⇒ Resource

Returns a new instance of Resource.



83
84
85
86
87
88
# File 'lib/restify/resource.rb', line 83

def initialize(client, relations = {}, attributes = {}, response = nil)
  @client     = client
  @response   = response
  @relations  = HashWithIndifferentAccess.new relations
  @attributes = HashWithIndifferentAccess.new attributes
end

Instance Attribute Details

#media_typeMediaType (readonly)

Return content Media-Type.

Returns:

  • (MediaType)

    Resource media type.



10
11
12
# File 'lib/restify/resource.rb', line 10

def media_type
  @media_type
end

Class Method Details

.create(client, data, response) ⇒ Object

Build a resource from given response.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/restify/resource.rb', line 94

def create(client, data, response)
  relations = {}
  relations = response.relations(client) if response

  hash = {}
  if data
    data.each do |key, value|
      hash[key] = case value
                  when Array
                    Collection.create(client, value, nil)
                  when Hash
                    Resource.create(client, value, nil)
                  else
                    value
                  end
    end

    data.keys.each do |key|
      name = nil
      if (m = /\A(\w+)_url\z/.match(key))
        name = m[1]
      elsif key == 'url'
        name = :self
      else
        next
      end

      unless relations.key?(name) || data[key].to_s.blank?
        relations[name] = Relation.new(client, data[key].to_s)
      end
    end
  end

  new client, relations, hash, response
end

Instance Method Details

#[](key) ⇒ Object

Retrieve value for given key.

Parameters:

  • key (String, Symbol)

    Data key.

Returns:

  • (Object)

    Data value for given key.



18
# File 'lib/restify/resource.rb', line 18

delegate :[], to: :attributes

#attributesHashWithIndifferentAccess

Return parsed resource attributes as hash.

Returns:

  • (HashWithIndifferentAccess)

    Attribute hash.



50
51
52
# File 'lib/restify/resource.rb', line 50

def attributes
  @attributes ||= HashWithIndifferentAccess.new
end

#codeFixnum

Return response status code if available.

Returns:

  • (Fixnum)

    Response status code.

See Also:



68
# File 'lib/restify/resource.rb', line 68

delegate :code, to: :@response, allow_nil: true

#each {|key, value| ... } ⇒ Object #eachEnumerator

Iterate over keys and values or return enumerator.

Overloads:

  • #each {|key, value| ... } ⇒ Object

    Calls block once for each key, passing the key-value pair as parameters.

    Yields:

    • (key, value)

      Yield for each key-value pair.

    Yield Parameters:

    • key (String)

      Attribute key.

    • value (Object)

      Attribute value.

  • #eachEnumerator

    Return enumerator for each key-value pair.

    Returns:

    • (Enumerator)

      Enumerator for each key-value pair.



44
# File 'lib/restify/resource.rb', line 44

delegate :each, to: :attributes

#followObligation<Resource>

Follow the Location header from the response of this resource if available.

Returns:

  • (Obligation<Resource>)

    Followed resource.



75
76
77
78
79
80
81
# File 'lib/restify/resource.rb', line 75

def follow
  if @response && @response.headers['LOCATION']
    @client.request :get, @response.headers['LOCATION']
  else
    raise RuntimeError.new 'Nothing to follow.'
  end
end

#key?(name) ⇒ Boolean

Check if resource has given key.

Parameters:

  • name (String, Symbol)

    Key name.

Returns:

  • (Boolean)

    True if resource contains key, false otherwise.



26
# File 'lib/restify/resource.rb', line 26

delegate :key?, :has_key?, to: :attributes

#statusSymbol

Return response status if available.

Returns:

  • (Symbol)

    Response status.

See Also:



60
# File 'lib/restify/resource.rb', line 60

delegate :status, to: :@response, allow_nil: true