Class: RedboothRuby::Request::Connection

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/redbooth-ruby/request/connection.rb

Overview

Connection class

Instance Attribute Summary collapse

Instance Method Summary collapse

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_tokenObject (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_dataObject (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

Parameters:

  • uri (URI)

    The URI to use

  • http (Net::HTTP)

    The http to use

Returns:

  • (Mixed)

    The downloaded file or the result of the download



63
64
65
66
67
68
69
# File 'lib/redbooth-ruby/request/connection.rb', line 63

def download_file(uri, http)
  if @info.options.key?(:download_path)
    download_to_path(uri, http, @info.options[:download_path])
  else
    http.request(Net::HTTP::Get.new(uri))
  end
end

#download_file_with_redirectObject

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.options.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

Parameters:

  • uri (URI)

    The URI to use



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

Parameters:

  • uri (URI)

    The URI to use

  • http (Net::HTTP)

    The http to use

  • path (String)

    The path to save the file to

Returns:

  • (Mixed)

    The result of the operation



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

#requestObject



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_dataObject



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