Module: Bridgetown::Builders::DSL::HTTP

Included in:
PluginBuilder
Defined in:
bridgetown-builder/lib/bridgetown-builder/dsl/http.rb

Instance Method Summary collapse

Instance Method Details

#connection(headers: {}, parse_json: true) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'bridgetown-builder/lib/bridgetown-builder/dsl/http.rb', line 27

def connection(headers: {}, parse_json: true)
  callers = caller_locations.map { [_1.base_label, _1.path] }
  this_file_path = "bridgetown-builder/lib/bridgetown-builder/dsl/http.rb"
  called_from_get = callers.any? do |base_label, path|
    base_label == "get" && path.end_with?(this_file_path)
  end
  unless called_from_get
    Deprecator.deprecation_message(
      "The DSL for HTTP requests (`connection`) is deprecated and will be " \
      "removed in Bridgetown 3.0. Use an HTTP client such as HTTPX: " \
      "https://honeyryderchuck.gitlab.io/httpx"
    )
  end

  headers["Content-Type"] = "application/json" if parse_json

  Faraday.new(headers:) do |faraday|
    faraday.response :follow_redirects

    if parse_json
      faraday.response :json, parser_options: {
        object_class: HashWithDotAccess::Hash,
      }
    end

    yield faraday if block_given?
  end
end

#get(url, headers: {}, parse_json: true, **params) {|body| ... } ⇒ Object

Yields:

  • (body)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'bridgetown-builder/lib/bridgetown-builder/dsl/http.rb', line 9

def get(url, headers: {}, parse_json: true, **params)
  Deprecator.deprecation_message(
    "The DSL for HTTP requests (`get`) is deprecated and will be " \
    "removed in Bridgetown 3.0. Use an HTTP client such as HTTPX: " \
    "https://honeyryderchuck.gitlab.io/httpx"
  )
  body = begin
    connection(parse_json:).get(url, params, headers).body
  rescue Faraday::ParsingError
    Bridgetown.logger.error(
      "Faraday::ParsingError",
      "The response from #{url} did not contain valid JSON"
    )
    nil
  end
  yield body
end