idra 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/Readme.md +41 -0
  3. data/lib/idra/version.rb +3 -0
  4. data/lib/idra.rb +71 -0
  5. metadata +62 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 68e9b10e44c01a0176ab696094b0a400b868b21a
4
+ data.tar.gz: 69e24798619594a1452f8a0d23bfa11ef2feaf1f
5
+ SHA512:
6
+ metadata.gz: 630a6c381614f0df48771072c7e89f5b9db984a7d834faf8e01dc7f0042ffe3c6052084037615cb053cdb617319afc1823370da3dc287d2a7685fe28477dc80f
7
+ data.tar.gz: 704b463912a650ff1f0bcb1221fc88e41d84c681ddaba4a6e95c78fc54b87eb49b0552235046c19f05f86c419853dbf90b1d774a0f1e71418d899cb399a21d55
data/Readme.md ADDED
@@ -0,0 +1,41 @@
1
+ # Idra
2
+
3
+ Idra is a tiny wrapper around Typoheus Hydra to make concurrent http requests.
4
+
5
+ Usually you don't need this, the Typoheus API itself is very good ( https://github.com/typhoeus/typhoeus#making-parallel-requests ), but I like to have an even slimmer API :)
6
+
7
+
8
+ ```rb
9
+ idra = Idra.new
10
+
11
+ idra.add_request :user1, "https://api.github.com/users/i0rek"
12
+ idra.add_request :user2, "https://api.github.com/users/makevoid"
13
+ idra.add_request :user3, "https://api.github.com/users/pauldix"
14
+ idra.add_request :user4, "https://api.github.com/users/dbalatero"
15
+
16
+ idra.response_for :user1 do |response|
17
+ puts response.body
18
+ end
19
+
20
+ idra.response_for :user2 do |response|
21
+ puts response.body
22
+ end
23
+
24
+ # ...
25
+ ```
26
+
27
+ or:
28
+
29
+ ```rb
30
+ idra.refresh # makes the requests again
31
+
32
+ idra.run do |request|
33
+ puts request.name # => :userX
34
+ puts request.response.body # => {...}
35
+ end
36
+ ```
37
+
38
+
39
+ You can read the code on github: <https://github.com/makevoid/idra/blob/master/lib/idra.rb> -- It's just few lines.
40
+
41
+ enjoy
@@ -0,0 +1,3 @@
1
+ class Idra
2
+ VERSION = "0.1.0"
3
+ end
data/lib/idra.rb ADDED
@@ -0,0 +1,71 @@
1
+ require 'typhoeus'
2
+ require_relative "idra/version"
3
+
4
+ class Typhoeus::Request
5
+ attr_accessor :name
6
+ end
7
+
8
+ class Idra
9
+ attr_reader :requests
10
+ attr_reader :enabled
11
+
12
+ def initialize
13
+ @hydra = Typhoeus::Hydra.new
14
+ @requests = []
15
+ @enabled = true
16
+ end
17
+
18
+ def add_request(name, *request)
19
+ request = Typhoeus::Request.new *request
20
+ request.name = name
21
+ @requests << request
22
+ @hydra.queue request
23
+ request
24
+ end
25
+
26
+ def response_for(name, &block)
27
+ run_hydra
28
+ request = @requests.find{ |req| req.name == name }
29
+ response = request.response
30
+ if block
31
+ block.call response
32
+ else
33
+ response
34
+ end
35
+ end
36
+
37
+ def run(&block)
38
+ run_hydra
39
+ @requests.each do |req|
40
+ block.call req if block
41
+ end
42
+ end
43
+
44
+ def refresh
45
+ @enabled = false
46
+ end
47
+
48
+ alias :enabled? :enabled
49
+
50
+ protected
51
+
52
+ def run_hydra
53
+ @hydra.run if enabled?
54
+ @enabled = false
55
+ true
56
+ end
57
+
58
+ end
59
+
60
+ # usage:
61
+ #
62
+ # idra = Idra.new
63
+ #
64
+ # idra.add_request "https://api.github.com/users/i0rek"
65
+ # idra.add_request "https://api.github.com/users/makevoid"
66
+ # idra.add_request "https://api.github.com/users/pauldix"
67
+ # idra.add_request "https://api.github.com/users/dbalatero"
68
+ #
69
+ # idra.run do |request|
70
+ # puts request.response.body
71
+ # end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: idra
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Francesco 'makevoid' Canessa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: typhoeus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.7.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.7.0
27
+ description: Tiny wrapper around Typoheus Hydra to make concurrent http requests even
28
+ simpler. Usually you don't need this, the Typoheus API itself is very good ( https://github.com/typhoeus/typhoeus#making-parallel-requests
29
+ ), but I like to have an even slimmer API :)
30
+ email: makevoid@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - Readme.md
36
+ - lib/idra.rb
37
+ - lib/idra/version.rb
38
+ homepage: http://github.com/makevoid?tab=repositories
39
+ licenses: []
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.4.3
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Idra is a tiny wrapper around Typoheus Hydra to make concurrent http requests
61
+ even simpler.
62
+ test_files: []