Class: HonStats::API

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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(options = {})
  @character_name = options[:character_name]
  @debug = options[:debug] || false
end

Instance Attribute Details

#character_nameObject

Returns the value of attribute character_name.



37
38
39
# File 'lib/honstats.rb', line 37

def character_name
  @character_name
end

Returns the value of attribute cookie.



37
38
39
# File 'lib/honstats.rb', line 37

def cookie
  @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(options = {})
	str = ""

	if (options[: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(options = { :gametype => 32 })
  options = merge_defaults(options)
  options[:type] = HonStats::Classes::Game
  return server_browser(options)
end

#get_banlist(options = {}) ⇒ Object



176
177
178
179
180
181
182
183
184
# File 'lib/honstats.rb', line 176

def get_banlist(options = {})
  options = merge_defaults(options)
  url = self.base_url + @@requester_file
  data = Net::HTTP.post_form(URI.parse(url), {"f"=>"banned_list", "account_id[0]"=>"#{options[: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, options = {})
  if (name.is_a?(Hash))
options = name
			else
options.merge!(:character_name => name)
options = {:character_name => @character_name}.merge(options) if (!@character_name.nil?)
			end

  options = merge_defaults(options)

  if options[:character_name].nil? || options[:character_name] == ""
raise "characer name not specified"
  end

  if options[:character_name].is_a?(String)
    url = self.base_url + @@requester_file
     = Net::HTTP.post_form(URI.parse(url), {"f"=>"nick2id", "nickname[0]"=>"#{options[:character_name]}"})
     = .body
  elsif options[:character_name].is_a?(Integer)
     = options[:character_name]
  end
  return HonStats::Classes::Character.new(, self)
end

#get_clan(options = {}) ⇒ Object



166
167
168
169
170
171
172
173
174
# File 'lib/honstats.rb', line 166

def get_clan(options = {})
  options = merge_defaults(options)
  url = self.base_url + @@requester_file
  data = Net::HTTP.post_form(URI.parse(url), {"f"=>"clan_list", "clan_id"=>"#{options[: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(options = { :gametype => 10 })
  options = merge_defaults(options)
  options[:type] = HonStats::Classes::Game
  return server_browser(options)
end

#get_ignorelist(options = {}) ⇒ Object



186
187
188
189
190
191
192
193
194
# File 'lib/honstats.rb', line 186

def get_ignorelist(options = {})
  options = merge_defaults(options)
  url = self.base_url + @@requester_file
  data = Net::HTTP.post_form(URI.parse(url), {"f"=>"ignored_list", "account_id[0]"=>"#{options[: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(options = { :gametype => 90 })
  options = merge_defaults(options)
  options[:type] = HonStats::Classes::Server
  return server_browser(options)
end

#login(username, password) ⇒ Object



196
197
198
199
200
201
# File 'lib/honstats.rb', line 196

def (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, options = {})
  
			if (string.is_a?(Hash))
options = string
			else
options.merge!(:search => string)
			end

  options = merge_defaults(options)

			if options[:search].nil? || options[:search].empty?
raise "no search string"
			end

			if !@@search_types.has_value?(options[:type])
raise "invalid search type"
			end

  results = []

  case options[:type]
    when @@search_types[:character]
      url = self.base_url + @@requester_file
      character_names = Net::HTTP.post_form(URI.parse(url), {"f"=>"autocompleteNicks", "nickname"=>"#{options[: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..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, options = {})
	if (name.is_a?(Hash))
		options = name
	else
		options.merge!(:search => name)
	end

	options.merge!(:type => @@search_types[:character])
	return search(options)
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(options = {})
  options = merge_defaults(options)
  if @cookie
    url = self.base_url + @@requester_file
    data = Net::HTTP.post_form(URI.parse(url), {"f"=>"server_list", "gametype"=>"#{options[: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 << options[:type].new(d)
    end
    output
  else
    raise "unable to access server browser data without an auth cookie, use login() first"
  end
end