Class: Datagraph::Resource

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

Overview

Base class for Datagraph.org resources.

Direct Known Subclasses

Account, Process, Query, Repository

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:

  • (RDF::URI, String) url


30
31
32
# File 'lib/datagraph/resource.rb', line 30

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

Instance Attribute Details

- (RDF::URI) url (readonly)

Returns:

  • (RDF::URI)


26
27
28
# File 'lib/datagraph/resource.rb', line 26

def url
  @url
end

Class Method Details

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



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/datagraph/resource.rb', line 11

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:

  • (Object) other

Returns:

  • (Integer)

    -1, 0, or 1



63
64
65
# File 'lib/datagraph/resource.rb', line 63

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:

  • (Object) other

Returns:

  • (Boolean)


54
55
56
# File 'lib/datagraph/resource.rb', line 54

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

- (Boolean) exists?

Returns true if this resource exists on Datagraph.org.

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
# File 'lib/datagraph/resource.rb', line 38

def exists?
  head do |response|
    case response
      when Net::HTTPSuccess     then true
      when Net::HTTPClientError then false
      else true # FIXME: dubious default, for now
    end
  end
end

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

Performs an HTTP GET request on this resource.

Parameters:

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

Yields:

  • (response)

Yield Parameters:

  • (Net::HTTPResponse) response

Returns:

  • (Net::HTTPResponse)


131
132
133
134
135
136
137
138
139
140
# File 'lib/datagraph/resource.rb', line 131

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:

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

Yields:

  • (response)

Yield Parameters:

  • (Net::HTTPResponse) response

Returns:

  • (Net::HTTPResponse)


112
113
114
115
116
117
118
119
120
121
# File 'lib/datagraph/resource.rb', line 112

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)


91
92
93
# File 'lib/datagraph/resource.rb', line 91

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.



100
101
102
# File 'lib/datagraph/resource.rb', line 100

def inspect!
  warn(inspect)
end

- (Object) to_rdf

Returns the RDF data for this resource.



77
78
79
80
81
82
83
84
85
# File 'lib/datagraph/resource.rb', line 77

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)


71
72
73
# File 'lib/datagraph/resource.rb', line 71

def to_uri
  url
end