kla-honsolo 0.0.1

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.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2009
2
+
3
+ AUTHORS:
4
+ Kien La
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
@@ -0,0 +1,11 @@
1
+ # Honsolo #
2
+
3
+ Ruby library for querying game information from Heroes of Newerth (http://www.heroesofnewerth.com/).
4
+
5
+ ## Example Usage ##
6
+
7
+ character = Honsolo::Character.find("teeto")
8
+ puts character.inspect
9
+
10
+ guild = Honsolo::Clan.find(character.clan_info.clan_id)
11
+ puts guild.inspect
@@ -0,0 +1,9 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:default) do |t|
5
+ t.libs << "test"
6
+ t.pattern = "test/**/*_test.rb"
7
+ t.verbose = true
8
+ end
9
+ Rake::Task['test'].comment = "Run the tests"
@@ -0,0 +1,40 @@
1
+ require 'net/http'
2
+ require 'ostruct'
3
+ require 'php_serialize'
4
+ require File.dirname(__FILE__) + '/honsolo/character'
5
+ require File.dirname(__FILE__) + '/honsolo/clan'
6
+
7
+ module Honsolo
8
+ MasterServer = URI.parse('http://masterserver.hon.s2games.com/client_requester.php')
9
+
10
+ class CharacterNotFound < StandardError; end
11
+ class ClanNotFound < StandardError; end
12
+
13
+ class << self
14
+ def post(params)
15
+ response = Net::HTTP.post_form(MasterServer,params)
16
+ response.is_a?(Net::HTTPSuccess) ? PHP.unserialize(response.body) : nil
17
+ end
18
+
19
+ def to_ostruct(hash, character_id=nil, parent=nil)
20
+ return hash unless hash.is_a?(Hash)
21
+
22
+ obj = parent ? parent : OpenStruct.new
23
+ hash.each do |k,v|
24
+ next if k == 0
25
+
26
+ if k == character_id && v.is_a?(Hash)
27
+ to_ostruct(v,character_id,obj)
28
+ else
29
+ if v.is_a?(Hash)
30
+ v = to_ostruct(v,character_id)
31
+ elsif v.is_a?(String)
32
+ v = v.to_i if v =~ /-?[0-9]+/
33
+ end
34
+ obj.send("#{k}=",v)
35
+ end
36
+ end
37
+ obj
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,23 @@
1
+ module Honsolo
2
+ module Character
3
+ class << self
4
+ def find_id(name)
5
+ name = name.downcase
6
+ data = Honsolo.post("f" => "nick2id", "nickname[]" => name)
7
+ raise Honsolo::CharacterNotFound, name unless data.is_a?(Hash) && data.has_key?(name)
8
+ data[name].to_i
9
+ end
10
+
11
+ def find(name_or_id)
12
+ id = name_or_id.is_a?(Fixnum) ? name_or_id : find_id(name_or_id)
13
+ data = Honsolo.post("f" => "get_all_stats", "account_id[]" => id)
14
+ raise Honsolo::CharacterNotFound, name_or_id if data["all_stats"].is_a?(Array) && data["all_stats"].empty?
15
+
16
+ character = Honsolo.to_ostruct(data["all_stats"],id)
17
+ character.last_match = Honsolo.to_ostruct(data["last_match"])
18
+ character.clan_info = Honsolo.to_ostruct(data["clan_info"],id)
19
+ character
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,13 @@
1
+ module Honsolo
2
+ module Clan
3
+ class << self
4
+ def find(clan_id)
5
+ data = Honsolo.post("f" => "clan_list", "clan_id" => clan_id)
6
+ raise ClanNotFound, clan_id if data["clan_roster"]["error"]
7
+
8
+ OpenStruct.new(:clan_roster => data["clan_roster"].collect { |id, member| Honsolo.to_ostruct(member) })
9
+ end
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,36 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class CharacterTest < ActiveSupport::TestCase
4
+ context "#find_id" do
5
+ setup { @id = Honsolo::Character.find_id("teeto") }
6
+
7
+ should "return an id" do
8
+ assert_kind_of Fixnum, @id
9
+ assert @id > 0
10
+ end
11
+ end
12
+
13
+ context "#find" do
14
+ setup { @character = Honsolo::Character.find("TEETO") }
15
+
16
+ should "not have all_stats" do
17
+ assert !@character.all_stats
18
+ end
19
+
20
+ should "have a nickname" do
21
+ assert_equal "teeto", @character.nickname
22
+ end
23
+
24
+ should "have last_match" do
25
+ assert_kind_of Array, @character.last_match
26
+ end
27
+
28
+ should "have clan_info" do
29
+ assert @character.clan_info
30
+ end
31
+
32
+ should "raise exception if not found" do
33
+ assert_raise(Honsolo::CharacterNotFound) { Honsolo::Character.find(-1) }
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class ClanTest < ActiveSupport::TestCase
4
+ context "#find" do
5
+ setup { @clan = Honsolo::Clan.find(1) }
6
+
7
+ should "have a roster" do
8
+ assert !@clan.clan_roster.empty?
9
+ end
10
+
11
+ should "raise exception if not found" do
12
+ assert_raise(Honsolo::ClanNotFound) { Honsolo::Clan.find(-1) }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,23 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class HonsoloTest < ActiveSupport::TestCase
4
+ context "#post" do
5
+ should "return nil if request failed" do
6
+ Net::HTTP.stubs(:post_form => Net::HTTPNotFound.new(nil,nil,nil))
7
+ assert !Honsolo.post(nil)
8
+ end
9
+ end
10
+
11
+ context "#to_ostruct" do
12
+ setup { @ostruct = Honsolo.to_ostruct(:a => "one", :b => "two", :nested => {:c => "three"}) }
13
+
14
+ should "work" do
15
+ assert_equal "one", @ostruct.a
16
+ assert_equal "two", @ostruct.b
17
+ end
18
+
19
+ should "work with a nested hash" do
20
+ assert_equal "three", @ostruct.nested.c
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ require 'active_support'
2
+ require 'active_support/test_case'
3
+ require 'shoulda'
4
+ require File.dirname(__FILE__) + '/../lib/honsolo'
5
+
6
+ class ActiveSupport::TestCase
7
+ end
8
+
9
+ class Net::HTTP
10
+ class << self
11
+ alias_method :post_form_without_cache, :post_form
12
+
13
+ def post_form(url, params)
14
+ @cache ||= {}
15
+ key = {:url => url, :params => params}
16
+ return @cache[key] if @cache.has_key?(key)
17
+
18
+ @cache[key] = post_form_without_cache(url,params)
19
+ end
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kla-honsolo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kien La
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: uhees-php-serialize
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.1.0
24
+ version:
25
+ description:
26
+ email: la.kien@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - test
35
+ - test/character_test.rb
36
+ - test/honsolo_test.rb
37
+ - test/test_helper.rb
38
+ - test/clan_test.rb
39
+ - Rakefile
40
+ - lib
41
+ - lib/honsolo
42
+ - lib/honsolo/clan.rb
43
+ - lib/honsolo/character.rb
44
+ - lib/honsolo.rb
45
+ - README.md
46
+ - LICENSE
47
+ has_rdoc: false
48
+ homepage: http://github.com/kla/honsolo/tree/master
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.2.0
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: Ruby library for querying game information from Heroes of Newerth
73
+ test_files: []
74
+