Module: Rhc::Rest

Included in:
Application, Cartridge, Client, Domain, Key, User
Defined in:
lib/rhc-rest.rb,
lib/rhc-rest/key.rb,
lib/rhc-rest/user.rb,
lib/rhc-rest/client.rb,
lib/rhc-rest/domain.rb,
lib/rhc-rest/version.rb,
lib/rhc-rest/cartridge.rb,
lib/rhc-rest/application.rb,
lib/rhc-rest/exceptions/exceptions.rb

Defined Under Namespace

Classes: Application, BaseException, Cartridge, Client, ClientErrorException, Domain, Key, RequestDeniedException, ResourceAccessException, ResourceNotFoundException, ServerErrorException, ServiceUnavailableException, UnAuthorizedException, User, ValidationException

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Instance Method Details

#loggerObject



18
19
20
21
22
23
24
# File 'lib/rhc-rest.rb', line 18

def logger
  if defined?Rails.logger
    Rails.logger
  else
    Logger.new(STDOUT)
  end
end

#parse_response(response) ⇒ Object



26
27
28
29
30
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
59
60
61
62
63
64
65
66
67
68
# File 'lib/rhc-rest.rb', line 26

def parse_response(response)
  result = JSON.parse(response)
  type = result['type']
  data = result['data']
  case type
  when 'domains'
    domains = Array.new
    data.each do |domain_json|
      domains.push(Domain.new(domain_json))
    end
    return domains
  when 'domain'
    return Domain.new(data)
  when 'applications'
    apps = Array.new
    data.each do |app_json|
      apps.push(Application.new(app_json))
    end
    return apps
  when 'application'
    return Application.new(data)
  when 'cartridges'
    carts = Array.new
    data.each do |cart_json|
      carts.push(Cartridge.new(cart_json))
    end
    return carts
  when 'cartridge'
    return Cartridge.new(data)
  when 'user'
    return User.new(data)
  when 'keys'
    keys = Array.new
    data.each do |key_json|
      keys.push(Key.new(key_json))
    end
    return keys
  when 'key'
    return Key.new(data)
  else
  data
  end
end

#process_error_response(response) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rhc-rest.rb', line 92

def process_error_response(response)
  messages = Array.new
  begin
    result = JSON.parse(response)
    messages = result['messages']
  rescue Exception => e
    logger.debug "Response did not include a message from server" if @mydebug
    #puts response
  end
  case response.code
  when 401
    raise UnAuthorizedException.new("Not authenticated")
  when 403
    messages.each do |message|
      if message['severity'].upcase == "ERROR"
        raise RequestDeniedException.new(message['text'])
      end
    end
  when 404
    messages.each do |message|
      if message['severity'].upcase == "ERROR"
        raise ResourceNotFoundException.new(message['text'])
      end
    end
  when 409
    messages.each do |message|
      if message['severity'] and message['severity'].upcase == "ERROR"
        raise ValidationException.new(message['text'])
      end
    end
  when 422
    #puts response
    e = nil
    messages.each do |message|
      if message["field"]
        if e and e.field ==message["field"]
          e.message << " #{message["text"]}"
        else
          e = ValidationException.new(message["text"], message["field"])
        end
      end
    end
    raise e
  when 400
    messages.each do |message|
      if message['severity'].upcase == "ERROR"
        raise ClientErrorException.new(message['text'])
      end
    end
  when 500
    messages.each do |message|
      if message['severity'].upcase == "ERROR"
        raise ServerErrorException.new(message['text'])
      end
    end
  when 503
    messages.each do |message|
      if message['severity'].upcase == "ERROR"
        raise ServiceUnavailableException.new(message['text'])
      end
    end
  else
    raise ResourceAccessException.new("Server returned error code with no output: #{response.code}")
  end

end

#send(request) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rhc-rest.rb', line 70

def send(request)
  begin
    #puts request.headers
    response = request.execute
    #set cookie
    rh_sso = response.cookies['rh_sso']
    #puts response.cookies
    if not rh_sso.nil?
      @@headers["cookie"] = "rh_sso=#{rh_sso}"
    end
    #puts "#{response}"
    return parse_response(response) unless response.nil? or response.code == 204
  rescue RestClient::RequestTimeout, RestClient::ServerBrokeConnection, RestClient::SSLCertificateNotVerified => e
    raise ResourceAccessException.new("Failed to access resource: #{e.message}")
  rescue RestClient::ExceptionWithResponse => e
  #puts "#{e.response}"
    process_error_response(e.response)
  rescue Exception => e
    raise ResourceAccessException.new("Failed to access resource: #{e.message}")
  end
end