class Torque::Pivotal

Public Class Methods

connection?() click to toggle source

@return True if a connection to www.pivotaltracker.com exists, else false

# File lib/torque/pivotal.rb, line 17
def self.connection?
  begin
    TCPSocket.new "www.pivotaltracker.com", 80
    true
  rescue SocketError
    false
  end
end
new(token) click to toggle source

@param token A Pivotal Tracker api token

# File lib/torque/pivotal.rb, line 11
def initialize(token)
  @token = token
end

Public Instance Methods

check_token() click to toggle source

@return True if the token supplied is a valid token, else false

# File lib/torque/pivotal.rb, line 28
def check_token
  begin
    get_project_data
    true
  rescue InvalidTokenError
    false
  end
end
get_project_data() click to toggle source

@return A string of html data from Pivotal Tracker with data on each of a user's projects

Sends a request through the Pivotal Tracker API

# File lib/torque/pivotal.rb, line 41
def get_project_data

  host="pivotaltracker.com"
  port=80
  url="http://www.pivotaltracker.com/services/v3/projects"
  headers={'X-TrackerToken'=>@token}

  url = URI.escape(url)

  response=Net::HTTP.new(host, port).get(url, headers)

  # Handles network errors
  if response.code == "401"
    raise InvalidTokenError.new "The Pivotal Tracker API token supplied is not valid"

  elsif response.code != "200"
    raise PivotalAPIError.new(
      "The Pivotal Tracker API responded with an unexpected error code: #{response.code}. Check your " \
        "API token, internet connection, and/or pivotaltracker.com"
    )
  end

  response.body
end
get_project_iterations(project, number=1) click to toggle source

@param project The ID of the Pivotal Tracker project from which to get data @param number The number of project iterations to fetch

@return A string of html data from Pivotal Tracker with data on finished iterations of a project

Sends a request throgh the Pivotal Tracker API

# File lib/torque/pivotal.rb, line 109
def get_project_iterations(project, number=1)

  # Polls story data from pivotal tracker
  host="pivotaltracker.com"
  port=80
  url="http://www.pivotaltracker.com/services/v3/projects/#{project}/iterations/done?offset=-#{number}"
  headers={'X-TrackerToken'=>@token}

  url = URI.escape(url)

  response=Net::HTTP.new(host, port).get(url, headers)

  # Handles network errors
  if response.code == "401"
    raise InvalidTokenError.new "The Pivotal Tracker API token supplied is not valid for project #{project}"

  elsif response.code == "500"
    raise InvalidProjectError.new "The Pivotal Tracker project ID supplied, #{project}, is invalid"

  elsif !(["2","3"].member? response.code[0])
    raise PivotalAPIError.new(
      "The Pivotal Tracker API responded with an unexpected error code: #{response.code}. Check your " \
      + "project ID, API token, internet connection, and/or pivotaltracker.com"
    )
    
  end

  response.body
end
get_project_stories(project) click to toggle source

@param project The ID of the Pivotal Tracker project from which to get data

@return A string of html data from Pivotal Tracker with data on the stories for the given project

Sends a request through the Pivotal Tracker API

# File lib/torque/pivotal.rb, line 72
def get_project_stories(project)

  # Polls story data from pivotal tracker
  host="pivotaltracker.com"
  port=80
  url="http://www.pivotaltracker.com/services/v3/projects/#{project}/stories"
  headers={'X-TrackerToken'=>@token}

  url = URI.escape(url)

  response=Net::HTTP.new(host, port).get(url, headers)

  # Handles network errors
  if response.code == "401"
    raise InvalidTokenError.new "The Pivotal Tracker API token supplied is not valid for project #{project}"

  elsif response.code == "500"
    raise InvalidProjectError.new "The Pivotal Tracker project ID supplied, #{project}, is invalid"

  elsif response.code != "200"
    raise PivotalAPIError.new(
      "The Pivotal Tracker API responded with an unexpected error code: #{response.code}. Check your " \
      + "project ID, API token, internet connection, and/or pivotaltracker.com"
    )
    
  end

  response.body
end