achetepe 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 47ec06bb484234f0d4b3f8ea850463f0dfc36669
4
- data.tar.gz: fddfdb1d0847184309f913234db4dbd81301614f
3
+ metadata.gz: c73e051ac431425db4964c64469137f4854660c7
4
+ data.tar.gz: adb35a468bf7718d056b64983edc2522801863c9
5
5
  SHA512:
6
- metadata.gz: a153b193691fa10305ee4f762515ed904c46930852ec48e117259b155fb606480d78e770ad4bd8e9dab3ea81df6c016025dc14c11dd8940cb40d9cec07325232
7
- data.tar.gz: 96e15f8a88d3bb0c3b3af5bb4b71afb8cd244efb7c7fd6ab086f72a30631df49d3a5ed6df045dd2d7b29455dcf3f5c06ac293f5fc26fe3986f0892fb703b2893
6
+ metadata.gz: f516ae17332a3823edb8df0b5c7edf31e8bcb49268e9aba755caa94b70b068b04a7b53b7209fdf96d65496f78473bca92df0a814113ea2377327e89cb0427add
7
+ data.tar.gz: 6d27ccf5cd48f58ded01368668706ca3de5bb44e0e76950780627de6a5668ab57d6237f206d11af90f23a2e9f3e7a5daaf8d0b93ae62c3fffe5861fa80849b14
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'achetepe'
3
- s.version = '0.0.1'
3
+ s.version = '0.0.2'
4
4
  s.date = Time.now.strftime('%Y-%m-%d')
5
5
  s.summary = 'Asynchronous HTTP Requests using Threads'
6
6
  s.description = '`achetepe` is a small and simple library to execute a block of code after an asynchronous HTTP request.'
@@ -7,9 +7,15 @@ class Achetepe
7
7
  uri = URI(url)
8
8
 
9
9
  Thread.new do
10
- response = Net::HTTP.get_response(uri)
10
+ yield Net::HTTP.get_response(uri)
11
+ end
12
+ end
11
13
 
12
- yield response
14
+ def post(url, params = {}, &block)
15
+ uri = URI(url)
16
+
17
+ Thread.new do
18
+ yield Net::HTTP.post_form(uri, params)
13
19
  end
14
20
  end
15
21
 
data/rakefile CHANGED
@@ -10,13 +10,13 @@ namespace :test do
10
10
  require File.expand_path("./test/helper", File.dirname(__FILE__))
11
11
 
12
12
  puts 'Starting TestApp server'
13
-
14
- $pid = fork do
15
- Rack::Server.start app: TestApp, Port: 9494, daemonize: true
13
+ fork do
14
+ Rack::Server.start app: TestApp,
15
+ Port: 9494,
16
+ daemonize: true,
17
+ pid: TEST_APP_PID
16
18
  end
17
19
 
18
- system "echo #{$pid} > #{TEST_APP_PID}"
19
-
20
20
  sleep(1.5)
21
21
  end
22
22
  end
@@ -3,8 +3,8 @@ require_relative './helper'
3
3
  Protest.describe 'Get' do
4
4
 
5
5
  it 'return a Net::HTTPOK on a valid url' do
6
- response = Achetepe.get('http://localhost:9494/') do |response|
7
- assert response.class.ancestors.include?(Net::HTTPResponse)
6
+ response = Achetepe.get(TestApp.url) do |response|
7
+ assert_equal response.class, Net::HTTPOK
8
8
  end
9
9
 
10
10
  assert_equal Thread, response.class
@@ -12,8 +12,8 @@ Protest.describe 'Get' do
12
12
  end
13
13
 
14
14
  it 'return a Net::HTTPNotFound' do
15
- response = Achetepe.get('http://localhost:9494/not_found') do |response|
16
- assert response.class.ancestors.include?(Net::HTTPResponse)
15
+ response = Achetepe.get(TestApp.url + 'not_found') do |response|
16
+ assert_equal response.class, Net::HTTPNotFound
17
17
  end
18
18
 
19
19
  assert_equal Thread, response.class
@@ -32,5 +32,28 @@ class TestApp < Cuba
32
32
  res.status = 404
33
33
  end
34
34
  end
35
+
36
+ on post do
37
+ on "slow_post" do
38
+ sleep(2)
39
+ res.write "It takes too much"
40
+ end
41
+
42
+ on "login" do
43
+
44
+ # POST /login, user: foo, pass: baz
45
+ on param("user"), param("pass") do |user, pass|
46
+ res.write "#{user}:#{pass}" #=> "foo:baz"
47
+ end
48
+
49
+ # If the params `user` and `pass` are not provided,
50
+ # this block will get executed.
51
+ on true do
52
+ res.status = 400
53
+ res.write "You need to provide user and pass!"
54
+ end
55
+ end
56
+
57
+ end
35
58
  end
36
59
  end
@@ -0,0 +1,46 @@
1
+ require_relative './helper'
2
+
3
+ Protest.describe 'Post' do
4
+
5
+ it 'return a Net::HTTPOK on a valid url' do
6
+ params = { user: 'emancu', pass: 'achetepe_rocks' }
7
+
8
+ response = Achetepe.post(TestApp.url + 'login', params) do |response|
9
+ assert_equal response.class, Net::HTTPOK
10
+ assert response.class.ancestors.include?(Net::HTTPResponse)
11
+ end
12
+
13
+ assert_equal Thread, response.class
14
+ response.join
15
+ end
16
+
17
+ it 'return a Net::HTTPBadRequest' do
18
+ response = Achetepe.post(TestApp.url + 'login') do |response|
19
+ assert_equal response.class, Net::HTTPBadRequest
20
+ end
21
+
22
+ assert_equal Thread, response.class
23
+ response.join
24
+ end
25
+
26
+ it "executes the block as soon as a response is received" do
27
+ params = { user: 'emancu', pass: 'achetepe_rocks' }
28
+ ordered_responses = []
29
+
30
+ get1 = Achetepe.post(TestApp.url + 'slow_post', params) do |response|
31
+ ordered_responses << 'slow_post'
32
+ end
33
+
34
+ sleep(1)
35
+
36
+ get2 = Achetepe.post(TestApp.url + 'login', params) do |response|
37
+ ordered_responses << 'root'
38
+ end
39
+
40
+ get1.join
41
+ get2.join
42
+
43
+ assert_equal ['root', 'slow_post'], ordered_responses
44
+ end
45
+
46
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: achetepe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emiliano Mancuso
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-06 00:00:00.000000000 Z
11
+ date: 2013-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: protest
@@ -52,6 +52,7 @@ files:
52
52
  - achetepe.gemspec
53
53
  - test/get_test.rb
54
54
  - test/helper.rb
55
+ - test/post_test.rb
55
56
  homepage: http://github.com/emancu/achetepe
56
57
  licenses:
57
58
  - MIT
@@ -79,3 +80,4 @@ summary: Asynchronous HTTP Requests using Threads
79
80
  test_files:
80
81
  - test/get_test.rb
81
82
  - test/helper.rb
83
+ - test/post_test.rb