class PivotalTracker::Story

Public Class Methods

all(project, options={}) click to toggle source
# 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) click to toggle source
# 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
new(attributes={}) click to toggle source
# File lib/pivotal-tracker/story.rb, line 48
def initialize(attributes={})
  if attributes[:owner]
    self.project_id = attributes.delete(:owner).id
  end
  update_attributes(attributes)
end

Public Instance Methods

create() click to toggle source
# File lib/pivotal-tracker/story.rb, line 55
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
delete() click to toggle source
# File lib/pivotal-tracker/story.rb, line 74
def delete
  Client.connection["/projects/#{project_id}/stories/#{id}"].delete
end
move(position, story) click to toggle source
# File lib/pivotal-tracker/story.rb, line 69
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) click to toggle source
# File lib/pivotal-tracker/story.rb, line 90
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
notes() click to toggle source
# File lib/pivotal-tracker/story.rb, line 78
def notes
  @notes ||= Proxy.new(self, Note)
end
tasks() click to toggle source
# File lib/pivotal-tracker/story.rb, line 82
def tasks
  @tasks ||= Proxy.new(self, Task)
end
update(attrs={}) click to toggle source
# File lib/pivotal-tracker/story.rb, line 63
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) click to toggle source
# File lib/pivotal-tracker/story.rb, line 86
def upload_attachment(filename)
  Attachment.parse(Client.connection["/projects/#{project_id}/stories/#{id}/attachments"].post(:Filedata => File.new(filename)))
end

Protected Instance Methods

to_xml() click to toggle source
# File lib/pivotal-tracker/story.rb, line 118
def to_xml
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.story {
      xml.name "#{name}"
      xml.description "#{description}"
      xml.story_type "#{story_type}"
      xml.estimate "#{estimate}"
      xml.current_state "#{current_state}"
      xml.requested_by "#{requested_by}"
      xml.owned_by "#{owned_by}"
      xml.labels "#{labels}"
      xml.project_id "#{project_id}"
      # See spec
      # xml.jira_id "#{jira_id}"
      # xml.jira_url "#{jira_url}"
      xml.other_id "#{other_id}" if other_id
      xml.integration_id "#{integration_id}" if integration_id
      xml.created_at DateTime.parse(created_at.to_s).to_s if created_at
      xml.accepted_at DateTime.parse(accepted_at.to_s).to_s if accepted_at
      xml.deadline DateTime.parse(deadline.to_s).to_s if deadline
    }
  end
  return builder.to_xml
end
update_attributes(attrs) click to toggle source
# File lib/pivotal-tracker/story.rb, line 143
def update_attributes(attrs)
  attrs.each do |key, value|
    self.send("#{key}=", value.is_a?(Array) ? value.join(',') : value )
  end
end