Class: PivotalTracker::Story

Inherits:
Object
  • Object
show all
Includes:
HappyMapper, Validation
Defined in:
lib/pivotal-tracker/story.rb,
lib/pivotal-tracker/story.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validation

#create_with_validations, #errors, included, #update_with_validations

Constructor Details

#initialize(attributes = {}) ⇒ Story

Returns a new instance of Story.


50
51
52
53
54
55
# File 'lib/pivotal-tracker/story.rb', line 50

def initialize(attributes={})
  if attributes[:owner]
    self.project_id = attributes.delete(:owner).id
  end
  update_attributes(attributes)
end

Class Method Details

.all(project, options = {}) ⇒ Object


6
7
8
9
10
11
# File 'lib/pivotal-tracker/story.rb', line 6

def all(project, options={})
  params = PivotalTracker.encode_options(options)
  stories = parse(Client.connection["/projects/#{project.id}/stories#{params}"].get)
  stories.each { |s| s.project_id = project.id }
  return stories
end

.find(param, project_id) ⇒ Object


13
14
15
16
17
18
19
20
21
# File 'lib/pivotal-tracker/story.rb', line 13

def find(param, project_id)
  begin
    story = parse(Client.connection["/projects/#{project_id}/stories/#{param}"].get)
    story.project_id = project_id
  rescue RestClient::ExceptionWithResponse
    story = nil
  end
  return story
end

Instance Method Details

#createObject


57
58
59
60
61
62
63
# File 'lib/pivotal-tracker/story.rb', line 57

def create
  return self if project_id.nil?
  response = Client.connection["/projects/#{project_id}/stories"].post(self.to_xml, :content_type => 'application/xml')
  new_story = Story.parse(response)
  new_story.project_id = project_id
  return new_story
end

#deleteObject


76
77
78
# File 'lib/pivotal-tracker/story.rb', line 76

def delete
  Client.connection["/projects/#{project_id}/stories/#{id}"].delete
end

#move(position, story) ⇒ Object

Raises:

  • (ArgumentError)

71
72
73
74
# File 'lib/pivotal-tracker/story.rb', line 71

def move(position, story)
  raise ArgumentError, "Can only move :before or :after" unless [:before, :after].include? position
  Story.parse(Client.connection["/projects/#{project_id}/stories/#{id}/moves?move\[move\]=#{position}&move\[target\]=#{story.id}"].post(''))
end

#move_to_project(new_project) ⇒ Object


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/pivotal-tracker/story.rb', line 92

def move_to_project(new_project)
  move = true
  old_project_id = self.project_id
  target_project = -1
  case new_project.class.to_s
    when 'PivotalTracker::Story'
      target_project = new_project.project_id
    when 'PivotalTracker::Project'
      target_project = new_project.id
    when 'String'
      target_project = new_project.to_i
    when 'Fixnum', 'Integer'
      target_project = new_project
    else
      move = false
  end
  if move
    move_builder = Nokogiri::XML::Builder.new do |story|
      story.story {
        story.project_id "#{target_project}"
              }
    end
    Story.parse(Client.connection["/projects/#{old_project_id}/stories/#{id}"].put(move_builder.to_xml, :content_type => 'application/xml'))
  end
end

#notesObject


80
81
82
# File 'lib/pivotal-tracker/story.rb', line 80

def notes
  @notes ||= Proxy.new(self, Note)
end

#tasksObject


84
85
86
# File 'lib/pivotal-tracker/story.rb', line 84

def tasks
  @tasks ||= Proxy.new(self, Task)
end

#update(attrs = {}) ⇒ Object


65
66
67
68
69
# File 'lib/pivotal-tracker/story.rb', line 65

def update(attrs={})
  update_attributes(attrs)
  response = Client.connection["/projects/#{project_id}/stories/#{id}"].put(self.to_xml, :content_type => 'application/xml')
  return Story.parse(response)
end

#upload_attachment(filename) ⇒ Object


88
89
90
# File 'lib/pivotal-tracker/story.rb', line 88

def upload_attachment(filename)
  Attachment.parse(Client.connection["/projects/#{project_id}/stories/#{id}/attachments"].post(:Filedata => File.new(filename)))
end