Skip to content

Instantly share code, notes, and snippets.

@sandro
Created January 8, 2010 23:44
Show Gist options
  • Save sandro/272583 to your computer and use it in GitHub Desktop.
Save sandro/272583 to your computer and use it in GitHub Desktop.
fetch pivotal tracker story name from id
#!/usr/bin/env ruby
=begin
Author: Sandro Turriate
Permalink: http://gist.github.com/272583
DESCRIPTION
-----------
Turn a Pivotal Tracker story id into the name of that story.
SETUP
-----
$ gem install httparty
$ cat ~/.tracker_project
54321
$ cat ~/.tracker.yml
api_token: 4798frafa4593fja32b32bwerbasflj1
The ~/.tracker_project file contains the current Pivotal Tracker project id
that you're working on.
The ~/.tracker.yml file contains your Pivotal Tracker api key.
USAGE
-----
$ ./tracker_story_name.rb 12345
Sign Out redirects to the home page
=end
begin; require 'rubygems'; rescue LoadError; end
require 'httparty'
class TrackerStory
include HTTParty
base_uri "https://www.pivotaltracker.com/services/v2"
attr_reader :name, :id
def initialize(id)
@id = id
self.class.headers 'X-TrackerToken' => api_token
end
def name
@name ||= result['name']
end
def result
@result ||= fetch
end
protected
def fetch
response = self.class.get("/projects/#{project_id}/stories/#{id}")
if response['story']
response['story']
elsif error_message = response['message']
abort error_message
else
abort "Could not interpret the response: #{response.inspect}"
end
end
def project_id
@project_id ||= File.read("#{ENV['HOME']}/.tracker_project").strip
end
def api_token
@api_token ||= YAML.load_file("#{ENV['HOME']}/.tracker.yml")['api_token']
end
end
if __FILE__ == $0
args = $stdin.tty? ? ARGV : $stdin.readlines
if args.empty?
abort "Please provide a story number."
else
puts TrackerStory.new(args.first.to_i).name
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment