Class: Ldp::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/ldp/resource.rb

Direct Known Subclasses

BinarySource, RdfSource

Defined Under Namespace

Classes: BinarySource, RdfSource

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, subject, response = nil, base_path = '') ⇒ Resource

Returns a new instance of Resource.



9
10
11
12
13
14
# File 'lib/ldp/resource.rb', line 9

def initialize client, subject, response = nil, base_path = ''
  @client = client
  @subject = subject
  @get = response if response.is_a? Faraday::Response and current? response
  @base_path = base_path
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#contentObject

Returns the value of attribute content.



7
8
9
# File 'lib/ldp/resource.rb', line 7

def content
  @content
end

#subjectObject (readonly)

Returns the value of attribute subject.



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

def subject
  @subject
end

Instance Method Details

#create(&block) ⇒ RdfSource

Create a new resource at the URI

Returns:



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ldp/resource.rb', line 69

def create &block
  raise "Can't call create on an existing resource" unless new?
  verb = subject.nil? ? :post : :put
  resp = client.send(verb, (subject || @base_path), content) do |req|

    yield req if block_given?
  end

  @subject = resp.headers['Location']
  @subject_uri = nil
  reload
end

#current?(response = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

def current? response = nil
  response ||= @get
  return true if new? and subject.nil?

  new_response = client.head(subject)

  response.headers['ETag'] &&
    response.headers['Last-Modified'] &&
    new_response.headers['ETag'] == response.headers['ETag'] &&
    new_response.headers['Last-Modified'] == response.headers['Last-Modified']
end

#deleteObject

Delete the resource



56
57
58
59
60
# File 'lib/ldp/resource.rb', line 56

def delete
  client.delete subject do |req|
    req.headers['If-Match'] = get.etag if retrieved_content?
  end
end

#getObject

Get the resource



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

def get
  @get ||= client.get(subject)
end

#headObject



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

def head
  @head ||= begin
    @get || client.head(subject)
  rescue Ldp::NotFound
    None
  end
end

#new?Boolean

Is the resource new, or does it exist in the LDP server?

Returns:

  • (Boolean)


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

def new?
  subject.nil? || head == None
end

#reloadObject

Reload the LDP resource



24
25
26
# File 'lib/ldp/resource.rb', line 24

def reload
  self.class.new client, subject, @get
end

#retrieved_content?Boolean

Have we retrieved the content already?

Returns:

  • (Boolean)


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

def retrieved_content?
  @get
end

#saveObject



62
63
64
# File 'lib/ldp/resource.rb', line 62

def save
  new? ? create : update
end

#subject_uriObject

Get the graph subject as a URI



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

def subject_uri
  @subject_uri ||= RDF::URI.new subject
end

#update(new_content = nil) ⇒ Object

Update the stored graph



84
85
86
87
88
89
90
91
92
# File 'lib/ldp/resource.rb', line 84

def update new_content = nil
  new_content ||= content
  resp = client.put subject, new_content do |req|
    req.headers['If-Match'] = get.etag if retrieved_content?
    yield req if block_given?
  end
  update_cached_get(resp) if retrieved_content?
  resp
end

#update_cached_get(response) ⇒ Object



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

def update_cached_get response
  Response.wrap(client, response)
  if response.etag.nil? || response.last_modified.nil?
    response = Response.wrap(client, client.head(subject))
  end
  @get.etag = response.etag
  @get.last_modified = response.last_modified
end