Class: Hyperclient::Resource

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/hyperclient/resource.rb

Overview

Represents a resource from your API. Its responsability is to perform HTTP requests against itself and ease the way you access the resource’s attributes, links and embedded resources.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Resource

Initializes a Resource.

Parameters:

  • url

    A String with the url of the resource. Can be either absolute or

  • relative.
  • options (defaults to: {})

    An options Hash to initialize different values:

Options Hash (options):

  • :name (Object)

    The String name of the resource.

  • :representation (Object)

    An optional Hash representation of the resource’s

  • :http (Object)

    An optional Hash to pass to the HTTP class.



30
31
32
33
34
35
# File 'lib/hyperclient/resource.rb', line 30

def initialize(url, options = {})
  @url = url
  @name = options[:name]
  @http = HTTP.new(self, options[:http])
  initialize_representation(options[:representation])
end

Instance Attribute Details

#nameObject (readonly)

A String representing the Resource name.



18
19
20
# File 'lib/hyperclient/resource.rb', line 18

def name
  @name
end

Class Method Details

.entry_point=(url) ⇒ void

This method returns an undefined value.

Sets the entry point for all the resources in your API client.

Parameters:

  • url

    A String with the URL of your API entry point.



42
43
44
# File 'lib/hyperclient/resource.rb', line 42

def self.entry_point=(url)
  @@entry_point = URI(url)
end

Instance Method Details

#initialize_representation(raw_representation) ⇒ void (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Initializes a Representation

Parameters:

  • raw_representation

    A Hash representing the HTTP representation for the resource.



69
70
71
72
73
# File 'lib/hyperclient/resource.rb', line 69

def initialize_representation(raw_representation)
  if raw_representation && !raw_representation.empty?
    @representation = Representation.new(raw_representation)
  end
end

#reloadObject

Gets a fresh representation from the resource representation.



58
59
60
61
# File 'lib/hyperclient/resource.rb', line 58

def reload
  initialize_representation(get)
  self
end

#representationObject (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the resource representation.



76
77
78
79
# File 'lib/hyperclient/resource.rb', line 76

def representation
  reload unless @representation
  @representation
end

#urlObject

Returns A String representing the resource url.



47
48
49
50
51
52
53
# File 'lib/hyperclient/resource.rb', line 47

def url
  begin
    @@entry_point.merge(@url).to_s
  rescue URI::InvalidURIError
    @url
  end
end