Class: Story

Inherits:
Object
  • Object
show all
Defined in:
lib/track-r/story.rb

Overview

Typhoon TODO: Documentation ☻

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Story

Returns a new instance of Story.



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

def initialize(options = {})
  @token      = options[:token].to_s
  if options.include?(:project_id) && options.include?(:story_id) && options.include?(:token)
    @id         = options[:story_id]
    @project_id = options[:project_id]
    @url        = "http://www.pivotaltracker.com/story/show/#{@id}"
    @api_url    = "http://www.pivotaltracker.com/services/v2/projects/#{@project_id}/stories/#{@id}"
    @story      = Hpricot(open(@api_url, {"X-TrackerToken" => @token}))
  elsif options.include?(:story) && options.include?(:project_id) && options.include?(:token)
    @project_id = options[:project_id]
    @story      = options[:story]
  else
    raise ArgumentError, "Valid options are: :story (receives an Hpricot Object) + :project_id OR :project_id + :story_id + :token"
  end
  build_story
end

Instance Attribute Details

#accepted_atObject

Returns the value of attribute accepted_at.



3
4
5
# File 'lib/track-r/story.rb', line 3

def accepted_at
  @accepted_at
end

#commentsObject

Returns the value of attribute comments.



3
4
5
# File 'lib/track-r/story.rb', line 3

def comments
  @comments
end

#created_atObject

Returns the value of attribute created_at.



3
4
5
# File 'lib/track-r/story.rb', line 3

def created_at
  @created_at
end

#current_stateObject

Returns the value of attribute current_state.



3
4
5
# File 'lib/track-r/story.rb', line 3

def current_state
  @current_state
end

#descriptionObject

Returns the value of attribute description.



3
4
5
# File 'lib/track-r/story.rb', line 3

def description
  @description
end

#estimateObject

Returns the value of attribute estimate.



3
4
5
# File 'lib/track-r/story.rb', line 3

def estimate
  @estimate
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/track-r/story.rb', line 7

def id
  @id
end

#labelsObject

Returns the value of attribute labels.



3
4
5
# File 'lib/track-r/story.rb', line 3

def labels
  @labels
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/track-r/story.rb', line 3

def name
  @name
end

#owned_byObject

Returns the value of attribute owned_by.



3
4
5
# File 'lib/track-r/story.rb', line 3

def owned_by
  @owned_by
end

#project_idObject

Returns the value of attribute project_id.



3
4
5
# File 'lib/track-r/story.rb', line 3

def project_id
  @project_id
end

#requested_byObject

Returns the value of attribute requested_by.



3
4
5
# File 'lib/track-r/story.rb', line 3

def requested_by
  @requested_by
end

#story_typeObject

Returns the value of attribute story_type.



3
4
5
# File 'lib/track-r/story.rb', line 3

def story_type
  @story_type
end

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/track-r/story.rb', line 7

def url
  @url
end

Instance Method Details

#build_storyObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/track-r/story.rb', line 26

def build_story
  @id            ||= @story.at('id').inner_html
  @url           ||= "http://www.pivotaltracker.com/story/show/#{@id}"
  @api_url       ||= "http://www.pivotaltracker.com/services/v2/projects/#{@project_id}/stories/#{@id}"
  @story_type    = @story.at('story_type').inner_html    unless @story.at('story_type').nil?
  @estimate      = @story.at('estimate').inner_html      unless @story.at('estimate').nil?
  @current_state = @story.at('current_state').inner_html unless @story.at('current_state').nil?
  @description   = @story.at('description').inner_html   unless @story.at('description').nil?
  @name          = @story.at('name').inner_html          unless @story.at('name').nil?
  @requested_by  = @story.at('requested_by').inner_html  unless @story.at('requested_by').nil?
  @owned_by      = @story.at('owned_by').inner_html      unless @story.at('owned_by').nil?
  @created_at    = @story.at('created_at').inner_html    unless @story.at('created_at').nil?
  @accepted_at   = @story.at('accepted_at').inner_html   unless @story.at('accepted_at').nil?
  @labels        = @story.at('labels').inner_html        unless @story.at('labels').nil?
  @comments      ||= build_comments(@story.at('notes').inner_html)         unless @story.at('notes').nil?
end

#destroyObject

TODO: test this method:



70
71
72
73
74
75
# File 'lib/track-r/story.rb', line 70

def destroy
  api_url = URI.parse("http://www.pivotaltracker.com/services/v2/projects/#{@project_id}/stories/#{@id}")
  response = Net::HTTP.start(api_url.host, api_url.port) do |http|
    http.delete(api_url.path, {"X-TrackerToken" => @token})
  end
end

#saveObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/track-r/story.rb', line 58

def save
  parameters = build_story_xml
  api_url = URI.parse("http://www.pivotaltracker.com/services/v2/projects/#{@project_id}/stories/#{@id}")
  response = Net::HTTP.start(api_url.host, api_url.port) do |http|
    http.put(api_url.path, parameters, {'X-TrackerToken' => @token, 'Content-Type' => 'application/xml'})
  end

  @story = (Hpricot(response.body)/:story)
  build_story
end

#update(attrs = {}) ⇒ Object

TODO: Test this method



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/track-r/story.rb', line 44

def update(attrs = {})
  unless attrs.empty?
    if validate_attributes(attrs)
      attrs.each do |attribute, value|
        virt_attr = "#{attribute}="
        self.send(virt_attr, value)
      end
      return save
    else
      raise ArgumentError
    end
  end
end