Class: Torque::Pivotal

Inherits:
Object
  • Object
show all
Defined in:
lib/torque/pivotal.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Pivotal

Returns a new instance of Pivotal.

Parameters:

  • token

    A Pivotal Tracker api token



11
12
13
# File 'lib/torque/pivotal.rb', line 11

def initialize(token)
  @token = token
end

Class Method Details

.connection?Boolean

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

Returns:



17
18
19
20
21
22
23
24
# File 'lib/torque/pivotal.rb', line 17

def self.connection?
  begin
    TCPSocket.new "www.pivotaltracker.com", 80
    true
  rescue SocketError
    false
  end
end

Instance Method Details

#check_tokenObject

Returns True if the token supplied is a valid token, else false.

Returns:

  • True if the token supplied is a valid token, else false



28
29
30
31
32
33
34
35
# File 'lib/torque/pivotal.rb', line 28

def check_token
  begin
    get_project_data
    true
  rescue InvalidTokenError
    false
  end
end

#get_project_dataObject

Sends a request through the Pivotal Tracker API

Returns:

  • A string of html data from Pivotal Tracker with data on each of a user’s projects



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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) ⇒ Object

Sends a request throgh the Pivotal Tracker API

Parameters:

  • project

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

  • number (defaults to: 1)

    The number of project iterations to fetch

Returns:

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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) ⇒ Object

Sends a request through the Pivotal Tracker API

Parameters:

  • project

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

Returns:

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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