Class: LogsTF::Upload

Inherits:
Object
  • Object
show all
Defined in:
lib/logs_tf/upload.rb

Defined Under Namespace

Classes: Error, GuruMeditationError, InvalidAPIKeyError, InvalidLogError, LogIsEmptyError, MissingAPIKeyOrLoginError, MissingLogError, NoValidRoundsError, NotAuthenticatedError, NotEnoughPlayersError, ParsingFailedError, RequestError, UnknownLogsTfError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log, logs_tf_url = 'http://logs.tf') ⇒ Upload

Returns a new instance of Upload.



11
12
13
14
# File 'lib/logs_tf/upload.rb', line 11

def initialize(log, logs_tf_url = 'http://logs.tf')
  @log          = log
  @logs_tf_url  = logs_tf_url
end

Instance Attribute Details

#api_key=(value) ⇒ Object

Sets the attribute api_key

Parameters:

  • value

    the value to set the attribute api_key to.



9
10
11
# File 'lib/logs_tf/upload.rb', line 9

def api_key=(value)
  @api_key = value
end

#logObject

Returns the value of attribute log.



9
10
11
# File 'lib/logs_tf/upload.rb', line 9

def log
  @log
end

#logs_tf_urlObject

Returns the value of attribute logs_tf_url.



9
10
11
# File 'lib/logs_tf/upload.rb', line 9

def logs_tf_url
  @logs_tf_url
end

#responseObject

Returns the value of attribute response.



9
10
11
# File 'lib/logs_tf/upload.rb', line 9

def response
  @response
end

#response_bodyObject

Returns the value of attribute response_body.



9
10
11
# File 'lib/logs_tf/upload.rb', line 9

def response_body
  @response_body
end

Instance Method Details

#errorObject



60
61
62
# File 'lib/logs_tf/upload.rb', line 60

def error
  response_body["error"]
end

#raise_logs_tf_errorObject

Raises:

  • (error_class)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/logs_tf/upload.rb', line 31

def raise_logs_tf_error
  error_class = case error
  when "Invalid log file"
    InvalidLogError
  when "No file"
    MissingLogError
  when "Not authenticated"
    NotAuthenticatedError
  when "Invalid API key"
    InvalidAPIKeyError
  when "Log has no valid rounds (at least one needed)"
    NoValidRoundsError
  when "Not enough players (2 needed)"
    NotEnoughPlayersError
  when "Log is empty"
    LogIsEmptyError
  when /^Parsing failed in line \d+$/
    ParsingFailedError
  when "Missing API key or login"
    MissingAPIKeyOrLoginError
  when "Guru Meditation"
    GuruMeditationError
  else
    UnknownLogsTfError
  end

  raise error_class, response_body["error"]
end

#request_success?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/logs_tf/upload.rb', line 72

def request_success?
  response.status == 200
end

#sendObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/logs_tf/upload.rb', line 16

def send
  @response = connection.post('/upload', post_options)
  if request_success?
    if !upload_success?
      raise_logs_tf_error
    end
  else
    raise RequestError, response.body
  end
end

#upload_success?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/logs_tf/upload.rb', line 68

def upload_success?
  response_body["success"] == true
end

#urlObject



27
28
29
# File 'lib/logs_tf/upload.rb', line 27

def url
  logs_tf_url + response_body["url"]
end