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, #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



84
85
86
# File 'lib/codeforces/client.rb', line 84

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

#create_contest(contest) ⇒ Object



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

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

#create_contests(contests) ⇒ Object



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

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

#create_problem(problem) ⇒ Object



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

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

#create_problems(problems) ⇒ Object



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

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

#create_submission(status) ⇒ Object



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

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

#create_user(user) ⇒ Object



88
89
90
# File 'lib/codeforces/client.rb', line 88

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

#create_users(users) ⇒ Object



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

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



80
81
82
# File 'lib/codeforces/client.rb', line 80

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

#paginate(offset) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/codeforces/client.rb', line 72

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
# 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}?#{options[:data]}"
  @last_response = agent.call(method, path, :query => options[:data])
  logger.debug "Status: #{last_response.data.status}"

  unless last_response.data.status === "OK"
    raise "Error: #{last_response.data.status} #{last_response.data.comment}"
  end

  last_response.data
end