Sha256: 77964f2eb5dbbe0c902739e2de0e4ca2e55dda941479513ddd1a0875e951e3b5

Contents?: true

Size: 1009 Bytes

Versions: 2

Compression:

Stored size: 1009 Bytes

Contents

require 'mercury_web_parser/error'

module MercuryWebParser
  module Request
    # Performs a HTTP Get request
    def get(path, params = {})
      request(:get, path, params)
    end

    private

    # Returns a Faraday::Response object
    #
    # @return [Faraday::Response]
    def request(method, path, params = {})
      if api_token.nil?
        raise MercuryWebParser::Error::ConfigurationError,
              'Please configure MercuryWebParser.api_token first'
      end

      make_request(method, path, params)
    end

    def make_request(method, path, params)
      response = connection.send(method) do |request|
        request.url(path, params)
      end
      response.body
    rescue Faraday::ParsingError
      raise MercuryWebParser::Error::NotParseable, 'Unparseable response'
    rescue Faraday::Error::ClientError => error
      raise MercuryWebParser::Error::ClientError, error
    rescue => error
      raise MercuryWebParser::Error::RequestError, error
    end
  end # Request
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mercury_web_parser-0.1.2 lib/mercury_web_parser/request.rb
mercury_web_parser-0.1.0 lib/mercury_web_parser/request.rb