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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#each_contest, #each_status, #user

Constructor Details

#initialize(endpoint = DEFAULT_ENDPOINT) ⇒ Client

Returns a new instance of Client.



19
20
21
# File 'lib/codeforces/client.rb', line 19

def initialize(endpoint = DEFAULT_ENDPOINT)
  @endpoint = endpoint
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



17
18
19
# File 'lib/codeforces/client.rb', line 17

def endpoint
  @endpoint
end

Instance Method Details

#agentObject



27
28
29
# File 'lib/codeforces/client.rb', line 27

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

#apiObject



65
66
67
# File 'lib/codeforces/client.rb', line 65

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

#create_submission(status) ⇒ Object



73
74
75
# File 'lib/codeforces/client.rb', line 73

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

#create_user(user) ⇒ Object



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

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

#get(path, options = {}) ⇒ Object



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

def get(path, options = {})
  request_uri = ::Addressable::URI.new
  options[:query] ||= {}
  request_uri.query_values = options[:query]
  request(:get, "#{path}#{request_uri.query.empty? ? "" : "?#{request_uri.query}"}", options[:data], options).result
end

#last_responseObject



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

def last_response
  @last_response
end

#loggerObject



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

def logger
  @logger ||= new_logger
end

#multi_values(values) ⇒ Object



61
62
63
# File 'lib/codeforces/client.rb', line 61

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

#paginate(offset) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/codeforces/client.rb', line 53

def paginate(offset)
  offset = 0 if offset.nil?
  result = {
    :from => DEFAULT_PAGE_COUNT * offset + 1,
    :count => DEFAULT_PAGE_COUNT,
  }
end

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



42
43
44
45
46
47
48
49
50
51
# File 'lib/codeforces/client.rb', line 42

def request(method, path, data, options = {})
  logger.debug "#{method.upcase} #{URI.join endpoint, path}"
  @last_response = agent.call(method, path, data)

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

  last_response.data
end