Sha256: e237ddf71150bd6c2d04bae2b3256f683da719aaab2ad88b8bb131289efd501c

Contents?: true

Size: 1.43 KB

Versions: 8

Compression:

Stored size: 1.43 KB

Contents

require 'faraday'

module Daddy
  
  class HttpClient
  
    def initialize(url, options = {})
      @url = url
      @options = options
      @cookie = options[:cookie]
    end
    
    def get(path, params = {})
      response = connection.get(path, params) do |request|
        if @options[:auth_user] and @options[:auth_password]
          basic = 'Basic ' + Base64.encode64(@options[:auth_user] + ':' + @options[:auth_password])
          request.headers['Authorization'] = basic
        end
        
        if @cookie
          request.headers['Cookie'] = @cookie
        end
        
        params.each do |key, value|
          request.params[key] = value
        end
      end
      
      @cookie = response.headers['set-cookie']
      
      if block_given?
        yield response
      else
        response.body.force_encoding('UTF-8')
      end
    end
  
    def post(path, params = {})
      response = connection.post(path, params)
    end
    
    private
  
    def connection
      Faraday.new(:url => @url, :ssl => ssl_options) do |faraday|
        faraday.request :url_encoded
        faraday.adapter Faraday.default_adapter
      end
    end
    
    def ssl_options
      lib_dir = File.expand_path('../..', __FILE__)
      ca_path = File.join(lib_dir, 'ssl')
  
      ret = {
        :ca_path => ca_path,
        :ca_file => File.join(ca_path, 'cert.pem'),
        :verify => @options[:verify_ssl] || false,
      }
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
daddy-0.3.13 lib/daddy/http_client.rb
daddy-0.3.12 lib/daddy/http_client.rb
daddy-0.3.11 lib/daddy/http_client.rb
daddy-0.3.10 lib/daddy/http_client.rb
daddy-0.3.9 lib/daddy/http_client.rb
daddy-0.3.8 lib/daddy/http_client.rb
daddy-0.3.7 lib/daddy/http_client.rb
daddy-0.3.6 lib/daddy/http_client.rb