Class: Codeforces::Client

Inherits:
Object
  • Object
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, #rounds, #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_keyObject (readonly)

Returns the value of attribute api_key.



22
23
24
# File 'lib/codeforces/client.rb', line 22

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



23
24
25
# File 'lib/codeforces/client.rb', line 23

def api_secret
  @api_secret
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



21
22
23
# File 'lib/codeforces/client.rb', line 21

def endpoint
  @endpoint
end

Instance Method Details

#agentObject



35
36
37
# File 'lib/codeforces/client.rb', line 35

def agent
  @agent ||= ::Sawyer::Agent.new(DEFAULT_ENDPOINT)
end

#apiObject



95
96
97
# File 'lib/codeforces/client.rb', line 95

def api
  @api ||= ::Codeforces::Api.new(self)
end

#create_contest(contest) ⇒ Object



111
112
113
# File 'lib/codeforces/client.rb', line 111

def create_contest(contest)
  ::Codeforces::Models::Contest.new self, contest
end

#create_contests(contests) ⇒ Object



115
116
117
# File 'lib/codeforces/client.rb', line 115

def create_contests(contests)
  ::Codeforces::Models::Contests.new self, contests
end

#create_problem(problem) ⇒ Object



119
120
121
# File 'lib/codeforces/client.rb', line 119

def create_problem(problem)
  ::Codeforces::Models::Problem.new self, problem
end

#create_problems(problems) ⇒ Object



123
124
125
# File 'lib/codeforces/client.rb', line 123

def create_problems(problems)
  ::Codeforces::Models::Problems.new self, problems
end

#create_submission(status) ⇒ Object



103
104
105
# File 'lib/codeforces/client.rb', line 103

def create_submission(status)
  ::Codeforces::Models::Submission.new self, status
end

#create_user(user) ⇒ Object



99
100
101
# File 'lib/codeforces/client.rb', line 99

def create_user(user)
  ::Codeforces::Models::User.new self, user
end

#create_users(users) ⇒ Object



107
108
109
# File 'lib/codeforces/client.rb', line 107

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_responseObject



39
40
41
# File 'lib/codeforces/client.rb', line 39

def last_response
  @last_response
end

#loggerObject



31
32
33
# File 'lib/codeforces/client.rb', line 31

def logger
  @logger ||= new_logger
end

#multi_values(values) ⇒ Object



91
92
93
# File 'lib/codeforces/client.rb', line 91

def multi_values(values)
  values.join ";"
end

#paginate(offset) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/codeforces/client.rb', line 83

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

#raise_codeforces_status(res) ⇒ Object



76
77
78
79
80
81
# File 'lib/codeforces/client.rb', line 76

def raise_codeforces_status(res)
  logger.debug "Codeforces: #{res.data[:status]}"
  unless res.data.status === "OK"
    raise "Error: #{res.data[:status]} #{res.data[:comment]}"
  end
end

#raise_http_status(res) ⇒ Object



69
70
71
72
73
74
# File 'lib/codeforces/client.rb', line 69

def raise_http_status(res)
  logger.debug "HTTP: #{res.status}"
  unless res.status === 200
    raise "Error: HTTP Status is #{res.status}"
  end
end

#request(method, path, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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 method === :post || api_key.nil?

  path += "?#{request_uri.query}" unless request_uri.query.empty?

  logger.debug "#{method.upcase} #{::URI.join endpoint, path} with #{options[:data]}"
  @last_response = agent.call(method, path, :query => options[:data])

  raise_http_status last_response
  raise_codeforces_status last_response
  last_response.data
end