Class: RedboothRuby::Request::Connection
- Inherits:
-
Object
- Object
- RedboothRuby::Request::Connection
- Includes:
- Helpers
- Defined in:
- lib/redbooth-ruby/request/connection.rb
Overview
Connection class
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#request_data ⇒ Object
readonly
Returns the value of attribute request_data.
Instance Method Summary collapse
-
#download_file(uri, http) ⇒ Mixed
Download a file using different strategies.
-
#download_file_with_redirect ⇒ Object
Downloads the desired file following redirects to amazon s3 without authentication headers.
-
#download_http(uri) ⇒ Object
Generate a download http from a URI.
-
#download_to_path(uri, http, path) ⇒ Mixed
Download a file to a path.
-
#initialize(request_info) ⇒ Connection
constructor
A new instance of Connection.
- #request ⇒ Object
- #set_request_data ⇒ Object
Methods included from Helpers
#flatten_hash_keys, #normalize_params
Constructor Details
#initialize(request_info) ⇒ Connection
Returns a new instance of Connection.
10 11 12 13 14 |
# File 'lib/redbooth-ruby/request/connection.rb', line 10 def initialize(request_info) @info = request_info @session = @info.session if @info @access_token = @info.session.access_token if @session end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
8 9 10 |
# File 'lib/redbooth-ruby/request/connection.rb', line 8 def access_token @access_token end |
#request_data ⇒ Object (readonly)
Returns the value of attribute request_data.
8 9 10 |
# File 'lib/redbooth-ruby/request/connection.rb', line 8 def request_data @request_data end |
Instance Method Details
#download_file(uri, http) ⇒ Mixed
Download a file using different strategies
63 64 65 66 67 68 69 |
# File 'lib/redbooth-ruby/request/connection.rb', line 63 def download_file(uri, http) if @info..key?(:download_path) download_to_path(uri, http, @info.[:download_path]) else http.request(Net::HTTP::Get.new(uri)) end end |
#download_file_with_redirect ⇒ Object
Downloads the desired file following redirects to amazon s3 without authentication headers
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/redbooth-ruby/request/connection.rb', line 31 def download_file_with_redirect max_redirects = access_token..fetch(:max_redirects, 20) response = access_token.send( :get, URI.encode(api_url), redirect_count: max_redirects + 1 ) return response unless [302, 301].include? response.status url = response.headers['Location'] uri = URI.parse(url) http = download_http(uri) http.start do |inner_http| download_file(uri, inner_http) end end |
#download_http(uri) ⇒ Object
Generate a download http from a URI
51 52 53 54 55 56 |
# File 'lib/redbooth-ruby/request/connection.rb', line 51 def download_http(uri) http = Net::HTTP.new(uri.host, Net::HTTP.https_default_port) http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.use_ssl = RedboothRuby.configuration[:use_ssl] http end |
#download_to_path(uri, http, path) ⇒ Mixed
Download a file to a path
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/redbooth-ruby/request/connection.rb', line 77 def download_to_path(uri, http, path) request = Net::HTTP::Get.new uri http.request request do |response| open path, 'w' do |io| response.read_body do |chunk| io.write chunk end io.write "\n" end end end |
#request ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/redbooth-ruby/request/connection.rb', line 16 def request return unless access_token case when use_body_file? multipart_request when download_file? download_file_with_redirect else access_token.send(*request_data) end end |
#set_request_data ⇒ Object
90 91 92 93 94 95 |
# File 'lib/redbooth-ruby/request/connection.rb', line 90 def set_request_data @request_data = [] @request_data << @info.http_method if @info @request_data << api_url @request_data << { body: body_hash } unless use_url_params? end |