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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'bridgetown-builder/lib/bridgetown-builder/dsl/http.rb', line 22

def connection(headers: {}, parse_json: true)
  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
# File 'bridgetown-builder/lib/bridgetown-builder/dsl/http.rb', line 9

def get(url, headers: {}, parse_json: true, **params)
  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