Class: Twurl::RequestController

Inherits:
AbstractCommandController show all
Defined in:
lib/twurl/request_controller.rb

Constant Summary collapse

NO_URI_MESSAGE =
'No URI specified'
INVALID_URI_MESSAGE =
'Invalid URI detected'
READ_TIMEOUT_MESSAGE =
'A timeout occurred (Net::ReadTimeout). ' \
'Please try again or increase the value using --timeout option.'
OPEN_TIMEOUT_MESSAGE =
'A timeout occurred (Net::OpenTimeout). ' \
'Please try again or increase the value using --connection-timeout option.'

Instance Attribute Summary

Attributes inherited from AbstractCommandController

#client, #options

Instance Method Summary collapse

Methods inherited from AbstractCommandController

dispatch, #initialize

Constructor Details

This class inherits a constructor from Twurl::AbstractCommandController

Instance Method Details

#dispatchObject

Raises:



9
10
11
12
13
14
15
16
# File 'lib/twurl/request_controller.rb', line 9

def dispatch
  if client.needs_to_authorize?
    raise Exception, "You need to authorize first."
  end
  options.path ||= OAuthClient.rcfile.alias_from_options(options)
  raise Exception, NO_URI_MESSAGE if options.path.empty?
  perform_request
end

#perform_requestObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/twurl/request_controller.rb', line 18

def perform_request
  client.perform_request_from_options(options) { |response|
    response.read_body { |body|
      CLI.print options.json_format ? JsonFormatter.format(body) : body
    }
  }
rescue URI::InvalidURIError
  raise Exception, INVALID_URI_MESSAGE
rescue Net::ReadTimeout
  raise Exception, READ_TIMEOUT_MESSAGE
rescue Net::OpenTimeout
  raise Exception, OPEN_TIMEOUT_MESSAGE
end