Class: Ruush::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/ruush/parser.rb

Defined Under Namespace

Classes: AuthObject, HistoryObject, UploadObject

Class Method Summary collapse

Class Method Details

.parse_auth(body) ⇒ Object



44
45
46
47
48
49
# File 'lib/ruush/parser.rb', line 44

def parse_auth(body)
  if body == "-1" # only (known) error code
    raise BadAuth, "Credentials are invalid"
  end
  AuthObject.new *body.split(",")
end

.parse_hist(body) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ruush/parser.rb', line 32

def parse_hist(body)
  hist_objects = []
  if body == "-1" # only (known) error code
    raise BadKey, "API key is invalid"
  else
    body.split("0\n")[1..-1].each do |hist_data| # we have to split on "0\n", and the first element is always blank
      hist_objects.push HistoryObject.new *hist_data.split(",") # magic
    end
  end
  hist_objects
end

.parse_upload(body) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ruush/parser.rb', line 51

def parse_upload(body)
  error_codes = {
    "-1" => [BadKey, "API key is invalid"],
    "-2" => [BadData, "Data sent is invalid"],
    "-3" => [BadHash, "Data hash is invalid"]
    # "-?" => [Errors::QuotaExceded, "Quota exceded"] (needs more research)
  }
  upload_data = body.split ","
  if error_codes.has_key? upload_data[0] # account for multiple error codes
    raise error_codes[upload_data[0]][0], error_codes[upload_data[0]][1]
  end
  UploadObject.new *upload_data[1..-1] # first element is always "0"
end