Class: PT::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pt/client.rb

Constant Summary collapse

STORY_FIELDS =
':default,requested_by,owners,tasks,comments(:default,person,file_attachments)'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, local_config = nil) ⇒ Client

Returns a new instance of Client.



18
19
20
21
22
# File 'lib/pt/client.rb', line 18

def initialize(token, local_config=nil)
  @client = TrackerApi::Client.new(token: token)
  @config = local_config
  @project = @client.project(local_config[:project_id]) if local_config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/pt/client.rb', line 10

def config
  @config
end

#projectObject (readonly)

Returns the value of attribute project.



10
11
12
# File 'lib/pt/client.rb', line 10

def project
  @project
end

Class Method Details

.get_api_token(email, password) ⇒ Object



12
13
14
15
16
# File 'lib/pt/client.rb', line 12

def self.get_api_token(email, password)
  PivotalAPI::Me.retrieve(email, password)
rescue RestClient::Unauthorized
  raise PT::InputError.new("Bad email/password combination.")
end

Instance Method Details

#add_label(task, label) ⇒ Object



150
151
152
153
154
# File 'lib/pt/client.rb', line 150

def add_label(task, label)
  task = get_story(task.id)
  task.add_label(label)
  task.save
end

#assign_task(task, owner) ⇒ Object



145
146
147
148
# File 'lib/pt/client.rb', line 145

def assign_task(task, owner)
  task = get_story(task.id)
  task.add_owner(owner)
end

#comment_task(task, comment) ⇒ Object



156
157
158
159
# File 'lib/pt/client.rb', line 156

def comment_task(task, comment)
  task = get_story(task.id)
  task.create_comment(text: comment)
end

#create_story(args) ⇒ Object



161
162
163
# File 'lib/pt/client.rb', line 161

def create_story(args)
  project.create_story(args)
end

#estimate_story(task, points) ⇒ Object



139
140
141
142
143
# File 'lib/pt/client.rb', line 139

def estimate_story(task, points)
  task = get_story(task.id)
  task.estimate = points
  task.save
end

#find_member(query) ⇒ Object



122
123
124
125
126
# File 'lib/pt/client.rb', line 122

def find_member(query)
  project.memberships.detect do |m|
    m.person.name.downcase.start_with?(query.downcase) || m.person.initials.downcase == query.downcase
  end
end

#get_activitiesObject



45
46
47
# File 'lib/pt/client.rb', line 45

def get_activities
  project.activity
end

#get_current_iteration(project) ⇒ Object



41
42
43
# File 'lib/pt/client.rb', line 41

def get_current_iteration(project)
  PivotalTracker::Iteration.current(project)
end

#get_member(query) ⇒ Object



117
118
119
120
# File 'lib/pt/client.rb', line 117

def get_member(query)
  member = project.memberships.select{ |m| m.person.name.downcase.start_with?(query.downcase) || m.person.initials.downcase == query.downcase }
  member.empty? ? nil : member.first
end

#get_membersObject



128
129
130
# File 'lib/pt/client.rb', line 128

def get_members
  project.memberships fields: ':default,person'
end

#get_membership(email) ⇒ Object



33
34
35
# File 'lib/pt/client.rb', line 33

def get_membership(email)
  PivotalTracker::Membership.all(project).select{ |m| m.email == email }.first
end

#get_my_infoObject



37
38
39
# File 'lib/pt/client.rb', line 37

def get_my_info
  @client.me
end

#get_my_open_tasks(params = {}) ⇒ Object



67
68
69
70
# File 'lib/pt/client.rb', line 67

def get_my_open_tasks(params={})
  params[:filter] =  "owner:#{config[:user_name]}"
  get_stories(params)
end

#get_my_workObject



53
54
55
# File 'lib/pt/client.rb', line 53

def get_my_work
  project.stories(filter: "owner:#{config[:user_name]} -state:accepted", limit: 50, fields: STORY_FIELDS)
end

#get_project(project_id) ⇒ Object



24
25
26
27
# File 'lib/pt/client.rb', line 24

def get_project(project_id)
  project = @client.project(project_id)
  project
end

#get_projectsObject



29
30
31
# File 'lib/pt/client.rb', line 29

def get_projects
  @client.projects
end

#get_stories(params = {}) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/pt/client.rb', line 108

def get_stories(params={})
  limit = config[:limit] || 20
  page = params[:page] || 0
  offset = page*limit
  filter = params[:filter] || '-state=accepted'
  project.stories limit: limit, fields: STORY_FIELDS, auto_paginate: false, offset: offset, filter: filter
end

#get_stories_to_accept(params = {}) ⇒ Object



93
94
95
96
# File 'lib/pt/client.rb', line 93

def get_stories_to_accept(params={})
  params[:filter] =  "owner:#{config[:user_name]} -state:accepted"
  get_stories(params)
end

#get_stories_to_assign(params = {}) ⇒ Object



103
104
105
106
# File 'lib/pt/client.rb', line 103

def get_stories_to_assign(params={})
  params[:filter] =  "-state:accepted"
  get_stories(params)
end

#get_stories_to_deliver(params = {}) ⇒ Object



88
89
90
91
# File 'lib/pt/client.rb', line 88

def get_stories_to_deliver(params={})
  params[:filter] =  "owner:#{config[:user_name]} -state:delivered,accepted,rejected"
  get_stories(params)
end

#get_stories_to_estimate(params = {}) ⇒ Object



72
73
74
75
# File 'lib/pt/client.rb', line 72

def get_stories_to_estimate(params={})
  params[:filter] =  "owner:#{config[:user_name]} type:feature estimate:-1"
  get_stories(params)
end

#get_stories_to_finish(params = {}) ⇒ Object



83
84
85
86
# File 'lib/pt/client.rb', line 83

def get_stories_to_finish(params={})
  params[:filter] =  "owner:#{config[:user_name]} -state:unscheduled,rejected"
  get_stories(params)
end

#get_stories_to_reject(params = {}) ⇒ Object



98
99
100
101
# File 'lib/pt/client.rb', line 98

def get_stories_to_reject(params={})
  params[:filter] =  "owner:#{config[:user_name]} -state:rejected"
  get_stories(params)
end

#get_stories_to_start(params = {}) ⇒ Object



77
78
79
80
81
# File 'lib/pt/client.rb', line 77

def get_stories_to_start(params={})
  params[:filter] =  "owner:#{config[:user_name]} type:feature,bug state:unscheduled,rejected,unstarted"
  tasks = get_stories(params)
  tasks.reject{ |t| (t.story_type == 'feature') && (!t.estimate) }
end

#get_task_by_id(id) ⇒ Object Also known as: get_story



62
63
64
# File 'lib/pt/client.rb', line 62

def get_task_by_id(id)
  project.story(id, fields: STORY_FIELDS)
end

#get_workObject



49
50
51
# File 'lib/pt/client.rb', line 49

def get_work
  project.stories(filter: 'state:unscheduled,unstarted,started', fields: STORY_FIELDS )
end

#mark_task_as(task, state) ⇒ Object



133
134
135
136
137
# File 'lib/pt/client.rb', line 133

def mark_task_as(task, state)
  task = get_story(task.id)
  task.current_state = state
  task.save
end

#search_for_story(query, params = {}) ⇒ Object



57
58
59
60
# File 'lib/pt/client.rb', line 57

def search_for_story(query, params={})
  params[:filter] =  "#{query}"
  get_stories(params)
end