Sha256: 9383f1ff288a8044d7dc302b2043c0f80fa17d0ea55a25c4fcb54cdd847638c9

Contents?: true

Size: 1.56 KB

Versions: 4

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

require "tracker_api"
require "nokogiri"

module GetToWork
  class Service
    class PivotalTracker < GetToWork::Service
      display_name "Pivotal Tracker"

      def get_auth_token(user:, pass:)
        # POST
        uri = URI("https://www.pivotaltracker.com/services/v3/tokens/active")
        req = Net::HTTP::Get.new(uri)
        req.basic_auth user, pass

        res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
          http.request(req)
        end

        case res
        when Net::HTTPUnauthorized
          raise Service::UnauthorizedError
        else
          res
        end

        # Parse the token
        xml = Nokogiri::XML(res.body)
        xml.xpath("//guid").first.text
      end

      def authenticate(username:, password:, subdomain:)
        token = get_auth_token(user: username, pass: password)
        set_client_token(token)
      end

      def authenticate_with_keychain
        if keychain
          set_client_token(keychain.password)
        end
      end

      def set_client_token(token)
        @api_token = token
      end

      def api_token
        @api_token ||= authenticate_with_keychain
      end

      def api_client
        @api_client ||= TrackerApi::Client.new(token: api_token)
      end

      def projects
        @projects ||= get_projects
      end

      def get_projects
        api_client.projects.sort do |x, y|
          x[:name] <=> y[:name]
        end
      end

      def story(story_id)
        api_client.project(@project_id).story(story_id)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
get_to_work-0.1.5 lib/get_to_work/service/pivotal_tracker.rb
get_to_work-0.1.4 lib/get_to_work/service/pivotal_tracker.rb
get_to_work-0.1.3 lib/get_to_work/service/pivotal_tracker.rb
get_to_work-0.1.2 lib/get_to_work/service/pivotal_tracker.rb