Sha256: c421475e1c8ea271793d8cebe43a2149979e67da63d4281eb1a52e0a3e992d05

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require 'readability_parser/error'

module ReadabilityParser
  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={})

      raise ReadabilityParser::Error::ConfigurationError.new("Please configure ReadabilityParser.api_token first") if api_token.nil?

      params.merge!({
        :token => api_token,
        :format => format
      })

      connection_options = {}

      begin
        response = connection(connection_options).send(method) do |request|
          request.url(path, params)
        end
      rescue Faraday::Error::ClientError => error
        if error.is_a?(Faraday::Error::ClientError)
          raise ReadabilityParser::Error::ClientError.new(error)
        else
          raise ReadabilityParser::Error::RequestError.new(error)
        end
      end

      # When using xml format the response is wrapped in a <response> node
      if response.body['response']
        # TODO: When using xml format, the response.body hash values are all strings.
        # Maybe convert values to proper type?
        response.body['response']
      else
        response.body
      end
    end
  end # Request
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
readability_parser-0.0.3 lib/readability_parser/request.rb