Class: TrackR::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.



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

def initialize(options = {})
  if options.include?(:project_id) && options.include?(:token)
    @id      = options[:project_id]
    @token   = options[:token].to_s
    @api_url = "#{CONFIG[:api_url]}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.



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

def api_url
  @api_url
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#iteration_lengthObject (readonly)

Returns the value of attribute iteration_length.



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

def iteration_length
  @iteration_length
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#point_scaleObject (readonly)

Returns the value of attribute point_scale.



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

def point_scale
  @point_scale
end

#tokenObject (readonly)

Returns the value of attribute token.



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

def token
  @token
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

#week_start_dayObject (readonly)

Returns the value of attribute week_start_day.



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

def week_start_day
  @week_start_day
end

Instance Method Details

#backlogObject

Gets the backlog’s stories



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

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 TrackR::Story object TODO: Validate attributes



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/track-r/project.rb', line 37

def create_story(attributes = {})
  api_url = URI.parse("#{CONFIG[:api_url]}projects/#{@id}/stories")
  query_string = attributes.map { |key, value| "story[#{key}]=#{CGI::escape(value)}"}.join('&')
  begin
    http = Net::HTTP.new(api_url.host, api_url.port)
    http.use_ssl = true
    response, data = http.post(api_url.path, query_string.concat("&token=#{@token}"))

    raise ResponseError, "Wrong response code" unless response.code.to_i == 200
    story = (Hpricot(response.body)/:story)
    TrackR::Story.new(:story => story, :project_id => @id, :token => @token)
  rescue ResponseError => e
    print "Got response code [#{response.code}]:\t"
    puts response.message
    raise
  end
end

#currentObject

Gets the current iteration’s stories



84
85
86
# File 'lib/track-r/project.rb', line 84

def current
  get_stories_by_iteration("current")
end

#delete_story(story) ⇒ Object

Deletes a story given a TrackR::Story object or a story_id



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/track-r/project.rb', line 56

def delete_story(story)
  if story.is_a?(TrackR::Story)
    api_url = URI.parse("#{CONFIG[:api_url]}projects/#{@id}/stories/#{story.id}")
  elsif story.is_a?(Integer) || story.to_i.is_a?(Integer)
    api_url = URI.parse("#{CONFIG[:api_url]}projects/#{@id}/stories/#{story}")
  else
    raise ArgumentError, "Should receive a story id or a TrackR::Story object."
  end
  begin
    http = Net::HTTP.new(api_url.host, api_url.port)
    http.use_ssl = true
    response, data = http.delete(api_url.path, {"X-TrackerToken" => @token})
    raise ResponseError, "Wrong response code" unless response.code.to_i == 200
    story = (Hpricot(response.body)/:story)
    TrackR::Story.new(:story => story, :project_id => @id, :token => @token)
  rescue ResponseError => e
    print "Got response code [#{response.code}]:\t"
    puts response.message
    raise
  end
end

#doneObject

Gets the done iteration’s stories



94
95
96
# File 'lib/track-r/project.rb', line 94

def done
  get_stories_by_iteration("done")
end

#iceboxObject

Gets the icebox iteration’s stories



89
90
91
# File 'lib/track-r/project.rb', line 89

def icebox
  get_stories_by_iteration("icebox")
end

#storiesObject

Builds an array containing the project’s story



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

def stories ; @stories || get_stories ; end

#story(id) ⇒ Object

Fetches a story with given id



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

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