-
-
Save tenderlove/21241 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
Pivotal Tracker API client (note: super-not-ready) | |
USAGE | |
Finding a project: | |
project = PivotalTracker::Projet.find(1) | |
project.name # => Some Name | |
project.point_scale # => "0,1,2,4,8" | |
etc. | |
Finding a project's stories | |
stories = project.stories | |
story = stories.first | |
story.id # => 123 | |
story.url # => "http://www.pivotaltracker.com/story/show/123" | |
story.story_type # => "feature" | |
story.requested_by # => "Pat Nakajima" | |
story.description # => "some sort of description" | |
etc. | |
You can find more information about things by looking at the raw | |
XML response. This stuff is just a way to access that info more | |
conviently. | |
=end | |
%w(rubygems nokogiri net/http uri).each { |lib| require lib } | |
module PivotalTracker | |
BASE_URI = "http://www.pivotaltracker.com/services/v1" | |
class << self | |
attr_writer :token | |
def token | |
@token ||= begin | |
print "Enter your Pivotal Tracker token: " | |
@token = $stdin.gets.chomp | |
end | |
end | |
def request(path, options={}) | |
uri = URI.parse(BASE_URI + path) | |
response = Net::HTTP.start(uri.host, uri.port) { |http| http.get(uri.path, 'Token' => token) } | |
result = Nokogiri::XML(response.body).search(options[:tag]).map { |res| options[:klass].new(res) } | |
result = result.first if options[:first] | |
result.id = options[:id] if options[:id] | |
result | |
end | |
end | |
class Base | |
attr_writer :id | |
def id | |
doc.at('id').inner_text | |
end | |
def method_missing(sym, *args) | |
doc.at(sym.to_s).inner_text | |
end | |
def initialize(doc) | |
@doc = doc | |
end | |
private | |
def doc | |
@doc | |
end | |
end | |
class Story < Base; end | |
class Project < Base | |
def self.find(id) | |
PivotalTracker.request "/projects/#{id}", :id => id, :klass => Project, :tag => 'project', :first => true | |
end | |
def stories(refresh=false) | |
@stories = nil if refresh | |
@stories ||= PivotalTracker.request("/projects/#{@id}/stories", :klass => Story, :tag => 'story') | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment