Class: Firstjob::HttpParser

Inherits:
Object
  • Object
show all
Defined in:
lib/firstjob/http_parser.rb

Class Method Summary collapse

Class Method Details

.parse_json_response(response) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/firstjob/http_parser.rb', line 28

def self.parse_json_response(response)
  Firstjob.last_response = response
  Firstjob.last_request = response.request
  case response.code
    when 200..201
      # "All good!"
      return JSON.parse(response.body)
    when 401
      Firstjob.invalidate_access_token!
      raise "Error 401: Unauthorized. Check login info.\n #{response.body}"
    when 403
      raise "Error 403: Forbidden"
    when 404
      raise "Error 404 not found"
    when 500...600
      raise "ZOMG ERROR #{response.code}: #{response.request.path}, #{response.body}"
    else
      raise "Error #{response.code}, unkown response: #{response.request.path}, #{response.body}"
  end
end

.parse_json_to_hash(json, hash) ⇒ Object



3
4
5
6
# File 'lib/firstjob/http_parser.rb', line 3

def self.parse_json_to_hash(json, hash)
  json.each{|object| hash[object["id"]] ? hash[object["id"]].merge!(object) : hash[object["id"]] = object}
  return hash
end

.parse_response(response) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/firstjob/http_parser.rb', line 8

def self.parse_response(response)
  Firstjob.last_response = response
  Firstjob.last_request = response.request
  case response.code
    when 200..201
      # "All good!"
      return response.body
    when 401
      Firstjob.invalidate_access_token!
      raise "Error 401: Unauthorized. Check login info.\n #{response.body}"
    when 403
      raise "Error 403: Forbidden"
    when 404
      raise "Error 404 not found"
    when 500...600
      raise "ZOMG ERROR #{response.code}: #{response.request.path}, #{response.body}"
    else
      raise "Error #{response.code}, unkown response: #{response.request.path}, #{response.body}"
  end
end