Sha256: 1e385730a459daf15a9b4ffb003c2500f3e67ce9a13238e38ac7f6fbc5205a42

Contents?: true

Size: 1.43 KB

Versions: 4

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
      daddy_dir = File.expand_path('../../..', __FILE__)
      ca_path = File.join(daddy_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

4 entries across 4 versions & 1 rubygems

Version Path
daddy-0.3.17 lib/daddy/http_client.rb
daddy-0.3.16 lib/daddy/http_client.rb
daddy-0.3.15 lib/daddy/http_client.rb
daddy-0.3.14 lib/daddy/http_client.rb