Class: Klient::Resource

Inherits:
Object
  • Object
show all
Extended by:
ResourceMethods
Defined in:
lib/klient/resource.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ResourceMethods

collection, default_collection_accessor, resource, resources

Constructor Details

#initialize(parent) ⇒ Resource

Returns a new instance of Resource.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/klient/resource.rb', line 15

def initialize(parent)
  @root = parent.root
  @header_proc = parent.header_proc
  @regexp = /#{self.class.name.demodulize.underscore.gsub(/(_|-|\s+)/, "(_|-|\s*)")}/i
  @id = self.class.try(:id)
  @collection_accessor = @identifier = self.class.try(:identifier)

  if @id
    @url_arguments = {@id => @identifier}
  else
    @url_arguments = {}
  end

  @parent = parent
  @headers = {}

  @url_template = Addressable::Template.new(
    @parent.url + self.class.url_template.pattern
  )
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mth, *args, &block) ⇒ Object (private)



106
107
108
109
110
111
112
# File 'lib/klient/resource.rb', line 106

def method_missing(mth, *args, &block)
  if @parsed_body.respond_to?(mth)
    @parsed_body.send(mth, *args, &block)
  else
    @last_response.send(mth, *args, &block)
  end
end

Class Attribute Details

.argumentsObject

Returns the value of attribute arguments.



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

def arguments
  @arguments
end

.collection_accessorObject (readonly)

Returns the value of attribute collection_accessor.



9
10
11
# File 'lib/klient/resource.rb', line 9

def collection_accessor
  @collection_accessor
end

.idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/klient/resource.rb', line 9

def id
  @id
end

.identifierObject (readonly)

Returns the value of attribute identifier.



9
10
11
# File 'lib/klient/resource.rb', line 9

def identifier
  @identifier
end

.mappingObject (readonly)

Returns the value of attribute mapping.



9
10
11
# File 'lib/klient/resource.rb', line 9

def mapping
  @mapping
end

.resource_typeObject

Returns the value of attribute resource_type.



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

def resource_type
  @resource_type
end

.typeObject

Returns the value of attribute type.



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

def type
  @type
end

.url_templateObject (readonly)

Returns the value of attribute url_template.



9
10
11
# File 'lib/klient/resource.rb', line 9

def url_template
  @url_template
end

Instance Attribute Details

#collection_accessorObject (readonly)

Returns the value of attribute collection_accessor.



6
7
8
# File 'lib/klient/resource.rb', line 6

def collection_accessor
  @collection_accessor
end

#header_procObject (readonly)

Returns the value of attribute header_proc.



6
7
8
# File 'lib/klient/resource.rb', line 6

def header_proc
  @header_proc
end

#headersObject (readonly)

Returns the value of attribute headers.



6
7
8
# File 'lib/klient/resource.rb', line 6

def headers
  @headers
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/klient/resource.rb', line 6

def id
  @id
end

#last_responseObject (readonly)

Returns the value of attribute last_response.



6
7
8
# File 'lib/klient/resource.rb', line 6

def last_response
  @last_response
end

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/klient/resource.rb', line 6

def parent
  @parent
end

#rootObject (readonly)

Returns the value of attribute root.



6
7
8
# File 'lib/klient/resource.rb', line 6

def root
  @root
end

#statusObject (readonly)

Returns the value of attribute status.



6
7
8
# File 'lib/klient/resource.rb', line 6

def status
  @status
end

#urlObject (readonly)

Returns the value of attribute url.



6
7
8
# File 'lib/klient/resource.rb', line 6

def url
  @url
end

#url_argumentsObject (readonly)

Returns the value of attribute url_arguments.



6
7
8
# File 'lib/klient/resource.rb', line 6

def url_arguments
  @url_arguments
end

#url_templateObject (readonly)

Returns the value of attribute url_template.



6
7
8
# File 'lib/klient/resource.rb', line 6

def url_template
  @url_template
end

Instance Method Details

#inspectObject



36
37
38
# File 'lib/klient/resource.rb', line 36

def inspect
  "#<#{self.class.name}:#{object_id} @url=#{self.url.inspect} @status_code=#{self.last_response.try(:status) || 'nil'}>"
end