Class: Heroku::Client::JSONUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/heroku-client/json_util.rb

Instance Method Summary collapse

Instance Method Details

#parse_error(body) ⇒ Object



5
6
7
# File 'lib/heroku-client/json_util.rb', line 5

def parse_error(body)
  parse_json_error(body) || parse_plain_text_error(body)
end

#parse_json_error(body) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/heroku-client/json_util.rb', line 9

def parse_json_error(body)
  json = JSON.parse body rescue false

  case json
  when Array
    json.first.join ' ' # message like [["name", "is already taken"]]
  when Hash
    json['error'] # message like {"error": "The error message"}
  end
end

#parse_plain_text_error(body) ⇒ Object



20
21
22
# File 'lib/heroku-client/json_util.rb', line 20

def parse_plain_text_error(body)
  body
end

#parse_response(body) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/heroku-client/json_util.rb', line 24

def parse_response(body)
  parsed = JSON.parse(body)

  return parsed.map { |o| HerokuObj.new(o) } if parsed.is_a? Array

  HerokuObj.new parsed
end