• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

samvera / ldp / 7aae4f98-f021-4aa3-ade3-5b1586e22f3d

19 Aug 2024 06:45PM UTC coverage: 86.165%. Remained the same
7aae4f98-f021-4aa3-ade3-5b1586e22f3d

push

circleci

web-flow
Test with latest ruby and rails versions (#156)

492 of 571 relevant lines covered (86.16%)

38.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.63
/lib/ldp/resource.rb
1
module Ldp
16✔
2
  class Resource
16✔
3
    require 'ldp/resource/binary_source'
16✔
4
    require 'ldp/resource/rdf_source'
16✔
5

6
    attr_reader :client, :subject
16✔
7
    attr_accessor :content
16✔
8

9
    def self.for(client, subject, response)
16✔
10
      case
11
      when response.container?
24✔
12
        Ldp::Container.for client, subject, response
12✔
13
      when response.rdf_source?
14
        Resource::RdfSource.new client, subject, response
3✔
15
      else
16
        Resource::BinarySource.new client, subject, response
9✔
17
      end
18
    end
19

20
    def initialize client, subject, response = nil, base_path = ''
16✔
21
      @client = client
189✔
22
      @subject = subject
189✔
23
      @get = response if response.is_a? Faraday::Response and current? response
189✔
24
      @base_path = base_path
189✔
25
    end
26

27
    ##
28
    # Get the graph subject as a URI
29
    def subject_uri
16✔
30
      @subject_uri ||= RDF::URI(subject)
36✔
31
    end
32

33
    ##
34
    # Reload the LDP resource
35
    def reload
16✔
36
      self.class.new client, subject, @get
45✔
37
    end
38

39
    ##
40
    # Is the resource new, or does it exist in the LDP server?
41
    def new?
16✔
42
      subject.nil? || head == None
72✔
43
    end
44

45
    ##
46
    # Have we retrieved the content already?
47
    def retrieved_content?
16✔
48
      @get
18✔
49
    end
50

51
    ##
52
    # Get the resource
53
    def get
16✔
54
      @get ||= client.get(subject)
75✔
55
    end
56

57
    def head
16✔
58
      @head ||= begin
78✔
59
        @get || client.head(subject)
69✔
60
                rescue Ldp::NotFound
61
                  None
39✔
62
      end
63
    end
64

65
    ##
66
    # Delete the resource
67
    def delete
16✔
68
      client.delete subject do |req|
×
69
        req.headers['If-Unmodified-Since'] = get.last_modified if retrieved_content?
×
70
      end
71
    end
72

73
    def save
16✔
74
      new? ? create : update
18✔
75
    end
76

77
    ##
78
    # Create a new resource at the URI
79
    # @return [RdfSource] the new representation
80
    # @raise [Ldp::Conflict] if you attempt to call create on an existing resource
81
    def create &block
16✔
82
      raise Ldp::Conflict, "Can't call create on an existing resource (#{subject})" unless new?
42✔
83
      verb = subject.nil? ? :post : :put
39✔
84
      resp = client.send(verb, (subject || @base_path), content) do |req|
39✔
85
        req.headers["Link"] = "<#{interaction_model}>;rel=\"type\"" if interaction_model
39✔
86
        yield req if block_given?
39✔
87
      end
88

89
      @subject = resp.headers['Location']
39✔
90
      @subject_uri = nil
39✔
91
      reload
39✔
92
    end
93

94
    ##
95
    # Update the stored graph
96
    def update new_content = nil
16✔
97
      new_content ||= content
12✔
98
      resp = client.put subject, new_content do |req|
12✔
99
        req.headers['If-Unmodified-Since'] = get.last_modified if retrieved_content?
12✔
100
        yield req if block_given?
12✔
101
      end
102
      update_cached_get(resp) if retrieved_content?
6✔
103
      resp
6✔
104
    end
105

106
    def current? response = nil
16✔
107
      response ||= @get
×
108
      return true if new? and subject.nil?
×
109

110
      new_response = client.head(subject)
×
111

112
      response.headers['ETag'] &&
×
113
        response.headers['Last-Modified'] &&
114
        new_response.headers['ETag'] == response.headers['ETag'] &&
115
        new_response.headers['Last-Modified'] == response.headers['Last-Modified']
116
    end
117

118
    def update_cached_get(response)
16✔
119
      response = Response.new(response)
3✔
120

121
      if response.etag.nil? || response.last_modified.nil?
3✔
122
        response = client.head(subject)
3✔
123
      end
124
      @get.etag = response.etag
3✔
125
      @get.last_modified = response.last_modified
3✔
126
    end
127

128
    protected
16✔
129

130
    def interaction_model
16✔
131
      nil
132
    end
133
  end
134
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc