Browse files

Adding VCR to record http requests

  • Loading branch information...
1 parent 73b2017 commit 5480d77b9f2a853ce97d5c07fc1c6c13a5d2e444 Leonardo Campos committed Mar 12, 2012
View
3 Gemfile
@@ -29,12 +29,13 @@ gem 'jquery-rails'
# gem 'capistrano'
# To use debugger
-# gem 'ruby-debug19', :require => 'ruby-debug'
+gem 'ruby-debug19', :require => 'ruby-debug'
group :test do
# Pretty printed test output
gem 'turn', '0.8.2', :require => false
gem 'rspec', '= 2.7.0'
gem 'rspec-rails', '= 2.7.0'
gem 'mocha'
+ gem "vcr", "~> 2.0.0"
end
View
17 Gemfile.lock
@@ -30,6 +30,7 @@ GEM
activesupport (3.1.3)
multi_json (~> 1.0)
ansi (1.4.2)
+ archive-tar-minitar (0.5.2)
arel (2.2.3)
builder (3.0.0)
coffee-rails (3.1.1)
@@ -39,6 +40,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.2.0)
+ columnize (0.3.6)
diff-lcs (1.1.3)
erubis (2.7.0)
execjs (1.3.0)
@@ -49,6 +51,8 @@ GEM
railties (~> 3.0)
thor (~> 0.14)
json (1.6.5)
+ linecache19 (0.5.12)
+ ruby_core_source (>= 0.1.4)
mail (2.3.2)
i18n (>= 0.4.0)
mime-types (~> 1.16)
@@ -100,6 +104,16 @@ GEM
activesupport (~> 3.0)
railties (~> 3.0)
rspec (~> 2.7.0)
+ ruby-debug-base19 (0.11.25)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby_core_source (>= 0.1.4)
+ ruby-debug19 (0.11.6)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby-debug-base19 (>= 0.11.19)
+ ruby_core_source (0.1.5)
+ archive-tar-minitar (>= 0.5.2)
sass (3.1.15)
sass-rails (3.1.5)
actionpack (~> 3.1.0)
@@ -122,6 +136,7 @@ GEM
uglifier (1.2.3)
execjs (>= 0.3.0)
multi_json (>= 1.0.2)
+ vcr (2.0.0)
PLATFORMS
ruby
@@ -134,7 +149,9 @@ DEPENDENCIES
rally_rest_api
rspec (= 2.7.0)
rspec-rails (= 2.7.0)
+ ruby-debug19
sass-rails (~> 3.1.5)
sqlite3
turn (= 0.8.2)
uglifier (>= 1.0.3)
+ vcr (~> 2.0.0)
View
5 config/rally_account.yml.sample
@@ -1,2 +1,3 @@
-username: ""
-password: ""
+test:
+ username: ""
+ password: ""
View
10 lib/scrum_board/client.rb
@@ -1,7 +1,7 @@
module ScrumBoard
class Client
def self.find_team_by_name(name)
- @rally_client.find_team_by_name(name)
+ client.find_team_by_name(name)
end
def self.find_sprints_by_team(team_name)
@@ -17,7 +17,7 @@ def self.client
class RallyClient
def initialize
- @connector = RallyConnector.new(:username => '', :password => '')
+ @connector = RallyConnector.new(:username => rally_configuration[:username], :password => rally_configuration[:password])
@conn = @connector.connect
@workspace = @connector.workspace
end
@@ -29,6 +29,12 @@ def find_team_by_name(name)
project.name.downcase == name
end
end
+
+ private
+ def rally_configuration
+ @rally_configuration ||= ScrumBoard::Yml.read_from_config("rally_account")
+ @rally_configuration
+ end
end
class RallyConnector
View
23 lib/scrum_board/rally_client.rb
@@ -0,0 +1,23 @@
+module ScrumBoard
+ class RallyClient
+ def initialize
+ @connector = RallyConnector.new(:username => rally_configuration[:username], :password => rally_configuration[:password])
+ @conn = @connector.connect
+ @workspace = @connector.workspace
+ end
+
+ def find_team_by_name(name)
+ #connector.workspace.projects[7].iterations.find {|sprint| sprint.name == 'Sprint #53'}.end_date
+ #connector.workspace.projects[7].iterations.find {|sprint| sprint.name == 'Sprint #54'}.state
+ @workspace.projects.find do |project|
+ project.name.downcase == name
+ end
+ end
+
+ private
+ def rally_configuration
+ @rally_configuration ||= ScrumBoard::Yml.read_from_config("rally_account")
+ @rally_configuration
+ end
+ end
+end
View
53 lib/scrum_board/rally_connector.rb
@@ -0,0 +1,53 @@
+module ScrumBoard
+ class RallyConnector
+ attr_accessor :username, :password, :custom_headers
+ attr_reader :conn, :workspace, :schedule_states, :defect_states, :api_version
+
+ def initialize(options = {})
+ @username = options[:username]
+ @password = options[:password]
+ @api_version = '1.29'
+
+ if options[:custom_headers]
+ @custom_headers = options[:custom_headers]
+ else
+ @custom_headers = CustomHttpHeader.new
+ @custom_headers.name = 'CodebaseHQ to Rally Connector'
+ @custom_headers.version = 'v0.0.0'
+ @custom_headers.vendor = 'Abril Group'
+ end
+
+ @schedule_states = nil
+ @defect_states = nil
+ @workspace = nil
+ @conn = nil
+ end
+
+ def find_workspace(workspace_name = nil)
+ return nil unless (workspace_name and @conn)
+
+ begin
+ workspace = @conn.user.subscription.workspaces.find { |w| w.name == workspace_name && w.state == 'Open' }
+
+ raise "Couldn't find workspace #{workspace_name}" if workspace.nil?
+
+ return workspace
+ rescue => e
+ Rails.logger.error("\n#{e.to_s}")
+ return nil
+ end
+ end
+
+ def connect
+ begin
+ @conn = RallyRestAPI.new(:username => @username, :password => @password, :version => @api_version, :http_headers => @custom_headers)
+ @workspace = find_workspace('Projetos Abril Midia')
+
+ return @conn
+ rescue => e
+ puts "Could not connect to Rally: #{e.to_s}"
+ return nil
+ end
+ end
+ end
+end
View
28 lib/scrum_board/yml.rb
@@ -0,0 +1,28 @@
+module ScrumBoard
+
+ require "yaml"
+ require "active_support"
+
+ class Yml
+ def self.read(file)
+ yaml_file = "#{file}.yml"
+ YAML::load_file(yaml_file)
+ end
+
+ def self.read_from_config(file, env = ::Rails.env)
+ yaml_file = File.join("config",file)
+ configs = env ? read(yaml_file)[::Rails.env] : read(yaml_file)
+ configs.is_a?(Hash) ? self.symbolize_keys(configs) : configs
+ end
+
+ def self.symbolize_keys(hash)
+ hash.symbolize_keys!
+
+ hash.each_value do |value|
+ self.symbolize_keys value if value.is_a?(Hash)
+ end
+ end
+
+ end
+
+end
View
49 spec/lib/scrum_board/client_spec.rb
@@ -0,0 +1,49 @@
+require_relative '../../spec_helper'
+
+describe ScrumBoard::Client do
+ context 'class' do
+ it 'should respond to find_team_by_name' do
+ ScrumBoard::Client.should respond_to(:find_team_by_name)
+ end
+
+ it 'should respond to find_sprints_by_team' do
+ ScrumBoard::Client.should respond_to(:find_sprints_by_team)
+ end
+
+ context 'on receiving a call to find_team_by_name' do
+ before :each do
+ ScrumBoard::Client.instance_variable_set('@rally_client', nil)
+ end
+
+ it 'should create a rally_client instance and call find_team_by_name' do
+ ScrumBoard::Client.instance_variable_get('@rally_client').should be_nil
+
+ stubbed = ScrumBoard::RallyClient.new
+ ScrumBoard::RallyClient.expects(:new).once.returns(stubbed)
+ stubbed.expects(:find_team_by_name).once
+
+ ScrumBoard::Client.find_team_by_name("NOME")
+ end
+ end
+
+ context 'on receiving a call to find_sprints_by_team' do
+ before :each do
+ ScrumBoard::Client.instance_variable_set('@rally_client', nil)
+ end
+
+ it 'should create a rally_client instance, call find_team_by_name and iteration on the returned value' do
+ ScrumBoard::Client.instance_variable_get('@rally_client').should be_nil
+
+ stubbed = ScrumBoard::RallyClient.new
+ rest_object = {}
+ rest_object.expects(:iterations).once
+
+ ScrumBoard::RallyClient.expects(:new).once.returns(stubbed)
+ stubbed.expects(:find_team_by_name).once.returns(rest_object)
+
+
+ ScrumBoard::Client.find_sprints_by_team("NOME")
+ end
+ end
+ end
+end
View
5 spec/lib/scrum_board/rally_client_spec.rb
@@ -0,0 +1,5 @@
+require_relative '../../spec_helper'
+
+describe ScrumBoard::RallyClient do
+
+end
View
5 spec/lib/scrum_board/test.yml
@@ -0,0 +1,5 @@
+test:
+ pessoa:
+ nome: "zezinho"
+ atributo:
+ idade: 12
View
16 spec/lib/scrum_board/yml_spec.rb
@@ -0,0 +1,16 @@
+require 'spec_helper'
+
+describe ::ScrumBoard::Yml do
+
+ it 'deveria ler um arquivo de config valido' do
+ file_path = File.join(Rails.root,"spec/lib/scrum_board/test")
+ File.expects(:join).with('config','test').returns(file_path)
+
+ @yml = ::ScrumBoard::Yml.read_from_config('test')
+ @yml.should == {:pessoa => {:nome => "zezinho", :atributo => { :idade => 12 }}}
+ end
+
+ it "deveria retornar nulo quando o arquivo nao existe" do
+ lambda { ::ScrumBoard::Yml.read_from_config('invalid_file') }.should raise_error(Errno::ENOENT, "No such file or directory - config/invalid_file.yml")
+ end
+end
View
3 spec/spec_helper.rb
@@ -2,7 +2,8 @@
ENV["RAILS_ENV"] ||= 'test'
require_relative File.expand_path("../../config/environment", __FILE__)
-require 'rspec/rails'
+require 'rspec/rails'
+require 'ruby-debug'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.

0 comments on commit 5480d77

Please sign in to comment.