Permalink
Browse files

Set up pull request information for Trajectory.

  • Loading branch information...
drapergeek committed Oct 22, 2012
1 parent e6c3b89 commit 0d40acaabb54b64c8e5e926aeeffea92c21c354b
Showing with 65 additions and 4 deletions.
  1. +35 −3 services/trajectory.rb
  2. +30 −1 test/trajectory_test.rb
View
@@ -1,15 +1,47 @@
class Service::Trajectory < Service
string :api_key
+ BASE_URL = "https://www.apptrajectory.com/api/payloads?api_key="
def receive_push
- raise_config_error "Missing 'api_key'" if data['api_key'].to_s == ''
+ send_to_trajectory
+ end
+
+ def receive_pull_request
+ send_to_trajectory
+ end
+ private
+
+ def send_to_trajectory
+ set_http_headers
+ response = send_post
+ raise_config_error_for_bad_status(response)
+ end
+
+ def set_http_headers
http.headers['content-type'] = 'application/json'
+ end
- res = http_post "https://www.apptrajectory.com/api/payloads?api_key=#{data['api_key'].to_s}", JSON.generate(payload)
+ def send_post
+ http_post full_url, json_payload
+ end
+
+ def full_url
+ BASE_URL + api_key
+ end
- if res.status < 200 || res.status > 299
+ def raise_config_error_for_bad_status(response)
+ if response.status < 200 || response.status > 299
raise_config_error
end
end
+
+ def api_key
+ raise_config_error "Missing 'api_key'" if data['api_key'].to_s == ''
+ data['api_key'].to_s
+ end
+
+ def json_payload
+ JSON.generate(payload)
+ end
end
View
@@ -5,9 +5,21 @@ def setup
@stubs = Faraday::Adapter::Test::Stubs.new
end
+ def test_raise_config_error_without_api_key
+ assert_raise Service::ConfigurationError do
+ svc = service({}, payload)
+ svc.receive_push
+ end
+
+ assert_raise Service::ConfigurationError do
+ svc = service({}, payload)
+ svc.receive_pull_request
+ end
+ end
+
def test_push
@stubs.post '/api/payloads?api_key=test_api_key' do |env|
- assert_equal 'application/json', env[:request_headers]['Content-Type']
+ confirm_trajectory_receives_request(env)
[200, {}, '']
end
@@ -17,7 +29,24 @@ def test_push
@stubs.verify_stubbed_calls
end
+ def test_pull_request
+ @stubs.post '/api/payloads?api_key=test_api_key' do |env|
+ confirm_trajectory_receives_request(env)
+ [200, {}, '']
+ end
+
+ svc = service({'api_key' => 'test_api_key'}, payload)
+ svc.receive_pull_request
+
+ @stubs.verify_stubbed_calls
+ end
+
def service(*args)
super Service::Trajectory, *args
end
+
+ def confirm_trajectory_receives_request(env)
+ assert_equal 'application/json', env[:request_headers]['Content-Type']
+ assert_equal 'https://www.apptrajectory.com/api/payloads?api_key=test_api_key', env[:url].to_s
+ end
end

0 comments on commit 0d40aca

Please sign in to comment.