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

samvera / ldp / 41b5a6ca-fe68-4193-a2c9-87e512bf0788

19 Aug 2024 06:45PM UTC coverage: 86.165%. Remained the same
41b5a6ca-fe68-4193-a2c9-87e512bf0788

push

circleci

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

492 of 571 relevant lines covered (86.16%)

51.82 hits per line

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

100.0
/lib/ldp/client/methods.rb
1
require 'faraday'
21✔
2

3
##
4
# HTTP client methods for making requests to an LDP resource and getting a response back.
5
module Ldp::Client::Methods
21✔
6
  attr_reader :http
21✔
7
  def initialize_http_client *http_client
21✔
8
    if http_client.length == 1 and http_client.first.is_a? Faraday::Connection
336✔
9
      @http = http_client.first
320✔
10
    else
11
      @http = Faraday.new *http_client
16✔
12
    end
13
    yield @http if block_given?
336✔
14
  end
15

16
  def head url
21✔
17
    Ldp.instrument("http.ldp",
100✔
18
                 url: url, name: "HEAD", ldp_client: object_id) do
19
      resp = http.head do |req|
100✔
20
        req.url munge_to_relative_url(url)
100✔
21

22
        yield req if block_given?
100✔
23
      end
24

25
      check_for_errors(resp)
100✔
26

27
      Ldp::Response.new(resp)
48✔
28
    end
29
  end
30

31
  # Get a LDP Resource by URI
32
  def get url, options = {}
21✔
33
    Ldp.instrument("http.ldp",
180✔
34
                 url: url, name: "GET", ldp_client: object_id) do
35
      resp = http.get do |req|
180✔
36
        req.url munge_to_relative_url(url)
180✔
37
        prefer_headers = ::Ldp::PreferHeaders.new
180✔
38

39
        if options[:minimal]
180✔
40
          prefer_headers.return = "minimal"
4✔
41
        else
42
          prefer_headers.return = "representation"
176✔
43
          includes = Array(options[:include]).map { |x| Ldp.send("prefer_#{x}") if Ldp.respond_to? "prefer_#{x}" }
180✔
44
          omits = Array(options[:omit]).map { |x| Ldp.send("prefer_#{x}") if Ldp.respond_to? "prefer_#{x}" }
180✔
45
          prefer_headers.include = includes
176✔
46
          prefer_headers.omit = omits
176✔
47
        end
48
        req.headers["Prefer"] = prefer_headers.to_s
180✔
49

50
        yield req if block_given?
180✔
51
      end
52

53
      check_for_errors(resp)
180✔
54

55
      Ldp::Response.new(resp)
136✔
56
    end
57
  end
58

59
  # Delete a LDP Resource by URI
60
  def delete url
21✔
61
    Ldp.instrument("http.ldp",
12✔
62
                 url: url, name: "DELETE", ldp_client: object_id) do
63
      resp = http.delete do |req|
12✔
64
        req.url munge_to_relative_url(url)
12✔
65
        yield req if block_given?
12✔
66
      end
67

68
      check_for_errors(resp)
12✔
69
    end
70
  end
71

72
  # Post TTL to an LDP Resource
73
  def post url, body = nil, headers = {}
21✔
74
    Ldp.instrument("http.ldp",
44✔
75
                 url: url, name: "POST", ldp_client: object_id) do
76
      resp = http.post do |req|
44✔
77
        req.url munge_to_relative_url(url)
44✔
78
        req.headers.merge!(default_headers).merge!(headers)
44✔
79
        req.body = body
44✔
80
        yield req if block_given?
44✔
81
      end
82
      check_for_errors(resp)
44✔
83
    end
84
  end
85

86
  # Update an LDP resource with TTL by URI
87
  def put url, body, headers = {}
21✔
88
    Ldp.instrument("http.ldp",
112✔
89
                 url: url, name: "PUT", ldp_client: object_id) do
90
      resp = http.put do |req|
112✔
91
        req.url munge_to_relative_url(url)
112✔
92
        req.headers.merge!(default_headers).merge!(headers)
112✔
93
        req.body = body
112✔
94
        yield req if block_given?
112✔
95
      end
96
      check_for_errors(resp)
112✔
97
    end
98
  end
99

100
  # Update an LDP resource with TTL by URI
101
  def patch url, body, headers = {}
21✔
102
    Ldp.instrument("http.ldp",
4✔
103
                 url: url, name: "PATCH", ldp_client: object_id) do
104
      resp = http.patch do |req|
4✔
105
        req.url munge_to_relative_url(url)
4✔
106
        req.headers.merge!(default_patch_headers).merge!(headers)
4✔
107
        req.body = body
4✔
108
        yield req if block_given?
4✔
109
      end
110
      check_for_errors(resp)
4✔
111
    end
112
  end
113

114
  private
21✔
115

116
  def check_for_errors resp
21✔
117
    resp.tap do |resp|
452✔
118
      unless resp.status < 400
452✔
119
        raise case resp.status
116✔
120
              when 400
121
                if resp.env.method == :head
12✔
122
                  # If the request was a HEAD request (which only retrieves HTTP headers),
123
                  # re-run it as a GET in order to retrieve a message body (which is passed on as the error message)
124
                  get(resp.env.url.path)
4✔
125
                else
126
                  Ldp::BadRequest.new(resp.body)
8✔
127
                end
128
              when 404
129
                Ldp::NotFound.new(resp.body)
80✔
130
              when 409
131
                Ldp::Conflict.new(resp.body)
4✔
132
              when 410
133
                Ldp::Gone.new(resp.body)
4✔
134
              when 412
135
                Ldp::PreconditionFailed.new(resp.body)
12✔
136
              else
137
                Ldp::HttpError.new("STATUS: #{resp.status} #{resp.body[0, 1000]}...")
4✔
138
              end
139
      end
140
    end
141
  end
142

143
  def default_headers
21✔
144
    { "Content-Type" => "text/turtle" }
156✔
145
  end
146

147
  def default_patch_headers
21✔
148
    { "Content-Type" => "application/sparql-update" }
4✔
149
  end
150

151
  ##
152
  # Some valid query paths can be mistaken for absolute URIs
153
  # with an alternative scheme. If the scheme isn't HTTP(S), assume
154
  # they meant a relative URI instead.
155
  def munge_to_relative_url url
21✔
156
    purl = URI.parse(url)
452✔
157
    if purl.absolute? and !((purl.scheme rescue nil) =~ /^http/)
452✔
158
      "./" + url
4✔
159
    else
160
      url
448✔
161
    end
162
  end
163
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

© 2026 Coveralls, Inc