module Bridgetown::Builders::DSL::HTTP

Public Instance Methods

connection(headers: {}, parse_json: true) { |faraday| ... } click to toggle source
# 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| ... } click to toggle source
# 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