pivotal 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pivotal.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem "rspec", "~> 2.11.0"
8
+ gem "guard-rspec"
9
+ end
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { "spec" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Thom Mahoney & Josh Lane
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # Pivotal
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'pivotal'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install pivotal
18
+
19
+ ## Releasing
20
+
21
+ gem bump -trv patch
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,70 @@
1
+ require 'logger'
2
+
3
+ class Pivotal::Client < Cistern::Service
4
+
5
+ model_path "pivotal/models"
6
+ request_path "pivotal/requests"
7
+
8
+ model :project
9
+ collection :projects
10
+ request :get_project
11
+ request :get_projects
12
+
13
+ model :iteration
14
+ collection :iterations
15
+ request :get_iteration
16
+ request :get_iterations
17
+
18
+ model :story
19
+
20
+ recognizes :token, :url
21
+
22
+ class Real
23
+
24
+ def initialize(options={})
25
+ @token = options[:token] || File.read(File.expand_path("~/.pivotal"))
26
+ @url = options[:url] || "https://www.pivotaltracker.com/services/v3/"
27
+
28
+ raise "Missing token" unless @token
29
+
30
+ @logger = options[:logger] || Logger.new(STDOUT)
31
+ adapter = options[:adapter] || Faraday.default_adapter
32
+ connection_options = options[:connection_options] || {ssl: {verify: false}}
33
+
34
+ @connection = Faraday.new({url: @url}.merge(connection_options)) do |builder|
35
+ # response
36
+ builder.use Faraday::Response::RaiseError
37
+ builder.response :xml, :content_type => /\bxml$/
38
+
39
+ # request
40
+ builder.request :multipart
41
+ builder.request :xml
42
+
43
+ builder.use Pivotal::Logger, @logger
44
+ builder.adapter adapter
45
+ end
46
+ end
47
+
48
+ def request(options={})
49
+ method = options[:method] || :get
50
+ url = File.join(@url, options[:path])
51
+ params = options[:params] || {}
52
+ body = options[:body]
53
+ headers = {
54
+ "X-TrackerToken" => @token,
55
+ }.merge(options[:headers] || {})
56
+
57
+ response = @connection.send(method) do |req|
58
+ req.url(url)
59
+ req.headers.merge!(headers)
60
+ req.params.merge!(params)
61
+ req.body = body
62
+ end
63
+ end
64
+ end # Real
65
+
66
+ class Mock
67
+ def initialize(options={})
68
+ end
69
+ end # Mock
70
+ end # Pivotal::Client
@@ -0,0 +1,56 @@
1
+ require 'faraday'
2
+
3
+ module Pivotal
4
+ # Request middleware that encodes the body as xml.
5
+ #
6
+ # Processes only requests with matching Content-type or those without a type.
7
+ # If a request doesn't have a type but has a body, it sets the Content-type
8
+ # to xml MIME-type.
9
+ #
10
+ # Doesn't try to encode bodies that already are in string form.
11
+ class EncodeXml < Faraday::Middleware
12
+ CONTENT_TYPE = 'Content-Type'.freeze
13
+ MIME_TYPE = 'application/xml'.freeze
14
+
15
+ dependency do
16
+ require 'xmlsimple' unless defined?(XmlSimple)
17
+ end
18
+
19
+ def call(env)
20
+ match_content_type(env) do |data|
21
+ env[:body] = encode data
22
+ end
23
+ @app.call env
24
+ end
25
+
26
+ def encode(data)
27
+ XmlSimple.xml_out(data)
28
+ end
29
+
30
+ def match_content_type(env)
31
+ if process_request?(env)
32
+ env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
33
+ yield env[:body] unless env[:body].respond_to?(:to_str)
34
+ end
35
+ end
36
+
37
+ def process_request?(env)
38
+ type = request_type(env)
39
+ has_body?(env) and (type.empty? or type == MIME_TYPE)
40
+ end
41
+
42
+ def has_body?(env)
43
+ body = env[:body] and !(body.respond_to?(:to_str) and body.empty?)
44
+ end
45
+
46
+ def request_type(env)
47
+ type = env[:request_headers][CONTENT_TYPE].to_s
48
+ type = type.split(';', 2).first if type.index(';')
49
+ type
50
+ end
51
+ end
52
+ end
53
+
54
+ if Faraday.respond_to? :register_middleware
55
+ Faraday.register_middleware(:request, {:xml => lambda { Pivotal::EncodeXml }})
56
+ end
@@ -0,0 +1,34 @@
1
+ require 'forwardable'
2
+
3
+ class Pivotal::Logger < Faraday::Response::Middleware
4
+ extend Forwardable
5
+
6
+ def initialize(app, logger = nil)
7
+ super(app)
8
+ @logger = logger || begin
9
+ require 'logger'
10
+ ::Logger.new(STDOUT)
11
+ end
12
+ end
13
+
14
+ def_delegators :@logger, :debug, :info, :warn, :error, :fatal
15
+
16
+ def call(env)
17
+ info "#{env[:method]} #{env[:url].to_s}"
18
+ debug('request') { dump_headers env[:request_headers] }
19
+ debug('request.body') { env[:body] }
20
+ super
21
+ end
22
+
23
+ def on_complete(env)
24
+ info('Status') { env[:status].to_s }
25
+ debug('response') { dump_headers env[:response_headers] }
26
+ #debug('response.body') { env[:body] }
27
+ end
28
+
29
+ private
30
+
31
+ def dump_headers(headers)
32
+ headers.map { |k, v| "#{k}: #{v.inspect}" }.join("\n")
33
+ end
34
+ end
@@ -0,0 +1,19 @@
1
+ class Pivotal::Client::Iteration < Cistern::Model
2
+ identity :id
3
+
4
+ attribute :name
5
+ attribute :number, type: :integer
6
+ attribute :start, type: :date
7
+ attribute :finish, type: :date
8
+ attribute :team_strength, type: :decimal
9
+
10
+ attr_reader :stories
11
+
12
+ def iterations
13
+ self.iterations.all(project_id: self.identity)
14
+ end
15
+
16
+ def stories=(stories_hash)
17
+ @stories = stories_hash.map{|story| Pivotal::Client::Story.new(story)}
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ class Pivotal::Client::Iterations < Cistern::Collection
2
+ model Pivotal::Client::Iteration
3
+
4
+ attribute :project_id
5
+
6
+ def all(params={})
7
+ response = connection.get_iterations(params.merge("project_id" => self.project_id))
8
+
9
+ self.clone.load(response.body["iterations"])
10
+ end
11
+
12
+ def current
13
+ all("id" => "current").first
14
+ end
15
+
16
+ def get(id)
17
+ if data = connection.get_iteration("project_id" => self.project_id, "id" => id).body["iteration"]
18
+ new(data)
19
+ else
20
+ nil
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ class Pivotal::Client::Project < Cistern::Model
2
+ identity :id
3
+
4
+ attribute :name
5
+ attribute :iteration_length, type: :integer
6
+ attribute :account
7
+ attribute :current_velocity, type: :integer
8
+ attribute :memberships
9
+
10
+ def iterations
11
+ connection.iterations.tap{|i| i.project_id = self.identity}
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ class Pivotal::Client::Projects < Cistern::Collection
2
+ model Pivotal::Client::Project
3
+
4
+ def all(params={})
5
+ response = connection.get_projects
6
+
7
+ self.clone.load(response.body["projects"])
8
+ end
9
+
10
+ def get(id)
11
+ if data = connection.get_project("id" => id).body["project"]
12
+ new(data)
13
+ else
14
+ nil
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,26 @@
1
+ class Pivotal::Client::Story < Cistern::Model
2
+ identity :id
3
+
4
+ attribute :accepted_at, type: :date
5
+ attribute :created_at, type: :date
6
+ attribute :current_state
7
+ attribute :description
8
+ attribute :labels
9
+ attribute :name
10
+ attribute :owned_by
11
+ attribute :project_id, type: :integer
12
+ attribute :requested_by
13
+ attribute :story_type
14
+ attribute :updated_at, type: :date
15
+ attribute :url
16
+
17
+ attr_reader :stories
18
+
19
+ def iterations
20
+ self.iterations.all(project_id: self.identity)
21
+ end
22
+
23
+ def stories=(stories_hash)
24
+ @stories = stories.map{|story| Pivotal::Client::Story.new(story)}
25
+ end
26
+ end
@@ -0,0 +1,30 @@
1
+ class Pivotal::Client
2
+ class Real
3
+ def get_iteration(params={})
4
+ id = params["id"]
5
+ project_id = params["project_id"]
6
+
7
+ request(
8
+ :path => "/projects/#{project_id}/iterations/#{id}"
9
+ )
10
+ end
11
+ end # Real
12
+ class Mock
13
+ def get_iteration(params={})
14
+ id = params["id"]
15
+ project_id = params["project_id"]
16
+ iteration = self.data[:iterations][id]
17
+
18
+ if iteration
19
+ response(
20
+ :body => {"iteration" => iteration},
21
+ :status => 200
22
+ )
23
+ else
24
+ response(
25
+ :status => 404
26
+ )
27
+ end
28
+ end
29
+ end # Mock
30
+ end
@@ -0,0 +1,31 @@
1
+ class Pivotal::Client
2
+ class Real
3
+ def get_iterations(params={})
4
+ id = params["id"]
5
+ project_id = params["project_id"]
6
+
7
+ request(
8
+ :path => "/projects/#{project_id}/iterations/#{id}"
9
+ )
10
+ end
11
+ end # Real
12
+
13
+ class Mock
14
+ def get_iterations(params={})
15
+ id = params["id"]
16
+ project_id = params["project_id"]
17
+ iteration = self.data[:iterations][id]
18
+
19
+ if iteration
20
+ response(
21
+ :body => {"iteration" => iteration},
22
+ :status => 200
23
+ )
24
+ else
25
+ response(
26
+ :status => 404
27
+ )
28
+ end
29
+ end
30
+ end # Mock
31
+ end
@@ -0,0 +1,28 @@
1
+ class Pivotal::Client
2
+ class Real
3
+ def get_project(params={})
4
+ id = params["id"]
5
+
6
+ request(
7
+ :path => "/projects/#{id}"
8
+ )
9
+ end
10
+ end # Real
11
+ class Mock
12
+ def get_project(params={})
13
+ id = params["id"]
14
+ project = self.data[:projects][id]
15
+
16
+ if project
17
+ response(
18
+ :body => {"project" => project},
19
+ :status => 200
20
+ )
21
+ else
22
+ response(
23
+ :status => 404
24
+ )
25
+ end
26
+ end
27
+ end # Mock
28
+ end
@@ -0,0 +1,19 @@
1
+ class Pivotal::Client
2
+ class Real
3
+ def get_projects(params={})
4
+ request(
5
+ :path => "/projects"
6
+ )
7
+ end
8
+ end # Real
9
+ class Mock
10
+ def get_projects(params={})
11
+ projects = self.data[:projects]
12
+
13
+ response(
14
+ :body => {"projects" => projects},
15
+ :status => 200
16
+ )
17
+ end
18
+ end # Mock
19
+ end
@@ -0,0 +1,3 @@
1
+ module Pivotal
2
+ VERSION = "0.0.2"
3
+ end
data/lib/pivotal.rb ADDED
@@ -0,0 +1,13 @@
1
+ require "pivotal/version"
2
+
3
+ require 'cistern'
4
+ require 'xmlsimple'
5
+ require 'faraday'
6
+ require 'faraday_middleware'
7
+
8
+ module Pivotal
9
+ require 'pivotal/encode_xml'
10
+
11
+ autoload :Client, 'pivotal/client'
12
+ autoload :Logger, 'pivotal/logger'
13
+ end
data/pivotal.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pivotal/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "pivotal"
8
+ gem.version = Pivotal::VERSION
9
+ gem.authors = ["Josh Lane & Thom Mahoney"]
10
+ gem.email = ["jlane@engineyard.com", "tmahoney@engineyard.com"]
11
+ gem.description = %q{A client for Pivotal Tracker.}
12
+ gem.summary = %q{A client for Pivotal Tracker using cistern and nokogiri.}
13
+ gem.homepage = "https://github.com/thommahoney/pivotal"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "cistern", "~> 0.0.3"
21
+ gem.add_dependency "xml-simple"
22
+ gem.add_dependency "faraday"
23
+ gem.add_dependency "faraday_middleware"
24
+ gem.add_dependency "multi_xml"
25
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe "projects" do
4
+ let(:client) { Pivotal::Client.new }
5
+ let(:project_id) { 635517 }
6
+
7
+ it "should fetch all projects" do
8
+ client.projects.all.should_not be_nil
9
+ end
10
+
11
+ describe "with a specific project" do
12
+ let(:project) { client.projects.get(project_id) }
13
+
14
+ it "should get project's current iteration" do
15
+ current_iteration = project.iterations.current
16
+ current_iteration.should_not be_nil
17
+
18
+ current_iteration.stories.should_not be_nil
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,16 @@
1
+ require File.expand_path('../../lib/pivotal', __FILE__)
2
+
3
+ Bundler.require(:test)
4
+
5
+ Dir[File.expand_path('../{support,matchers}/*.rb', __FILE__)].each{|f| require(f)}
6
+
7
+ if ENV["MOCK_PIVOTAL"] == "true"
8
+ Pivotal::Client.mock!
9
+ end
10
+
11
+ RSpec.configure do |config|
12
+ config.before(:all) do
13
+ Pivotal::Client.reset! if Pivotal::Client.mocking?
14
+ end
15
+ end
16
+
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pivotal
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Josh Lane & Thom Mahoney
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: cistern
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.0.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.0.3
30
+ - !ruby/object:Gem::Dependency
31
+ name: xml-simple
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: faraday
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: faraday_middleware
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: multi_xml
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: A client for Pivotal Tracker.
95
+ email:
96
+ - jlane@engineyard.com
97
+ - tmahoney@engineyard.com
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - .gitignore
103
+ - Gemfile
104
+ - Guardfile
105
+ - LICENSE.txt
106
+ - README.md
107
+ - Rakefile
108
+ - lib/pivotal.rb
109
+ - lib/pivotal/client.rb
110
+ - lib/pivotal/encode_xml.rb
111
+ - lib/pivotal/logger.rb
112
+ - lib/pivotal/models/iteration.rb
113
+ - lib/pivotal/models/iterations.rb
114
+ - lib/pivotal/models/project.rb
115
+ - lib/pivotal/models/projects.rb
116
+ - lib/pivotal/models/story.rb
117
+ - lib/pivotal/requests/get_iteration.rb
118
+ - lib/pivotal/requests/get_iterations.rb
119
+ - lib/pivotal/requests/get_project.rb
120
+ - lib/pivotal/requests/get_projects.rb
121
+ - lib/pivotal/version.rb
122
+ - pivotal.gemspec
123
+ - spec/project_spec.rb
124
+ - spec/spec_helper.rb
125
+ homepage: https://github.com/thommahoney/pivotal
126
+ licenses: []
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 1.8.24
146
+ signing_key:
147
+ specification_version: 3
148
+ summary: A client for Pivotal Tracker using cistern and nokogiri.
149
+ test_files:
150
+ - spec/project_spec.rb
151
+ - spec/spec_helper.rb