Sha256: 78fcb0954f1d82b6823df1f4f638e78cbdbfc6593e8b645c386dbe75280fceb8

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

class TentClient

  # Proxies to Faraday and cycles through server urls
  #   until either non left or response status in the 200s or 400s
  class CycleHTTP
    attr_reader :client, :server_urls
    def initialize(client, &faraday_block)
      @faraday_block = faraday_block
      @client = client
      @server_urls = client.server_urls.dup
    end

    def new_http
      @http = Faraday.new(:url => server_urls.shift) do |f|
        @faraday_block.call(f)
      end
    end

    def http
      @http || new_http
    end

    %w{ head get put post patch delete options }.each do |verb|
      define_method verb do |*args, &block|
        res = http.send(verb, *args, &block)
        return res unless server_urls.any?
        case res.status
        when 200...300, 400...500
          res
        else
          new_http
          send(verb, *args, &block)
        end
      end
    end

    def respond_to_missing?(method_name, include_private = false)
      http.respond_to?(method_name, include_private)
    end

    def method_missing(method_name, *args, &block)
      if http.respond_to?(method_name)
        http.send(method_name, *args, &block)
      else
        super
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
tent-client-joebadmo-0.0.1 lib/tent-client/cycle_http.rb
tent-client-0.0.1 lib/tent-client/cycle_http.rb