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.



44
45
46
47
48
49
# File 'lib/pivotal-tracker/story.rb', line 44

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



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

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



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

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

#move_to_project(new_project) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/pivotal-tracker/story.rb', line 81

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



69
70
71
# File 'lib/pivotal-tracker/story.rb', line 69

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

#tasksObject



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

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

#update(attrs = {}) ⇒ Object



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

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



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

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