Skip to content

Commit

Permalink
Adds guard and background processes for requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Knadler committed Dec 28, 2012
1 parent a255181 commit 2b385ee
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 23 deletions.
1 change: 1 addition & 0 deletions .rspec
@@ -0,0 +1 @@
--color
1 change: 1 addition & 0 deletions .travis.yml
@@ -1,4 +1,5 @@
language: ruby
bundler_args: --without development
rvm:
- 1.9.3
- 1.9.2
Expand Down
23 changes: 22 additions & 1 deletion Gemfile
@@ -1,4 +1,25 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in git-feats.gemspec
gemspec

# ignored with Travis
group :development do
gem 'yard'

# file system notifications
gem 'rb-inotify', :require => false
gem 'rb-fsevent', :require => false
gem 'rb-fchange', :require => false

# auto testing
gem 'guard-rspec'

# growl notifications for tests
gem 'ruby_gntp'
end

# installed with Travis
group :test do
gem 'rspec'
gem 'simplecov'
end
5 changes: 5 additions & 0 deletions Guardfile
@@ -0,0 +1,5 @@
guard 'rspec' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/git-feats/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
end
5 changes: 1 addition & 4 deletions git-feats.gemspec
Expand Up @@ -43,9 +43,6 @@ desc
s.add_runtime_dependency "json"
s.add_runtime_dependency "faraday", "~> 0.8.4"

s.add_development_dependency "rake", "~> 10.0.2"

#TODO: Remove these.
s.add_development_dependency "rake", "~> 10.0.2"
s.add_development_dependency "rspec"
s.add_development_dependency "simplecov"
end
35 changes: 24 additions & 11 deletions lib/git-feats/api.rb
Expand Up @@ -6,23 +6,36 @@ module API

extend self

URL = 'http://www.gitfeats.com'

def upload_feats
# Post json to git-feats
begin
# Attempt to update feats
conn.post do |req|
req.url '/api/feats'
req.headers['Content-Type'] = 'application/json'
req.body = upload_feats_body.to_json
URL = 'http://localhost:3000'

def upload_feats(background=true)
# spawn the request as a background process
if background
job = fork do
begin
post_feats
rescue
end
end
rescue
Process.detach(job)

# make the request normally
else
response = post_feats
puts response
end
end

private

def post_feats
conn.post do |req|
req.url '/api/feats'
req.headers['Content-Type'] = 'application/json'
req.body = upload_feats_body.to_json
end
end

# Return the faraday connection or create one
def conn
@conn ||= new_connection
Expand Down
20 changes: 17 additions & 3 deletions spec/api_spec.rb
@@ -1,14 +1,28 @@
require File.expand_path('../spec_helper', __FILE__)
require 'git-feats'
require 'faraday'


describe GitFeats::API do
let(:klass) { GitFeats::API }

subject { klass }

describe ".upload_feats" do
let(:meth) { :upload_feats }
subject { klass.method(meth) }
its(:arity) { should eq(0) }
its(:arity) { should eq(-1) }

# request mocks with faraday
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
stub.post('/api/feats') {[200, {}, '{"message":"OK"}']}
end

test = Faraday.new do |builder|
builder.adapter :test, stubs
end

it "display success if response is 200" do
response = test.post('/api/feats')
end
end
end
end
8 changes: 4 additions & 4 deletions spec/checker_spec.rb
Expand Up @@ -22,9 +22,9 @@ def stub_config_exists(exists)
end

describe ".upload_feats" do
let(:meth) { :upload_feats }
subject { klass.method(meth) }
its(:arity) { should eq(0) }
let(:meth) { :upload_feats }
subject { klass.method(meth) }
its(:arity) { should eq(0) }

context "when GitFeats::Config exists" do
let(:upload_string) { "API upload feats report string" }
Expand All @@ -43,4 +43,4 @@ def stub_config_exists(exists)
end
end
end
end
end

0 comments on commit 2b385ee

Please sign in to comment.