Class: Project

Inherits:
Object
  • Object
show all
Defined in:
lib/track-r/project.rb

Overview

Container for project’s attributes. Receives a hash with either :project key pointing to an hpricot object or a project_id and a token with which to fetch and build the project object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Project

Returns a new instance of Project.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/track-r/project.rb', line 8

def initialize(options = {})
  if options.include?(:project_id) && options.include?(:token)
    @id      = options[:project_id]
    @token   = options[:token].to_s
    @api_url = "http://www.pivotaltracker.com/services/v2/projects/#{@id}"
    @url     = "http://www.pivotaltracker.com/projects/#{@id}"
    @project = Hpricot(open(@api_url, {"X-TrackerToken" => @token}))
    @stories = nil
  elsif options.include?(:project) && options.include?(:token)
    @project = options[:project]
    @token   = options[:token].to_s
  else
    raise ArgumentError, "Valid options are: :project (receives an Hpricot Object) OR :project_id + :token"
  end
  build_project
end

Instance Attribute Details

#api_urlObject (readonly)

Returns the value of attribute api_url.



5
6
7
# File 'lib/track-r/project.rb', line 5

def api_url
  @api_url
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/track-r/project.rb', line 5

def id
  @id
end

#iteration_lengthObject (readonly)

Returns the value of attribute iteration_length.



5
6
7
# File 'lib/track-r/project.rb', line 5

def iteration_length
  @iteration_length
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/track-r/project.rb', line 5

def name
  @name
end

#point_scaleObject (readonly)

Returns the value of attribute point_scale.



5
6
7
# File 'lib/track-r/project.rb', line 5

def point_scale
  @point_scale
end

#tokenObject (readonly)

Returns the value of attribute token.



5
6
7
# File 'lib/track-r/project.rb', line 5

def token
  @token
end

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/track-r/project.rb', line 5

def url
  @url
end

#week_start_dayObject (readonly)

Returns the value of attribute week_start_day.



5
6
7
# File 'lib/track-r/project.rb', line 5

def week_start_day
  @week_start_day
end

Instance Method Details

#backlogObject

Gets the backlog’s stories



64
65
66
# File 'lib/track-r/project.rb', line 64

def backlog
  get_stories_by_iteration("backlog")
end

#create_story(attributes = {}) ⇒ Object

Creates a story for this project. Receives a set of valid attributes. Returns a Story object TODO: Validate attributes



36
37
38
39
40
41
42
43
44
45
# File 'lib/track-r/project.rb', line 36

def create_story(attributes = {})
  api_url = URI.parse("http://www.pivotaltracker.com/services/v2/projects/#{@id}/stories")
  query_string = attributes.map { |key, value| "story[#{key}]=#{CGI::escape(value)}"}.join('&')
  response = Net::HTTP.start(api_url.host, api_url.port) do |http|
    http.post(api_url.path, query_string.concat("&token=#{@token}"))
  end

  story = (Hpricot(response.body)/:story)
  Story.new(:story => story, :project_id => @id, :token => @token)
end

#currentObject

Gets the current iteration’s stories



69
70
71
# File 'lib/track-r/project.rb', line 69

def current
  get_stories_by_iteration("current")
end

#delete_story(story) ⇒ Object

Deletes a story given a Story object or a story_id



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/track-r/project.rb', line 48

def delete_story(story)
  if story.is_a?(Story)
    api_url = URI.parse("http://www.pivotaltracker.com/services/v2/projects/#{@id}/stories/#{story.id}")
  elsif story.is_a?(Integer) || story.to_i.is_a?(Integer)
    api_url = URI.parse("http://www.pivotaltracker.com/services/v2/projects/#{@id}/stories/#{story}")
  else
    raise ArgumentError, "Should receive a story id or a Story object."
  end
  response = Net::HTTP.start(api_url.host, api_url.port) do |http|
    http.delete(api_url.path, {"X-TrackerToken" => @token})
  end
  story = (Hpricot(response.body)/:story)
  Story.new(:story => story, :project_id => @id, :token => @token)
end

#doneObject

Gets the done iteration’s stories



79
80
81
# File 'lib/track-r/project.rb', line 79

def done
  get_stories_by_iteration("done")
end

#iceboxObject

Gets the icebox iteration’s stories



74
75
76
# File 'lib/track-r/project.rb', line 74

def icebox
  get_stories_by_iteration("icebox")
end

#storiesObject

Builds an array containing the project’s story



26
# File 'lib/track-r/project.rb', line 26

def stories ; @stories || get_stories ; end

#story(id) ⇒ Object

Fetches a story with given id



29
30
31
# File 'lib/track-r/project.rb', line 29

def story(id)
  Story.new(:story_id => id, :project_id => @id, :token => @token)
end