Sha256: c1e423936fe9c9ea9dccc7aa4945268268cf16073affd48122010298d378afff

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

require 'faraday'

module Rapidash
  module HTTPClient
    attr_accessor :login, :password
    attr_writer :connection

    def initialize(options = {})
      [:login, :password].each do |key|
        self.send("#{key.to_s}=".to_sym, options[key])
      end
    end

    def connection
      raise ConfigurationError.new "Site is required" unless site
      @connection ||= Faraday.new(site)
    end

    def request(verb, url, options = {})
      url = connection.build_url(normalize_url(url), options[:params]).to_s
      response = connection.run_request verb, url, options[:body], options[:header] do |request|
        request.headers.update(:Authorization => connection.basic_auth(login, password)) if login && password
      end

      process_response(response, verb, options)
    end

    def process_response(response, verb, options)
      # "foo"[0] does not work in 1.8.7, "foo"[0,1] is required
      case response.status.to_s[0, 1]
      when "5", "4"
        error = ResponseError.new(response)
        raise error if self.class.respond_to?(:raise_error) && self.class.raise_error
        return nil
      #Handle redirects
      when "3"
        request(verb, response.headers["location"], options)
      when "2"
        return Response.new(response)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rapidash-0.2.2 lib/rapidash/http_client.rb
rapidash-0.2.1 lib/rapidash/http_client.rb
rapidash-0.2.0 lib/rapidash/http_client.rb