Class: HonStats::API
- Inherits:
-
Object
- Object
- HonStats::API
- Defined in:
- lib/honstats.rb
Constant Summary collapse
- VERSION =
'0.0.1'
- @@stats_url_base =
"masterserver.hon.s2games.com/"
- @@requester_file =
"client_requester.php"
- @@search_types =
{ :character => 'character' }
Instance Attribute Summary collapse
-
#character_name ⇒ Object
Returns the value of attribute character_name.
-
#cookie ⇒ Object
Returns the value of attribute cookie.
Class Method Summary collapse
-
.get_data(attribute, data) ⇒ Object
Used to fetch the data from the messy JSON type string sent back from the server, might be a better/faster/cleaner way of doing this.
Instance Method Summary collapse
- #base_url(options = {}) ⇒ Object
- #get_active_games(options = { :gametype => 32 }) ⇒ Object
- #get_banlist(options = {}) ⇒ Object
- #get_character(name = @character_name, options = {}) ⇒ Object
- #get_clan(options = {}) ⇒ Object
- #get_games(options = { :gametype => 10 }) ⇒ Object
- #get_ignorelist(options = {}) ⇒ Object
- #get_servers(options = { :gametype => 90 }) ⇒ Object
-
#initialize(options = {}) ⇒ API
constructor
Accepts an optional hash of parameters to create defaults for all API requests * options (Hash) - Hash used to set default values for all API requests.
- #login(username, password) ⇒ Object
-
#search(string, options = {}) ⇒ Object
As more API’s are created, we might need a place to search for items/guilds/players etc but for now, it’s just for players.
- #search_characters(name, options = {}) ⇒ Object
- #server_browser(options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ API
Accepts an optional hash of parameters to create defaults for all API requests
-
options (Hash) - Hash used to set default values for all API requests
42 43 44 45 |
# File 'lib/honstats.rb', line 42 def initialize( = {}) @character_name = [:character_name] @debug = [:debug] || false end |
Instance Attribute Details
#character_name ⇒ Object
Returns the value of attribute character_name.
37 38 39 |
# File 'lib/honstats.rb', line 37 def character_name @character_name end |
#cookie ⇒ Object
Returns the value of attribute cookie.
37 38 39 |
# File 'lib/honstats.rb', line 37 def @cookie end |
Class Method Details
.get_data(attribute, data) ⇒ Object
Used to fetch the data from the messy JSON type string sent back from the server, might be a better/faster/cleaner way of doing this
219 220 221 222 223 224 225 226 |
# File 'lib/honstats.rb', line 219 def self.get_data(attribute, data) data = data.split("\"#{attribute}\"") if data[1] data = data[1].split(";") data = data[1].split("\"") data[1] end end |
Instance Method Details
#base_url(options = {}) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/honstats.rb', line 203 def base_url( = {}) str = "" if ([:secure] == true) str += 'https://' else str += 'http://' end str += @@stats_url_base return str end |
#get_active_games(options = { :gametype => 32 }) ⇒ Object
142 143 144 145 146 |
# File 'lib/honstats.rb', line 142 def get_active_games( = { :gametype => 32 }) = merge_defaults() [:type] = HonStats::Classes::Game return server_browser() end |
#get_banlist(options = {}) ⇒ Object
176 177 178 179 180 181 182 183 184 |
# File 'lib/honstats.rb', line 176 def get_banlist( = {}) = merge_defaults() url = self.base_url + @@requester_file data = Net::HTTP.post_form(URI.parse(url), {"f"=>"banned_list", "account_id[0]"=>"#{[:account_id]}"}) data = data.body.split(';a:3:{s:10:"account_id";s:') data.delete_at(0) data.map! {|d| 's:10:"account_id";s:' + d } return HonStats::Classes::Banlist::Banlist.new(data) end |
#get_character(name = @character_name, options = {}) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/honstats.rb', line 106 def get_character(name = @character_name, = {}) if (name.is_a?(Hash)) = name else .merge!(:character_name => name) = {:character_name => @character_name}.merge() if (!@character_name.nil?) end = merge_defaults() if [:character_name].nil? || [:character_name] == "" raise "characer name not specified" end if [:character_name].is_a?(String) url = self.base_url + @@requester_file character_account_id = Net::HTTP.post_form(URI.parse(url), {"f"=>"nick2id", "nickname[0]"=>"#{[:character_name]}"}) character_account_id = character_account_id.body elsif [:character_name].is_a?(Integer) character_account_id = [:character_name] end return HonStats::Classes::Character.new(character_account_id, self) end |
#get_clan(options = {}) ⇒ Object
166 167 168 169 170 171 172 173 174 |
# File 'lib/honstats.rb', line 166 def get_clan( = {}) = merge_defaults() url = self.base_url + @@requester_file data = Net::HTTP.post_form(URI.parse(url), {"f"=>"clan_list", "clan_id"=>"#{[:clan_id]}"}) data = data.body.split(';a:6:{s:10:"account_id";s:') data.delete_at(0) data.map! {|d| 's:10:"account_id";s:' + d } return HonStats::Classes::Clan.new(data) end |
#get_games(options = { :gametype => 10 }) ⇒ Object
136 137 138 139 140 |
# File 'lib/honstats.rb', line 136 def get_games( = { :gametype => 10 }) = merge_defaults() [:type] = HonStats::Classes::Game return server_browser() end |
#get_ignorelist(options = {}) ⇒ Object
186 187 188 189 190 191 192 193 194 |
# File 'lib/honstats.rb', line 186 def get_ignorelist( = {}) = merge_defaults() url = self.base_url + @@requester_file data = Net::HTTP.post_form(URI.parse(url), {"f"=>"ignored_list", "account_id[0]"=>"#{[:account_id]}"}) data = data.body.split(';a:3:{s:10:"account_id";s:') data.delete_at(0) data.map! {|d| 's:10:"account_id";s:' + d } return HonStats::Classes::Ignorelist::Ignorelist.new(data) end |
#get_servers(options = { :gametype => 90 }) ⇒ Object
130 131 132 133 134 |
# File 'lib/honstats.rb', line 130 def get_servers( = { :gametype => 90 }) = merge_defaults() [:type] = HonStats::Classes::Server return server_browser() end |
#login(username, password) ⇒ Object
196 197 198 199 200 201 |
# File 'lib/honstats.rb', line 196 def login(username, password) url = self.base_url + @@requester_file data = Net::HTTP.post_form(URI.parse(url), {"f"=>"auth", "login"=>"#{username}", "password"=>"#{password}"}) @cookie = HonStats::API.get_data("cookie", data.body).to_s end |
#search(string, options = {}) ⇒ Object
As more API’s are created, we might need a place to search for items/guilds/players etc but for now, it’s just for players
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/honstats.rb', line 49 def search(string, = {}) if (string.is_a?(Hash)) = string else .merge!(:search => string) end = merge_defaults() if [:search].nil? || [:search].empty? raise "no search string" end if !@@search_types.has_value?([:type]) raise "invalid search type" end results = [] case [:type] when @@search_types[:character] url = self.base_url + @@requester_file character_names = Net::HTTP.post_form(URI.parse(url), {"f"=>"autocompleteNicks", "nickname"=>"#{[:search]}"}) character_names = character_names.body.split('"') character_names.each_index do |d| if character_names[d].include?(";") character_names.delete_at(d) end end character_names.delete_at(0) character_names.each do |character| sheet = self.get_character(character) if sheet.account.id > 0 results << self.get_character(character) end end end return results end |
#search_characters(name, options = {}) ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/honstats.rb', line 94 def search_characters(name, = {}) if (name.is_a?(Hash)) = name else .merge!(:search => name) end .merge!(:type => @@search_types[:character]) return search() end |
#server_browser(options = {}) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/honstats.rb', line 148 def server_browser( = {}) = merge_defaults() if @cookie url = self.base_url + @@requester_file data = Net::HTTP.post_form(URI.parse(url), {"f"=>"server_list", "gametype"=>"#{[:gametype]}", "cookie"=>@cookie}) data = data.body.split('s:9:"server_id";s:') data.delete_at(0) data.map! {|d| 's:9:"server_id";s:' + d } output = [] data.each do |d| output << [:type].new(d) end output else raise "unable to access server browser data without an auth cookie, use login() first" end end |