Class: Codeforces::Client
- Inherits:
-
Object
- Object
- Codeforces::Client
show all
- Includes:
- Helper
- Defined in:
- lib/codeforces/client.rb
Constant Summary
collapse
- DEFAULT_ENDPOINT =
"http://codeforces.com/api/"
- DEFAULT_PAGE_COUNT =
50
- DEFAULT_API_KEY =
ENV["CODEFORCES_API_KEY"]
- DEFAULT_API_SECRET =
ENV["CODEFORCES_API_SECRET"]
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Helper
#contest, #contests, #each_contest, #each_status, #problems, #recent_status, #user, #users
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
25
26
27
28
29
|
# File 'lib/codeforces/client.rb', line 25
def initialize(options = {})
@endpoint = options.fetch(:endpoint, DEFAULT_ENDPOINT)
@api_key = options.fetch(:api_key, DEFAULT_API_KEY)
@api_secret = options.fetch(:api_secret, DEFAULT_API_SECRET)
end
|
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
22
23
24
|
# File 'lib/codeforces/client.rb', line 22
def api_key
@api_key
end
|
#api_secret ⇒ Object
Returns the value of attribute api_secret.
23
24
25
|
# File 'lib/codeforces/client.rb', line 23
def api_secret
@api_secret
end
|
#endpoint ⇒ Object
Returns the value of attribute endpoint.
21
22
23
|
# File 'lib/codeforces/client.rb', line 21
def endpoint
@endpoint
end
|
Instance Method Details
#agent ⇒ Object
35
36
37
|
# File 'lib/codeforces/client.rb', line 35
def agent
@agent ||= ::Sawyer::Agent.new(DEFAULT_ENDPOINT)
end
|
#api ⇒ Object
87
88
89
|
# File 'lib/codeforces/client.rb', line 87
def api
@api ||= ::Codeforces::Api.new(self)
end
|
#create_contest(contest) ⇒ Object
103
104
105
|
# File 'lib/codeforces/client.rb', line 103
def create_contest(contest)
::Codeforces::Models::Contest.new self, contest
end
|
#create_contests(contests) ⇒ Object
107
108
109
|
# File 'lib/codeforces/client.rb', line 107
def create_contests(contests)
::Codeforces::Models::Contests.new self, contests
end
|
#create_problem(problem) ⇒ Object
111
112
113
|
# File 'lib/codeforces/client.rb', line 111
def create_problem(problem)
::Codeforces::Models::Problem.new self, problem
end
|
#create_problems(problems) ⇒ Object
115
116
117
|
# File 'lib/codeforces/client.rb', line 115
def create_problems(problems)
::Codeforces::Models::Problems.new self, problems
end
|
#create_submission(status) ⇒ Object
#create_user(user) ⇒ Object
91
92
93
|
# File 'lib/codeforces/client.rb', line 91
def create_user(user)
::Codeforces::Models::User.new self, user
end
|
#create_users(users) ⇒ Object
99
100
101
|
# File 'lib/codeforces/client.rb', line 99
def create_users(users)
::Codeforces::Models::Users.new self, users
end
|
#get(path, options = {}) ⇒ Object
43
44
45
|
# File 'lib/codeforces/client.rb', line 43
def get(path, options = {})
request(:get, path, options).result
end
|
#last_response ⇒ Object
39
40
41
|
# File 'lib/codeforces/client.rb', line 39
def last_response
@last_response
end
|
#logger ⇒ Object
31
32
33
|
# File 'lib/codeforces/client.rb', line 31
def logger
@logger ||= new_logger
end
|
#multi_values(values) ⇒ Object
83
84
85
|
# File 'lib/codeforces/client.rb', line 83
def multi_values(values)
values.join ";"
end
|
#paginate(offset) ⇒ Object
75
76
77
78
79
80
81
|
# File 'lib/codeforces/client.rb', line 75
def paginate(offset)
offset = 0 if offset.nil?
result = {
:from => DEFAULT_PAGE_COUNT * offset + 1,
:count => DEFAULT_PAGE_COUNT,
}
end
|
#post(path, options = {}) ⇒ Object
47
48
49
|
# File 'lib/codeforces/client.rb', line 47
def post(path, options = {})
request(:post, path, options).result
end
|
#request(method, path, options = {}) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/codeforces/client.rb', line 51
def request(method, path, options = {})
request_uri = ::Addressable::URI.new
query = {}
query.merge!(options[:query]) unless options[:query].nil?
request_uri.query_values = query
enable_auth!(path, request_uri, query) unless api_key.nil?
path += "?#{request_uri.query}" unless request_uri.query.empty?
tmp_uri = ::Addressable::URI.new
tmp_uri.query_values = options[:data]
@last_response = agent.call(method, path, tmp_uri.query)
logger.debug "#{method.upcase} #{::URI.join endpoint, path}"
logger.debug "Status: #{last_response.data.status}"
unless last_response.data.status === "OK"
raise "Error: #{last_response.data.status} #{last_response.data.}"
end
last_response.data
end
|