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.



112
113
114
115
116
117
# File 'lib/pivotal-tracker/story.rb', line 112

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

.parse_stories(xml_string, project_id) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pivotal-tracker/story.rb', line 23

def parse_stories(xml_string, project_id)
  doc = Nokogiri::XML(xml_string)
  doc.xpath('//story').map do |xml_story|
    story = Story.new({
        id: xml_story.xpath('id').inner_text.to_i,
        url: xml_story.xpath('url').inner_text,
        project_id: project_id,
        name: xml_story.xpath('name').inner_text,
        description: xml_story.xpath('description').inner_text,
        story_type: xml_story.xpath('story_type').inner_text,
        estimate: xml_story.xpath('estimate').inner_text.to_i,
        current_state: xml_story.xpath('current_state').inner_text,
        requested_by: xml_story.xpath('requested_by').inner_text,
        owned_by: xml_story.xpath('owned_by').inner_text,
        labels: xml_story.xpath('labels').inner_text,
        jira_id: xml_story.xpath('jira_id').inner_text,
        jira_url: xml_story.xpath('jira_url').inner_text,
        other_id: xml_story.xpath('other_id').inner_text,
        integration_id: xml_story.xpath('integration_id').inner_text,
        deadline: xml_story.xpath('deadline').inner_text
    })

    @attachments = xml_story.xpath('attachments')
    story.attachments = @attachments.xpath('attachment').map do |xml|
      attachment = Attachment.new
      attachment.id = xml.xpath('id').inner_text.to_i
      attachment.filename = xml.xpath('filename').inner_text
      attachment.description = xml.xpath('description').inner_text
      attachment.uploaded_by = xml.xpath('uploaded_by').inner_text
      attachment.uploaded_at = xml.xpath('uploaded_at').inner_text
      attachment.url = xml.xpath('url').inner_text
      attachment.status = xml.xpath('status').inner_text
      attachment
    end

    @notes = xml_story.xpath('notes')
    story.notes = @notes.xpath('note').map do |xml|
      Note.new({
         id: xml.xpath('id').inner_text.to_i,
         text: xml.xpath('text').inner_text,
         author: xml.xpath('author').inner_text,
         noted_at: DateTime.parse(xml.xpath('noted_at').inner_text.to_s).to_s,
         story: story
      })
    end

    @tasks = xml_story.xpath('tasks')
    story.tasks = @tasks.xpath('task').map do |xml|
      Task.new({
         id: xml.xpath('id').inner_text.to_i,
         description: xml.xpath('description').inner_text,
         complete: xml.xpath('complete').inner_text,
         created_at: DateTime.parse(xml.xpath('created_at').inner_text.to_s).to_s,
         story: story
      })
    end

    story
  end
end

Instance Method Details

#createObject



119
120
121
122
123
124
125
# File 'lib/pivotal-tracker/story.rb', line 119

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



138
139
140
# File 'lib/pivotal-tracker/story.rb', line 138

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

#move(position, story) ⇒ Object

Raises:

  • (ArgumentError)


133
134
135
136
# File 'lib/pivotal-tracker/story.rb', line 133

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



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/pivotal-tracker/story.rb', line 146

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

#update(attrs = {}) ⇒ Object



127
128
129
130
131
# File 'lib/pivotal-tracker/story.rb', line 127

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



142
143
144
# File 'lib/pivotal-tracker/story.rb', line 142

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