httpattack-lib 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1 @@
1
+ The game library for HTTP Attack.
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ require 'spec/rake/spectask'
2
+
3
+ task :default => :spec
4
+
5
+ desc 'Run the specs'
6
+ Spec::Rake::SpecTask.new(:spec) do |t|
7
+ t.spec_opts = ['--color']
8
+ t.spec_files = FileList['spec/**/*_spec.rb']
9
+ end
10
+
11
+ desc 'Run the specs with autotest'
12
+ task :autotest do
13
+ ENV['RSPEC'] = 'true'
14
+ sh 'autotest'
15
+ end
16
+
17
+ desc 'Start an IRB session with the library loaded'
18
+ task :console do
19
+ sh "irb -I ./lib -r 'httpattack'"
20
+ end
21
+
22
+
23
+ #
24
+ # Gem Stuff
25
+ #
26
+ APP_DIR = File.dirname(__FILE__)
27
+ GEMSPEC = File.join(APP_DIR, 'httpattack-lib.gemspec')
28
+ GEMNAME = File.basename(GEMSPEC, '.gemspec')
29
+
30
+ desc 'Build the gem'
31
+ task :gem => ['gem:build']
32
+
33
+ namespace :gem do
34
+ task :build do
35
+ sh "gem build #{GEMSPEC}"
36
+ end
37
+
38
+ desc "Clean the built gem"
39
+ task :clean do
40
+ sh "rm -f #{APP_DIR}/*.gem"
41
+ end
42
+
43
+ desc 'Clean, rebuild and install the gem'
44
+ task :install do
45
+ Rake::Task['gem:clean'].invoke
46
+ Rake::Task['gem:build'].invoke
47
+ gem = FileList["#{APP_DIR}/#{GEMNAME}-*.gem"].first
48
+ sudo = RUBY_PLATFORM !~ /win32/ ? "sudo" : ""
49
+ sh "#{sudo} gem uninstall #{GEMNAME} ; #{sudo} gem install #{gem}"
50
+ end
51
+ end
data/lib/httpattack.rb ADDED
@@ -0,0 +1,7 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__)
2
+
3
+ require 'httpattack/weapons'
4
+
5
+ module HTTPAttack
6
+ VERSION = '0.0.1'
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'httpattack/weapons/base'
2
+ require 'httpattack/weapons/github'
3
+ require 'httpattack/weapons/twitter'
4
+
5
+ module HTTPAttack
6
+ module Weapons
7
+ class InvalidStat < Exception; end
8
+ class MustOverrideMethod < Exception; end
9
+ end # Weapons
10
+ end # HTTPAttack
@@ -0,0 +1,44 @@
1
+ require 'json'
2
+ require 'open-uri'
3
+
4
+ module HTTPAttack
5
+ module Weapons
6
+
7
+ class Base
8
+ attr_reader :stat, :stats
9
+
10
+ # This is the strength of the weapon and must be
11
+ # overridden in a subclass. It should return
12
+ # an integer (or float).
13
+ def attack
14
+ raise MustOverrideMethod
15
+ end
16
+
17
+ # This is the URL where the JSON will come from.
18
+ # Must be overridden in a subclassed weapon.
19
+ def stats_url
20
+ raise MustOverrideMethod
21
+ end
22
+
23
+ # Parses the JSON response from the +uri+ (which can be a a path
24
+ # or URL) and returns it as a hash.
25
+ #
26
+ # On an error (exception), returns a hash like:
27
+ # { 'error' => error.message }
28
+ def reload_stats(uri = stats_url)
29
+ begin
30
+ JSON.parse(open(uri).read)
31
+ rescue => error
32
+ {'error' => error.message}
33
+ end
34
+ end
35
+
36
+ # Loads <tt>@stats</tt> with the JSON response through <tt>#reload_stats</tt>.
37
+ def reload_stats!(*args)
38
+ @stats = self.reload_stats(*args)
39
+ end
40
+
41
+ end # Base
42
+
43
+ end # Weapons
44
+ end # HTTPAttack
@@ -0,0 +1,26 @@
1
+ module HTTPAttack
2
+ module Weapons
3
+
4
+ class Github < HTTPAttack::Weapons::Base
5
+ STATS = ['followers_count', 'following_count']
6
+
7
+ def initialize(username, options = {})
8
+ options[:stat] ||= 'followers_count'
9
+ raise Weapons::InvalidStat unless STATS.include?(options[:stat])
10
+ @username = username
11
+ @stat = options[:stat]
12
+ end
13
+
14
+ def attack
15
+ self.reload_stats! unless stats
16
+ stats['user'][@stat].to_i
17
+ end
18
+
19
+ def stats_url
20
+ "http://github.com/api/v2/json/user/show/#{@username}"
21
+ end
22
+
23
+ end # Github
24
+
25
+ end # Weapons
26
+ end # HTTPAttack
@@ -0,0 +1,27 @@
1
+ module HTTPAttack
2
+ module Weapons
3
+
4
+ class Twitter < HTTPAttack::Weapons::Base
5
+ STATS = ['followers_count', 'friends_count']
6
+
7
+ def initialize(username, options = {})
8
+ options[:stat] ||= 'followers_count'
9
+ raise Weapons::InvalidStat unless STATS.include?(options[:stat])
10
+ @username = username
11
+ @stat = options[:stat]
12
+ end
13
+
14
+ def attack
15
+ self.reload_stats! unless stats
16
+ stats[@stat].to_i
17
+ end
18
+
19
+ def stats_url
20
+ "http://twitter.com/users/show/#{@username}.json"
21
+ end
22
+
23
+ end # Twitter
24
+
25
+ end # Weapons
26
+ end # HTTPAttack
27
+
@@ -0,0 +1,50 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+
3
+ include HTTPAttack::Weapons
4
+ describe Base do
5
+ before(:each) do
6
+ @remote_api = 'http://localhost:3000/users.json'
7
+ @local_file = File.dirname(__FILE__) + '/responses/base.json'
8
+ end
9
+
10
+ it 'should fetch some JSON from the URL' do
11
+ @weapon = Base.new
12
+ data = @weapon.reload_stats(@remote_api)
13
+ data.should_not be_nil
14
+ data.should be_kind_of(Hash)
15
+ end
16
+
17
+ it 'should fetch some JSON from the file path' do
18
+ @weapon = Base.new
19
+ data = @weapon.reload_stats(@local_file)
20
+ data.should_not be_nil
21
+ data.should be_kind_of(Hash)
22
+ end
23
+
24
+ it 'should return an error hash' do
25
+ @weapon = Base.new
26
+ data = @weapon.reload_stats('invalid path')
27
+ data.should have_key('error')
28
+ data['error'].should_not be_nil
29
+ end
30
+
31
+ it 'should force a subclass to override stats_url' do
32
+ @weapon = Base.new
33
+ lambda do
34
+ @weapon.stats_url
35
+ end.should raise_error(HTTPAttack::Weapons::MustOverrideMethod)
36
+ end
37
+
38
+ it 'should load the stats with #fetch_json!' do
39
+ @weapon = Base.new
40
+ @weapon.reload_stats!(@local_file)
41
+ @weapon.stats.should_not be_nil
42
+ end
43
+
44
+ it 'should force a subclass to override attack' do
45
+ @weapon = Base.new
46
+ lambda do
47
+ @weapon.attack
48
+ end.should raise_error(HTTPAttack::Weapons::MustOverrideMethod)
49
+ end
50
+ end
@@ -0,0 +1,45 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+ include HTTPAttack::Weapons
3
+
4
+ describe Github do
5
+ before(:each) do
6
+ @username = 'Oshuma'
7
+ @stats_file = File.dirname(__FILE__) + '/responses/github-oshuma.json'
8
+ @stats_url = "http://github.com/api/v2/json/user/show/#{@username}"
9
+
10
+ @weapon = Github.new(@username)
11
+ @weapon.reload_stats!(@stats_file)
12
+ end
13
+
14
+ it 'should form the API URL' do
15
+ @weapon.stats_url.should == @stats_url
16
+ end
17
+
18
+ it 'should load the weapon stats' do
19
+ stats = @weapon.reload_stats(@stats_file)
20
+ stats.should_not be_nil
21
+ stats.should be_kind_of(Hash)
22
+ end
23
+
24
+ it 'should default to the followers_count stat' do
25
+ @weapon.stat.should == 'followers_count'
26
+ end
27
+
28
+ it 'should have a (followers_count) attack of 15' do
29
+ @weapon = Github.new(@username, :stat => 'followers_count')
30
+ @weapon.reload_stats!(@stats_file) # test data
31
+ @weapon.attack.should == 15
32
+ end
33
+
34
+ it 'should have a (following_count) attack of 37' do
35
+ @weapon = Github.new(@username, :stat => 'following_count')
36
+ @weapon.reload_stats!(@stats_file) # test data
37
+ @weapon.attack.should == 37
38
+ end
39
+
40
+ it 'should raise InvalidStat' do
41
+ lambda do
42
+ Github.new(@username, :stat => 'invalid')
43
+ end.should raise_error(HTTPAttack::Weapons::InvalidStat)
44
+ end
45
+ end
@@ -0,0 +1 @@
1
+ {"user":{"name":"Dale Campbell","company":"Contract Killer","following_count":37,"public_gist_count":40,"public_repo_count":27,"blog":"http://oshuma.github.com/","id":2441,"followers_count":15,"login":"Oshuma","location":"Madison, WI","email":"oshuma@gmail.com","created_at":"2008/03/06 11:28:35 -0800"}}
@@ -0,0 +1 @@
1
+ {"user":{"name":"Dale Campbell","company":"Contract Killer","following_count":37,"public_gist_count":40,"public_repo_count":27,"blog":"http://oshuma.github.com/","id":2441,"followers_count":15,"login":"Oshuma","location":"Madison, WI","email":"oshuma@gmail.com","created_at":"2008/03/06 11:28:35 -0800"}}
@@ -0,0 +1 @@
1
+ {"profile_sidebar_border_color":"87bc44","followers_count":19,"profile_image_url":"http:\/\/a1.twimg.com\/profile_images\/56247600\/Young_Oshuma_normal.png","description":"This twitter account is used to track repository commits.","verified":false,"utc_offset":-21600,"created_at":"Thu Apr 17 01:56:34 +0000 2008","friends_count":1,"screen_name":"oshuma_dev","profile_text_color":"000000","profile_background_image_url":"http:\/\/a3.twimg.com\/profile_background_images\/2860337\/binary_love__resized_.jpg","favourites_count":0,"url":"http:\/\/corrupt.save-state.net\/","name":"Dale Campbell","profile_link_color":"0000ff","protected":false,"status":{"truncated":false,"created_at":"Wed Jun 10 21:54:18 +0000 2009","in_reply_to_status_id":null,"favorited":false,"in_reply_to_user_id":null,"in_reply_to_screen_name":null,"text":"[pingfm] http:\/\/bit.ly\/CkuJn Dale Campbell - version bump to 1.0.2","id":2108903929,"source":"<a href=\"http:\/\/github.com\" rel=\"nofollow\">GitHub<\/a>"},"profile_background_tile":true,"notifications":null,"profile_background_color":"9ae4e8","following":null,"statuses_count":1118,"profile_sidebar_fill_color":"e0ff92","location":"Madison, WI","id":14415445,"time_zone":"Central Time (US & Canada)"}
@@ -0,0 +1,45 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+ include HTTPAttack::Weapons
3
+
4
+ describe Twitter do
5
+ before(:each) do
6
+ @username = 'oshuma_dev'
7
+ @stats_file = File.dirname(__FILE__) + '/responses/twitter-oshuma_dev.json'
8
+ @stats_url = "http://twitter.com/users/show/#{@username}.json"
9
+
10
+ @weapon = Twitter.new(@username)
11
+ @weapon.reload_stats!(@stats_file)
12
+ end
13
+
14
+ it 'should form the API URL' do
15
+ @weapon.stats_url.should == @stats_url
16
+ end
17
+
18
+ it 'should load the weapon stats' do
19
+ stats = @weapon.reload_stats(@stats_file)
20
+ stats.should_not be_nil
21
+ stats.should be_kind_of(Hash)
22
+ end
23
+
24
+ it 'should default to the followers_count stat' do
25
+ @weapon.stat.should == 'followers_count'
26
+ end
27
+
28
+ it 'should have a (followers_count) attack of 19' do
29
+ @weapon = Twitter.new(@username, :stat => 'followers_count')
30
+ @weapon.reload_stats!(@stats_file) # test data
31
+ @weapon.attack.should == 19
32
+ end
33
+
34
+ it 'should have a (friends_count) attack of 1' do
35
+ @weapon = Twitter.new(@username, :stat => 'friends_count')
36
+ @weapon.reload_stats!(@stats_file) # test data
37
+ @weapon.attack.should == 1
38
+ end
39
+
40
+ it 'should raise InvalidStat' do
41
+ lambda do
42
+ Twitter.new(@username, :stat => 'invalid')
43
+ end.should raise_error(HTTPAttack::Weapons::InvalidStat)
44
+ end
45
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe HTTPAttack do
4
+ it 'should have a version' do
5
+ HTTPAttack::VERSION.should_not be_nil
6
+ end
7
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'httpattack'))
3
+ require 'spec'
4
+
5
+ Spec::Runner.configure do
6
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: httpattack-lib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dale Campbell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-03 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email:
18
+ - oshuma@gmail.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - README
27
+ - Rakefile
28
+ - lib/httpattack/weapons/base.rb
29
+ - lib/httpattack/weapons/github.rb
30
+ - lib/httpattack/weapons/twitter.rb
31
+ - lib/httpattack/weapons.rb
32
+ - lib/httpattack.rb
33
+ - spec/httpattack/weapons/base_spec.rb
34
+ - spec/httpattack/weapons/github_spec.rb
35
+ - spec/httpattack/weapons/responses/base.json
36
+ - spec/httpattack/weapons/responses/github-oshuma.json
37
+ - spec/httpattack/weapons/responses/twitter-oshuma_dev.json
38
+ - spec/httpattack/weapons/twitter_spec.rb
39
+ - spec/httpattack_spec.rb
40
+ - spec/spec.opts
41
+ - spec/spec_helper.rb
42
+ has_rdoc: true
43
+ homepage: http://oshuma.github.com/HTTPAttack
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --charset=UTF-8
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.5
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: The game library for HTTPAttack
70
+ test_files: []
71
+