Class: Dydra::Resource

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/dydra/resource.rb

Overview

Represents a Dydra.com resource.

This is the base class for all classes that represent dereferenceable HTTP resources on Dydra.com.

See Also:

Direct Known Subclasses

Account, Job, Query, Repository, Revision

Constant Summary

HEADERS =

N-Triples

{'Accept' => 'text/plain'}

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Resource) initialize(url)

A new instance of Resource

Parameters:

  • url (RDF::URI, String)


38
39
40
# File 'lib/dydra/resource.rb', line 38

def initialize(url)
  @url = RDF::URI.new(url)
end

Instance Attribute Details

- (RDF::URI) url (readonly)

Returns:

  • (RDF::URI)


34
35
36
# File 'lib/dydra/resource.rb', line 34

def url
  @url
end

Class Method Details

+ (Resource) new(*args, &block)

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dydra/resource.rb', line 19

def self.new(*args, &block)
  if self == Resource
    case spec = args.first
      when Repository::SPEC
        Repository.new(*spec.split('/'))
      when Account::SPEC
        Account.new(spec)
    end
  else
    super
  end
end

Instance Method Details

- (Integer) <=>(other)

Compares this resources to the given other resource.

Parameters:

  • other (Object)

Returns:

  • (Integer)

    -1, 0, or 1



78
79
80
# File 'lib/dydra/resource.rb', line 78

def <=>(other)
  self.to_uri <=> other.to_uri
end

- (Boolean) eql?(other)

Returns true if this resource is equal to the given other resource.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


69
70
71
# File 'lib/dydra/resource.rb', line 69

def eql?(other)
  other.class.eql?(self.class) && self == other
end

- (Boolean) exists?

Returns true if this resource exists on Dydra.com.

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
# File 'lib/dydra/resource.rb', line 54

def exists?
  # to bangbang or not to bangbang...
  begin
    !!info
  rescue RestClient::ResourceNotFound
    return false
  end
end

- (Net::HTTPResponse) get(format = nil, headers = {}) {|response| ... }

Performs an HTTP GET request on this resource.

Parameters:

  • format (String, #to_s) (defaults to: nil)
  • headers (Hash{String => String}) (defaults to: {})

Yields:

  • (response)

Yield Parameters:

  • response (Net::HTTPResponse)

Returns:

  • (Net::HTTPResponse)


148
149
150
151
152
153
154
155
156
157
# File 'lib/dydra/resource.rb', line 148

def get(format = nil, headers = {}, &block)
  Net::HTTP.start(url.host, url.port) do |http|
    response = http.get(url.path.to_s + format.to_s, HEADERS.merge(headers))
    if block_given?
      block.call(response)
    else
      response
    end
  end
end

- (Net::HTTPResponse) head(format = nil, headers = {}) {|response| ... }

Performs an HTTP HEAD request on this resource.

Parameters:

  • format (String, #to_s) (defaults to: nil)
  • headers (Hash{String => String}) (defaults to: {})

Yields:

  • (response)

Yield Parameters:

  • response (Net::HTTPResponse)

Returns:

  • (Net::HTTPResponse)


129
130
131
132
133
134
135
136
137
138
# File 'lib/dydra/resource.rb', line 129

def head(format = nil, headers = {}, &block)
  Net::HTTP.start(url.host, url.port) do |http|
    response = http.head(url.path.to_s + format.to_s, HEADERS.merge(headers))
    if block_given?
      block.call(response)
    else
      response
    end
  end
end

- (String) inspect

Returns a developer-friendly representation of this resource.

Returns:

  • (String)


108
109
110
# File 'lib/dydra/resource.rb', line 108

def inspect
  sprintf("#<%s:%#0x(%s)>", self.class.name, object_id, to_s)
end

- inspect!

This method returns an undefined value.

Outputs a developer-friendly representation of this resource to stderr.



117
118
119
# File 'lib/dydra/resource.rb', line 117

def inspect!
  warn(inspect)
end

- (String) path

Returns the root-relative path of this resource.

Returns:

  • (String)


46
47
48
# File 'lib/dydra/resource.rb', line 46

def path
  url.path[1..-1]
end

- (RDF::Enumerable) to_rdf

Returns the RDF data for this resource.

Returns:

  • (RDF::Enumerable)


94
95
96
97
98
99
100
101
102
# File 'lib/dydra/resource.rb', line 94

def to_rdf
  get('.nt', 'Accept' => 'text/plain') do |response|
    case response
      when Net::HTTPSuccess
        reader = RDF::NTriples::Reader.new(response.body)
        reader.to_a.extend(RDF::Enumerable, RDF::Queryable) # FIXME
    end
  end
end

- (RDF::URI) to_uri

Returns the URL of this resource.

Returns:

  • (RDF::URI)


86
87
88
# File 'lib/dydra/resource.rb', line 86

def to_uri
  url
end