Sha256: 5b608d73103ee20feae86097d57be9375a98adf9b7df0fa5b5362a3e9803b410

Contents?: true

Size: 1.98 KB

Versions: 13

Compression:

Stored size: 1.98 KB

Contents

module PivotalTracker

  class Client

    class NoToken < StandardError; end

    class << self
      attr_writer :use_ssl, :token, :tracker_host

      def use_ssl
        @use_ssl || false
      end

      def token(username, password, method='post')
        response = if method == 'post'
          RestClient.post api_ssl_url + '/tokens/active', :username => username, :password => password
        else
          RestClient.get "#{api_ssl_url(username, password)}/tokens/active"
        end
        @token= Nokogiri::XML(response.body).search('guid').inner_html
      end

      # this is your connection for the entire module
      def connection(options={})
        raise NoToken if @token.to_s.empty?

        @connections ||= {}

        cached_connection? && !protocol_changed? ? cached_connection : new_connection
      end

      def clear_connections
        @connections = nil
      end

      def tracker_host
        @tracker_host ||= "www.pivotaltracker.com"
      end

      def api_ssl_url(user=nil, password=nil)
        user_password = (user && password) ? "#{user}:#{password}@" : ''
        "https://#{user_password}#{tracker_host}#{api_path}"
      end

      def api_url
        "http://#{tracker_host}#{api_path}"
      end

      protected

        def protocol
          use_ssl ? 'https' : 'http'
        end

        def cached_connection?
          !@connections[@token].nil?
        end

        def cached_connection
          @connections[@token]
        end

        def new_connection
          @connections[@token] = RestClient::Resource.new("#{use_ssl ? api_ssl_url : api_url}", :headers => {'X-TrackerToken' => @token, 'Content-Type' => 'application/xml'})
        end

        def protocol_changed?
          cached_connection? ? (cached_connection_protocol != protocol) : false
        end

        def cached_connection_protocol
          cached_connection.url.match(/^(.*):\/\//).captures.first
        end

        def api_path
          '/services/v3'
        end
    end

  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
pivotal-tracker-0.5.12 lib/pivotal-tracker/client.rb
pivotal-tracker-fox-0.5.13 lib/pivotal-tracker/client.rb
pivotal-tracker-fox-0.5.12 lib/pivotal-tracker/client.rb
pivotal-tracker-fox-0.5.11 lib/pivotal-tracker/client.rb
pivotal-tracker-fox-0.5.10 lib/pivotal-tracker/client.rb
pivotal-tracker-0.5.10 lib/pivotal-tracker/client.rb
pivotal-tracker-0.5.9 lib/pivotal-tracker/client.rb
pivotal-tracker-0.5.8 lib/pivotal-tracker/client.rb
pivotal-tracker-0.5.7 lib/pivotal-tracker/client.rb
pivotal-tracker-0.5.6 lib/pivotal-tracker/client.rb
pivotal-tracker-0.5.5 lib/pivotal-tracker/client.rb
pivotal-tracker-0.5.4 lib/pivotal-tracker/client.rb
pivotal-tracker-0.5.3 lib/pivotal-tracker/client.rb