Class: Yt::Models::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/yt/models/request.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Request

Returns a new instance of Request.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/yt/models/request.rb', line 16

def initialize(options = {})
  @auth = options[:auth]
  @body = options[:body]
  @file = options[:file]
  @body_type = options.fetch :body_type, :json
  @expected_response = options.fetch :expected_response, Net::HTTPSuccess
  @format = options.fetch :format, :json
  @headers = options.fetch :headers, gzip_headers
  @host = options.fetch :host, google_api_host
  @method = options.fetch :method, :get
  @path = options[:path]
  @query = options.fetch(:params, {}).to_param
end

Instance Method Details

#request_error_messageObject



38
39
40
41
42
43
# File 'lib/yt/models/request.rb', line 38

def request_error_message
  {}.tap do |message|
    message[:request_curl] = as_curl
    message[:response_body] = JSON(response.body) rescue response.inspect
  end.to_json
end

#runObject



30
31
32
33
34
35
36
# File 'lib/yt/models/request.rb', line 30

def run
  if response.is_a? @expected_response
    response.tap{|response| response.body = parse_format response.body}
  else
    run_again? ? run : raise(response_error, request_error_message)
  end
end