Class: CodeforcesAPI::Client
- Inherits:
-
Object
- Object
- CodeforcesAPI::Client
show all
- Includes:
- Configuration
- Defined in:
- lib/codeforces_api/client.rb,
lib/codeforces_api/client/helper.rb
Constant Summary
collapse
- API_URI =
'http://codeforces.com/api'
CodeforcesAPI::Configuration::ATTRS
Instance Method Summary
collapse
#configure, #options, #reset!, #using_api?
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
27
28
29
|
# File 'lib/codeforces_api/client.rb', line 27
def initialize options = {}
options.each{ |k, v| instance_variable_set(:"@#{k}", v) }
end
|
Instance Method Details
#get(method, required_params, optional_params = {}) ⇒ Object
26
27
28
29
30
31
|
# File 'lib/codeforces_api/client/helper.rb', line 26
def get method, required_params, optional_params = {}
params = required_params.merge(optional_params)
uri = URI(request_url(method, params))
http_response = Net::HTTP.get(uri)
json_response = JSON.parse(http_response)
end
|
#post_form(options = {}) ⇒ Object
9
10
11
|
# File 'lib/codeforces_api/client/helper.rb', line 9
def post_form options = {}
options.collect{ |k, v| "#{k}=#{v}" }.join('&')
end
|
5
6
7
|
# File 'lib/codeforces_api/client/helper.rb', line 5
def rand6
[*0..5].collect{ rand(10) }.join
end
|
#request_url(method, _params) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/codeforces_api/client/helper.rb', line 13
def request_url method, _params
url = "#{API_URI}/#{method}?#{post_form(_params)}"
return url unless using_api?
salt = rand6()
time = Time.now.to_i
info = {'apiKey' => @key, 'time' => time}
params = _params.merge(info).map{|k, v| [k.to_s, v]}.sort.to_h
digest_string = "#{salt}/#{method}?#{post_form(params)}##{@secret}"
api_sig = Digest::SHA2.new(512).hexdigest(digest_string)
params.merge!('apiSig'=>"#{salt}#{api_sig}")
"#{API_URI}/#{method}?#{post_form(params)}"
end
|