Class: TrackR::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.



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

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    = "#{CONFIG[:api_url]}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.



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

def accepted_at
  @accepted_at
end

#commentsObject

Returns the value of attribute comments.



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

def comments
  @comments
end

#created_atObject

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#current_stateObject

Returns the value of attribute current_state.



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

def current_state
  @current_state
end

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#estimateObject

Returns the value of attribute estimate.



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

def estimate
  @estimate
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#labelsObject

Returns the value of attribute labels.



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

def labels
  @labels
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#owned_byObject

Returns the value of attribute owned_by.



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

def owned_by
  @owned_by
end

#project_idObject

Returns the value of attribute project_id.



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

def project_id
  @project_id
end

#requested_byObject

Returns the value of attribute requested_by.



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

def requested_by
  @requested_by
end

#storyObject

Returns the value of attribute story.



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

def story
  @story
end

#story_typeObject

Returns the value of attribute story_type.



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

def story_type
  @story_type
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#build_storyObject



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

def build_story
  @id            ||= @story.at('id').inner_html if @story.at('id')
  @url           ||= "http://www.pivotaltracker.com/story/show/#{@id}"
  @api_url       ||= "#{CONFIG[:api_url]}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:



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/track-r/story.rb', line 79

def destroy
  api_url = URI.parse("#{CONFIG[:api_url]}projects/#{@project_id}/stories/#{@id}")
  begin
    http = Net::HTTP.new(api_url.host, api_url.port)
    http.use_ssl = true
    response, data = http.delete(api_url.path, {"X-TrackerToken" => @token})
    raise ResponseError, "Wrong response code" unless response.code.to_i == 200
  rescue ResponseError => e
    print "Got response code [#{response.code}]:\t"
    puts response.message
    raise
  end
end

#saveObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/track-r/story.rb', line 59

def save
  parameters = build_story_xml
  api_url = URI.parse("#{CONFIG[:api_url]}projects/#{@project_id}/stories/#{@id}")
  begin
    http = Net::HTTP.new(api_url.host, api_url.port)
    http.use_ssl = true
    response, data = http.put(api_url.path, parameters, {'X-TrackerToken' => @token, 'Content-Type' => 'application/xml'})

    raise ResponseError, "Wrong response code" unless response.code.to_i == 200
  rescue ResponseError => e
    print "Got response code [#{response.code}]:\t"
    puts response.message
    raise
  end

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

#update(attrs = {}) ⇒ Object

TODO: Test this method



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

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