Sha256: 35d6cfae68a6653226c536a5746ea9d277e1ea607458b12a83789fa100c1e0fb

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

module Relax
  class Performer
    def initialize(method, url, values, options={})
      @method = method
      @url = url
      @values = values
      @logger = options.delete(:logger)
      @credentials = options.delete(:credentials)
      @proxy = options.delete(:proxy)
      @options = options

      parse_url_tokens
    end

    def perform
      RestClient.proxy = @proxy if @proxy

      case @method
        when :delete, :get, :head
          @logger.info "#{@method.to_s.upcase} #{url}" if @logger
          RestClient.send(@method, url)
        when :post, :put
          @logger.info "#{@method.to_s.upcase} #{url}\n#{query}" if @logger
          RestClient.send(@method, url, query)
      end
    end

    def url
      url = @url.gsub(/\:[a-z_]+/) do |name|
        @url_values[name[1..-1].to_sym]
      end

      uri = URI.parse(url)
      uri.query = query unless query.nil? || query.empty?
      uri.userinfo = @credentials.join(':') if @credentials
      uri.to_s
    end
    private :url

    def query
      @values.collect do |name, value|
        "#{name}=#{value}" if value || @options[:include_blank_values]
      end.compact.join('&')
    end
    private :query

    def parse_url_tokens
      tokens = @url.scan(/(?:\:)([a-z_]+)/).flatten

      @url_values = tokens.inject({}) do |values, name|
        name = name.to_sym
        values[name] = @values.delete(name) if @values.key?(name)
        values
      end
    end
    private :parse_url_tokens
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
tylerhunt-relax-0.1.3 lib/relax/performer.rb
relax-0.1.3 lib/relax/performer.rb