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

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

Instance Method Summary collapse

Instance Method Details

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



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

def connection(headers: {}, parse_json: true)
  headers["Content-Type"] = "application/json" if parse_json

  Faraday.new(headers: headers) do |faraday|
    faraday.use FaradayMiddleware::FollowRedirects
    if parse_json
      faraday.use FaradayMiddleware::ParseJson, parser_options: {
        object_class: HashWithDotAccess::Hash,
      }
    end
    yield faraday if block_given?
  end
end

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

Yields:

  • (body)


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

def get(url, headers: {}, parse_json: true)
  body = begin
           connection(parse_json: parse_json).get(url, headers: headers).body
         rescue Faraday::ParsingError
           Bridgetown.logger.error(
             "Faraday::ParsingError",
             "The response from #{url} did not contain valid JSON"
           )
           nil
         end
  yield body
end