Class: KiPivotal::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/ki_pivotal/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = Kiseru::Config[:ki_pivotal]) ⇒ Api

Returns a new instance of Api.



7
8
9
# File 'lib/ki_pivotal/api.rb', line 7

def initialize(config = Kiseru::Config[:ki_pivotal])
  @config = config
end

Instance Method Details

#get_token(username, password) ⇒ Object

TODO - this is only used internally, unlike the other methods It also doesn’t return JSON because it is used internally



13
14
15
# File 'lib/ki_pivotal/api.rb', line 13

def get_token(username, password)
  PivotalTracker::Client.token(username, password)
end

#members_of_project(project_id, opts = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ki_pivotal/api.rb', line 50

def members_of_project(project_id, opts = {})
  with_session do
    pretty = opts.fetch(:pretty) { true }

    project = get_project(project_id)

    data = project.memberships.all.map do |member|
      { 'name' => member.name,
        'email' => member.email,
        'intials' => member.initials,
        'role' => member.role}
    end

    MultiJson.dump(data, :pretty => pretty)
  end
end

#new_story(project_id, json) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ki_pivotal/api.rb', line 34

def new_story(project_id, json)
  from_and_to_json(json, true) do |data|
    with_session do
      data.delete('ki_type')

      # You must provide current_state or the Pivotal UI will give an error
      defaults = {
        :current_state => 'unscheduled'
      }

      new_story = add_story_to_project(project_id, defaults.merge(data))
      new_story
    end
  end
end

#projects(opts = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ki_pivotal/api.rb', line 17

def projects(opts = {})
  with_session do
    pretty = opts.fetch(:pretty) { true }

    projects = PivotalTracker::Project.all
    data = []
    projects.each do |project|
      data <<
        {
        'id' => project.id,
        'name' => project.name
      }
    end
    MultiJson.dump(data, :pretty => pretty)
  end
end

#stories_in_project(project_id, opts = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ki_pivotal/api.rb', line 67

def stories_in_project(project_id, opts = {})
  with_session do
    pretty = opts.fetch(:pretty) { true }

    project_ids = project_id.to_s.split(',')
    data = []
    project_ids.each do |project_id|
      project = get_project(project_id)

      data += project.stories.all.inject([]) { |array, story| array << story_to_hash(story).merge({'project_id' => project_id}) }

    end
    MultiJson.dump(data, :pretty => pretty)
  end
end

#story_in_project(story_id, project_id, opts = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ki_pivotal/api.rb', line 83

def story_in_project(story_id, project_id, opts = {})
  with_session do
    pretty = opts.fetch(:pretty) { true }

    project = get_project(project_id)

    story = project.stories.find(story_id)

    raise ApiError, "No story #{story_id} for project #{project_id} ('#{project.name}')" if story.nil?

    data = story_to_hash(story).
      merge({'project_id' => project_id}).
      merge({:notes => story.notes.all.map { |n| note_to_hash(n) }})

    MultiJson.dump(data, :pretty => pretty)
  end
end