Module: Ldp::Client::Methods

Included in:
Ldp::Client
Defined in:
lib/ldp/client/methods.rb

Overview

HTTP client methods for making requests to an LDP resource and getting a response back.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



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

def http
  @http
end

Instance Method Details

#delete(url) ⇒ Object

Delete a LDP Resource by URI



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ldp/client/methods.rb', line 60

def delete url
  Ldp.instrument("http.ldp",
               url: url, name: "DELETE", ldp_client: object_id) do
    resp = http.delete do |req|
      req.url munge_to_relative_url(url)
      yield req if block_given?
    end

    check_for_errors(resp)
  end
end

#get(url, options = {}) ⇒ Object

Get a LDP Resource by URI



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ldp/client/methods.rb', line 32

def get url, options = {}
  Ldp.instrument("http.ldp",
               url: url, name: "GET", ldp_client: object_id) do
    resp = http.get do |req|
      req.url munge_to_relative_url(url)
      prefer_headers = ::Ldp::PreferHeaders.new

      if options[:minimal]
        prefer_headers.return = "minimal"
      else
        prefer_headers.return = "representation"
        includes = Array(options[:include]).map { |x| Ldp.send("prefer_#{x}") if Ldp.respond_to? "prefer_#{x}" }
        omits = Array(options[:omit]).map { |x| Ldp.send("prefer_#{x}") if Ldp.respond_to? "prefer_#{x}" }
        prefer_headers.include = includes
        prefer_headers.omit = omits
      end
      req.headers["Prefer"] = prefer_headers.to_s

      yield req if block_given?
    end

    check_for_errors(resp)

    Ldp::Response.new(resp)
  end
end

#head(url) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ldp/client/methods.rb', line 16

def head url
  Ldp.instrument("http.ldp",
               url: url, name: "HEAD", ldp_client: object_id) do
    resp = http.head do |req|
      req.url munge_to_relative_url(url)

      yield req if block_given?
    end

    check_for_errors(resp)

    Ldp::Response.new(resp)
  end
end

#initialize_http_client(*http_client) {|@http| ... } ⇒ Object

Yields:



7
8
9
10
11
12
13
14
# File 'lib/ldp/client/methods.rb', line 7

def initialize_http_client *http_client
  if http_client.length == 1 and http_client.first.is_a? Faraday::Connection
    @http = http_client.first
  else
    @http = Faraday.new *http_client
  end
  yield @http if block_given?
end

#patch(url, body, headers = {}) ⇒ Object

Update an LDP resource with TTL by URI



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ldp/client/methods.rb', line 101

def patch url, body, headers = {}
  Ldp.instrument("http.ldp",
               url: url, name: "PATCH", ldp_client: object_id) do
    resp = http.patch do |req|
      req.url munge_to_relative_url(url)
      req.headers.merge!(default_patch_headers).merge!(headers)
      req.body = body
      yield req if block_given?
    end
    check_for_errors(resp)
  end
end

#post(url, body = nil, headers = {}) ⇒ Object

Post TTL to an LDP Resource



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ldp/client/methods.rb', line 73

def post url, body = nil, headers = {}
  Ldp.instrument("http.ldp",
               url: url, name: "POST", ldp_client: object_id) do
    resp = http.post do |req|
      req.url munge_to_relative_url(url)
      req.headers.merge!(default_headers).merge!(headers)
      req.body = body
      yield req if block_given?
    end
    check_for_errors(resp)
  end
end

#put(url, body, headers = {}) ⇒ Object

Update an LDP resource with TTL by URI



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ldp/client/methods.rb', line 87

def put url, body, headers = {}
  Ldp.instrument("http.ldp",
               url: url, name: "PUT", ldp_client: object_id) do
    resp = http.put do |req|
      req.url munge_to_relative_url(url)
      req.headers.merge!(default_headers).merge!(headers)
      req.body = body
      yield req if block_given?
    end
    check_for_errors(resp)
  end
end