Class: Wakame::JsonRequester

Inherits:
Object
  • Object
show all
Defined in:
lib/wakame/runner/administrator_command.rb

Defined Under Namespace

Classes: AuthenticationError, CommandError, ResponseError, ServerError

Instance Method Summary collapse

Constructor Details

#initialize(common_opts, merge_opts = {}) ⇒ JsonRequester

Returns a new instance of JsonRequester.



137
138
139
140
# File 'lib/wakame/runner/administrator_command.rb', line 137

def initialize(common_opts, merge_opts={})
  @common_opts = common_opts
  @merge_opts = merge_opts.dup
end

Instance Method Details

#request(options = {}) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/wakame/runner/administrator_command.rb', line 142

def request(options={})
  request_uri = URI.parse(@common_opts[:command_server_uri])
  @merge_opts[:timestamp] = Time.now.utc.strftime("%Y%m%dT%H%M%SZ")
  request_uri.path = (request_uri.path.nil? || request_uri.path == '') ? '/' : request_uri.path
  request_uri.query = sign_query(build_escaped_query(options.merge(@merge_opts)))

  res = Net::HTTP.get_response(request_uri)
  hash = JSON.parse(res.body)
  if res.is_a?(Net::HTTPSuccess)
    # 
  else
    case res.code
    when '404'
      raise CommandError.new(hash)
    when '403'
      raise AuthenticationError.new(hash)
    when '500'
      raise ServerError.new(hash)
    else
      fail "Unknown HTTP Error Code: #{res.code}: #{res.message}"
    end
  end

  if @common_opts[:json_print]
    require 'pp'
    puts "Response for: #{request_uri.to_s}"
    pp hash
  end

  hash
end