kag 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ .rake_tasks~
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source :rubygems
2
+
3
+ gem 'httparty', '~>0.8.3'
4
+
5
+ group :test do
6
+ gem 'rake'
7
+ gem 'webmock', '>=1.8.6'
8
+ gem 'vcr', '>=2.1.1'
9
+ end
@@ -0,0 +1,24 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.2.7)
5
+ crack (0.3.1)
6
+ httparty (0.8.3)
7
+ multi_json (~> 1.0)
8
+ multi_xml
9
+ multi_json (1.3.2)
10
+ multi_xml (0.4.4)
11
+ rake (0.9.2.2)
12
+ vcr (2.1.1)
13
+ webmock (1.8.6)
14
+ addressable (>= 2.2.7)
15
+ crack (>= 0.1.7)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ httparty (~> 0.8.3)
22
+ rake
23
+ vcr (>= 2.1.1)
24
+ webmock (>= 1.8.6)
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2012 Kyrylo Silin
2
+
3
+ This software is provided 'as-is', without any express or implied
4
+ warranty. In no event will the authors be held liable for any damages
5
+ arising from the use of this software.
6
+
7
+ Permission is granted to anyone to use this software for any purpose,
8
+ including commercial applications, and to alter it and redistribute it
9
+ freely, subject to the following restrictions:
10
+
11
+ 1. The origin of this software must not be misrepresented; you must not
12
+ claim that you wrote the original software. If you use this software
13
+ in a product, an acknowledgment in the product documentation would be
14
+ appreciated but is not required.
15
+
16
+ 2. Altered source versions must be plainly marked as such, and must not be
17
+ misrepresented as being the original software.
18
+
19
+ 3. This notice may not be removed or altered from any source distribution.
@@ -0,0 +1,47 @@
1
+ KAG
2
+ ===
3
+
4
+ * [https://github.com/kyrylo/ruby-kag/][rkag]
5
+
6
+ Description
7
+ -----------
8
+
9
+ This is [King Arthur's Gold][kag] API wrapper implemented in Ruby language.
10
+
11
+ Features
12
+ --------
13
+
14
+ * Ability to retrieve information about player (nick, role and so on)
15
+ * Ability to retrieve player's avatar on [KAG forums][kagf]
16
+
17
+ Installation
18
+ ------------
19
+
20
+ gem install ruby-kag
21
+
22
+ Synopsis
23
+ --------
24
+
25
+ ``` ruby
26
+ require 'kag'
27
+
28
+ player = KAG::Player.new('prostosuper')
29
+
30
+ # Get small avatar of player prostosuper
31
+ player.avatar.small
32
+
33
+ # Force request to API to refresh player's small avatar
34
+ player.avatar.small(true)
35
+
36
+ # Get all information about player
37
+ player.info
38
+ ```
39
+
40
+ License
41
+ -------
42
+
43
+ The project uses Zlib License. See LICENSE file for more information.
44
+
45
+ [rkag]: https://github.com/kyrylo/ruby-kag/ "Home page"
46
+ [kag]: http://kag2d.com/
47
+ [kagf]: https://forum.kag2d.com/
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.test_files = FileList['spec/lib/kag/*_spec.rb']
5
+ t.verbose = true
6
+ end
7
+
8
+ task :default => :test
@@ -0,0 +1,31 @@
1
+ require './lib/kag/version'
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'kag'
5
+ s.version = KAG::VERSION
6
+ s.date = Time.now.strftime('%Y-%m-%d')
7
+ s.summary = "King Arthur's Gold (KAG) game API wrapper written in Ruby."
8
+ s.author = 'Kyrylo Silin'
9
+ s.email = 'kyrylosilin@gmail.com'
10
+ s.homepage = 'https://github.com/kyrylo/ruby-kag'
11
+ s.license = 'zlib'
12
+
13
+ s.require_path = 'lib'
14
+ s.files = `git ls-files`.split "\n"
15
+ s.test_files = `git ls-files -- spec`.split "\n"
16
+
17
+ s.extra_rdoc_files = %W{README.md LICENSE}
18
+ s.rdoc_options = ["--charset=UTF-8"]
19
+
20
+ s.add_runtime_dependency 'httparty', '~>0.8.3'
21
+ s.add_development_dependency 'rake'
22
+ s.add_development_dependency 'webmock', '>=1.8.6'
23
+ s.add_development_dependency 'vcr', '>=2.1.1'
24
+
25
+ s.required_ruby_version = '~>1.9'
26
+
27
+ s.description = <<description
28
+ Ruby implementation of King Arthur's Gold (aka KAG) Application Programming
29
+ Interface (aka API).
30
+ description
31
+ end
@@ -0,0 +1,4 @@
1
+ require_relative 'kag/kag'
2
+ require_relative 'kag/version'
3
+ require_relative 'kag/player'
4
+ require_relative 'kag/avatar'
@@ -0,0 +1,115 @@
1
+ module KAG
2
+ # https://wiki.kag2d.com/wiki/Avatar_URL
3
+ class Player::Avatar
4
+ # Public: Initialize a new avatar.
5
+ #
6
+ # nick - The case-insensitive name of the avatar owner.
7
+ def initialize(nick)
8
+ @nick = nick
9
+ end
10
+
11
+ # Public: Retrieve avatar information about KAG player.
12
+ #
13
+ # force - The flag which enables/disables caching of the avatars. Being
14
+ # equal to true, disables caching and resend GET request to KAG API
15
+ # endpoint (default: false).
16
+ #
17
+ # Returns Hash with avatar sizes of the player or Hash with statusMessage,
18
+ # if the player doesn't exist.
19
+ def sizes(force = false)
20
+ if force
21
+ @sizes = get_avatar
22
+ else
23
+ @sizes ||= get_avatar
24
+ end
25
+ end
26
+
27
+ # Internal: Intercepts all method calls on instances of this class, that
28
+ # aren't defined here. If the called method coincides with one of the valid
29
+ # methods, then it gets the value of that Hash pair from @sizes Hash. If
30
+ # the parameter true was provided, then the method performs a request
31
+ # directly to API rather than to cache.
32
+ #
33
+ # force - If equals to true, then request an avatar from API. If false, then
34
+ # use avatar from cache (default: false).
35
+ #
36
+ # Examples
37
+ #
38
+ # avatar.foo_bar_baz
39
+ # # => NoMethodError
40
+ # avatar.small
41
+ # # => "https://forum.kag2d.com/data/avatars/s/0/231.jpg"
42
+ #
43
+ # # From cache.
44
+ # avatar.small
45
+ # # => "https://forum.kag2d.com/data/avatars/s/0/231.jpg"
46
+ #
47
+ # # Refresh cache.
48
+ # avatar.small(true)
49
+ # # => "https://forum.kag2d.com/data/avatars/s/0/231.jpg"
50
+ #
51
+ # # Attempt to get an avatar of nonexistent player.
52
+ # nonexistent_avatar = KAG::Player::Avatar.new('foobarbazbaz')
53
+ # nonexistent_avatar.small
54
+ # # => {"statusMessage"=>"Player not found"}
55
+ #
56
+ # Returns the value of the method call on self.
57
+ # Raises NoMethodError, if the given method doesn't exist.
58
+ def method_missing(m, *args, &block)
59
+ valid_method = [:small, :medium, :large].find { |size| size === m }
60
+
61
+ if valid_method
62
+ if args[0] === true
63
+ send("get_#{m}_avatar")
64
+ else
65
+ sizes.fetch(m.to_s) { sizes }
66
+ end
67
+ else
68
+ super
69
+ end
70
+ end
71
+
72
+ protected
73
+
74
+ # Internal: Send GET request to the KAG API endpoint in order to get
75
+ # player's avatars (small, medium, large).
76
+ #
77
+ # Returns Hash with the information about avatars or Hash with
78
+ # statusMessage, telling, that player doesn't exist, if there is no KAG
79
+ # player with given nick.
80
+ def get_avatar
81
+ KAG.get "/player/#@nick/avatar"
82
+ end
83
+
84
+ # Internal: Send GET request to the KAG API endpoint in order to get
85
+ # player's small avatar.
86
+ #
87
+ # Returns Hash with the information about avatars or Hash with
88
+ # statusMessage, telling, that player doesn't exist, if there is no KAG
89
+ # player with given nick.
90
+ def get_small_avatar
91
+ KAG.get "/player/#@nick/avatar/s"
92
+ end
93
+
94
+ # Internal: Send GET request to the KAG API endpoint in order to get
95
+ # player's medium avatar.
96
+ #
97
+ # Returns Hash with the information about avatars or Hash with
98
+ # statusMessage, telling, that player doesn't exist, if there is no KAG
99
+ # player with given nick.
100
+ def get_medium_avatar
101
+ KAG.get "/player/#@nick/avatar/m"
102
+ end
103
+
104
+ # Internal: Send GET request to the KAG API endpoint in order to get
105
+ # player's large avatar.
106
+ #
107
+ # Returns Hash with the information about avatars or Hash with
108
+ # statusMessage, telling, that player doesn't exist, if there is no KAG
109
+ # player with given nick.
110
+ def get_large_avatar
111
+ KAG.get "/player/#@nick/avatar/l"
112
+ end
113
+
114
+ end
115
+ end
@@ -0,0 +1,10 @@
1
+ require 'httparty'
2
+
3
+ module KAG
4
+ include HTTParty
5
+
6
+ # Internal: KAG API URI (JAFAs all the way!)
7
+ API = 'http://api.kag2d.com'
8
+
9
+ base_uri API
10
+ end
@@ -0,0 +1,174 @@
1
+ module KAG
2
+ # https://wiki.kag2d.com/wiki/Player_Info
3
+ class Player
4
+ # Public: Returns the String name of the player.
5
+ attr_reader :nick
6
+
7
+ # Public: Returns the KAG::Player::Avatar object.
8
+ attr_reader :avatar
9
+
10
+ # Public: Initialize a new player.
11
+ #
12
+ # nick - The case-insensitive name of the player in King Arthur's Gold game.
13
+ def initialize(nick)
14
+ @nick = nick
15
+ @avatar = Avatar.new(nick)
16
+ end
17
+
18
+ # Public: Retrieve information about KAG player.
19
+ #
20
+ # force - The flag which enables/disables caching of the info. Being equal
21
+ # to true, disables caching and resend GET request to KAG API
22
+ # endpoint (default: false).
23
+ #
24
+ # Examples
25
+ #
26
+ # player.info
27
+ # player.info(true) # Disable cached info
28
+ #
29
+ # Returns Hash with info about player or Hash with statusMessage, if the
30
+ # player doesn't exist..
31
+ def info(force = false)
32
+ if force
33
+ @info = get_info
34
+ else
35
+ @info ||= get_info
36
+ end
37
+ end
38
+
39
+ # Public: Get the status of the player.
40
+ #
41
+ # Examples
42
+ #
43
+ # player.active?
44
+ # # => false
45
+ #
46
+ # Returns true or false.
47
+ def active?
48
+ info['active']
49
+ end
50
+
51
+ # Public: Get the username of the player.
52
+ #
53
+ # Examples
54
+ #
55
+ # player.username
56
+ # # => 'prostosuper'
57
+ #
58
+ # # Please, note, that KAG::Player#username is not alias for
59
+ # # KAG::Player#nick. KAG::Player#username is the name, which is given by
60
+ # # KAG API and Player#nick is an attribute of your object. Feel the
61
+ # # difference:
62
+ # # player = KAG::Player.new('flieslikeabrick')
63
+ # # player.username
64
+ # # => 'FliesLikeABrick'
65
+ # # player.nick
66
+ # # => 'flieslikeabrick'
67
+ #
68
+ # Returns String with containing name of the user in game.
69
+ def username
70
+ info['username']
71
+ end
72
+
73
+ # Public: Get the ban status of the player.
74
+ #
75
+ # Examples
76
+ #
77
+ # player.banned?
78
+ # # => true
79
+ #
80
+ # Returns true or false.
81
+ def banned?
82
+ info['banned']
83
+ end
84
+
85
+ # Public: Get the role of the player.
86
+ #
87
+ # readable - The Boolean flag, which describes how the return value should
88
+ # be represented (either machine-readable format or
89
+ # human-readable) (default: false).
90
+ #
91
+ # Examples
92
+ #
93
+ # # Machine-readable role.
94
+ # player.role
95
+ # # => 4
96
+ #
97
+ # # Human-readable role.
98
+ # player.role(true)
99
+ # # => 'team member'
100
+ #
101
+ # Returns Integer, representing role of the player or String, if readable
102
+ # flag was used.
103
+ def role(readable = false)
104
+ role = info['role']
105
+
106
+ if readable
107
+ case role
108
+ when 0 then 'normal'
109
+ when 1 then 'developer'
110
+ when 2 then 'guard'
111
+ when 4 then 'team member'
112
+ when 5 then 'tester'
113
+ end
114
+ else
115
+ role
116
+ end
117
+ end
118
+
119
+ # Public: Check for account type of the player. Owners of gold account
120
+ # bought the game.
121
+ #
122
+ # Examples
123
+ #
124
+ # player.gold?
125
+ # # => false
126
+ #
127
+ # Returns true if the player has gold account or false otherwise.
128
+ def gold?
129
+ info['gold']
130
+ end
131
+
132
+ # Public: Get the ban expiration date if user was banned. This field is
133
+ # appears only for banned users.
134
+ #
135
+ # Examples
136
+ #
137
+ # player.ban_expiration
138
+ # # => #<DateTime: 2022-03-02T09:09:53+00:00 ((2459641j,32993s,0n),+0s,2299161j)>
139
+ #
140
+ # Returns DateTime object, which has ban expiration date of the player or
141
+ # nil, if the player has no active bans.
142
+ def ban_expiration
143
+ ban_expiration_date = info['banExpiration']
144
+ DateTime.parse ban_expiration_date if ban_expiration_date
145
+ end
146
+
147
+ # Public: Get the ban reason if user was banned. This field is appears only
148
+ # for banned users.
149
+ #
150
+ # Examples
151
+ #
152
+ # player.ban_reason
153
+ # # => 'Speedhacking'
154
+ #
155
+ # Returns String object, which represents description of the ban if any bans
156
+ # are active.
157
+ def ban_reason
158
+ info['banReason']
159
+ end
160
+
161
+ protected
162
+
163
+ # Internal: Send GET request to the KAG API endpoint in order to get the
164
+ # information about KAG player.
165
+ #
166
+ # Returns Hash with the information about player or Hash with statusMessage,
167
+ # telling, that player doesn't exist, if there is no KAG player with given
168
+ # nick.
169
+ def get_info
170
+ KAG.get "/player/#@nick/info"
171
+ end
172
+
173
+ end
174
+ end