Class: TF2R::API

Inherits:
Object
  • Object
show all
Defined in:
lib/tf2r/api.rb

Overview

This class handles interaction with TF2R’s “API”, which is really just an open endpoint located at tf2r.com/job.php, in use all over the site for dynamically updating pages.

Constant Summary collapse

HOST =
'http://tf2r.com'
ENDPOINT =
'/job.php'
MAX_ENTRY_RESPONSE_COUNT =

newentry on a checkraffle job can only return up to 2500 entries. This is a problem for any raffles with a maximum entries greater than 2500.

2500

Class Method Summary collapse

Class Method Details

.raffle_info(link_snippet) ⇒ Array

Queries the API for information about a raffle.

Returns:

  • (Array)

    the API response for the raffle.

    • ended (Boolean) whether the raffle is done.

    • timeleft (Fixnum) seconds remaining.

    • max_entry (Fixnum) maximum number of entries.

    • entry (String) but really a number. This is the ID in TF2R’s db of

      the last entry returned in the response.
      
    • wc (Float) the chance for any one participant to win.

    • cur_entry (Fixnum) current number of entries.

    • started (Boolean) whether the raffle has begun.

    • newentry (Array) contains hashes representing participants in

      chronological order.
      
    • chaten (Array) I have no idea.

    • chatmax (+Fixnum) ¯_(ツ)_/¯



28
29
30
31
32
# File 'lib/tf2r/api.rb', line 28

def self.raffle_info(link_snippet)
  params = {checkraffle: true, rid: link_snippet[1..-1],
            lastentrys: 0, lastchat: 0}
  request(params)['message']
end

.request(params) ⇒ Object

Submits a request to job.php with the given params. TODO: should this do something with the responses headers?



38
39
40
41
42
43
44
45
46
47
# File 'lib/tf2r/api.rb', line 38

def self.request(params)
  uri = URI.parse(HOST)
  http = Net::HTTP.new(uri.host, uri.port)

  request = Net::HTTP::Post.new(ENDPOINT)
  request.body = URI.encode_www_form(params)

  response = http.request(request).body
  JSON.parse(response)
end