Class: Sndacs::Connection

Inherits:
Object
  • Object
show all
Includes:
Parser
Defined in:
lib/sndacs/connection.rb

Overview

Class responsible for handling connections to grandcloud hosts

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Parser

#parse_all_buckets_result, #parse_all_objects_result, #parse_copy_object_result, #parse_error, #parse_is_truncated, #parse_location_constraint, #rexml_document

Constructor Details

#initialize(options = {}) ⇒ Connection

Creates new connection object.

Options

  • :access_key_id - Access key id (REQUIRED)

  • :secret_access_key - Secret access key (REQUIRED)

  • :use_ssl - Use https or http protocol (false by default)

  • :debug - Display debug information on the STDOUT (false by default)

  • :timeout - Timeout to use by the Net::HTTP object (60 by default)

  • :proxy - Hash for Net::HTTP Proxy settings { :host => “proxy.mydomain.com”, :port => “80, :user => ”user_a“, :password => ”secret“ }

  • :proxy - Hash for Net::HTTP Proxy settings

  • :chunk_size - Size of a chunk when streaming (1048576 (1 MiB) by default)



30
31
32
33
34
35
36
37
38
# File 'lib/sndacs/connection.rb', line 30

def initialize(options = {})
  @access_key_id = options.fetch(:access_key_id, Config.access_key_id)
  @secret_access_key = options.fetch(:secret_access_key, Config.secret_access_key)
  @proxy = options.fetch(:proxy, Config.proxy)
  @timeout = options.fetch(:timeout, Config.timeout)
  @use_ssl = options.fetch(:use_ssl, Config.use_ssl)
  @chunk_size = options.fetch(:chunk_size, Config.chunk_size)
  @debug = options.fetch(:debug, Config.debug)
end

Instance Attribute Details

#access_key_idObject

Returns the value of attribute access_key_id.



11
12
13
# File 'lib/sndacs/connection.rb', line 11

def access_key_id
  @access_key_id
end

#debugObject

Returns the value of attribute debug.



11
12
13
# File 'lib/sndacs/connection.rb', line 11

def debug
  @debug
end

#proxyObject

Returns the value of attribute proxy.



11
12
13
# File 'lib/sndacs/connection.rb', line 11

def proxy
  @proxy
end

#secret_access_keyObject

Returns the value of attribute secret_access_key.



11
12
13
# File 'lib/sndacs/connection.rb', line 11

def secret_access_key
  @secret_access_key
end

#timeoutObject

Returns the value of attribute timeout.



11
12
13
# File 'lib/sndacs/connection.rb', line 11

def timeout
  @timeout
end

#use_sslObject Also known as: use_ssl?

Returns the value of attribute use_ssl.



11
12
13
# File 'lib/sndacs/connection.rb', line 11

def use_ssl
  @use_ssl
end

Class Method Details

.parse_headers(headers) ⇒ Object

Helper function to change headers from symbols, to in correct form (i.e. with ‘-’ instead of ‘_’)

Parameters

  • headers - Hash of pairs headername => value, where value can be Range (for Range header) or any other value which can be translated to string

Returns

Hash of headers translated from symbol to string, containing only interesting headers



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
173
174
175
176
177
# File 'lib/sndacs/connection.rb', line 142

def self.parse_headers(headers)
  interesting_keys = [:content_type, :cache_control, :x_snda_acl, :x_snda_storage_class, :range,
                      :if_modified_since, :if_unmodified_since,
                      :if_match, :if_none_match,
                      :content_disposition, :content_encoding,
                      :x_snda_copy_source, :x_snda_metadata_directive,
                      :x_snda_copy_source_if_match,
                      :x_snda_copy_source_if_none_match,
                      :x_snda_copy_source_if_unmodified_since,
                      :x_snda_copy_source_if_modified_since]
   = "x-snda-meta-"
  parsed_headers = {}
  if headers
    headers.each do |key, value|
      if interesting_keys.include?(key)
        parsed_key = key.to_s.gsub("_", "-")
        parsed_value = value

        case value
        when Range
          parsed_value = "bytes=#{value.first}-#{value.last}"
        end

        parsed_headers[parsed_key] = parsed_value
      else
          parsed_key = key.to_s.gsub("_","-")
          if parsed_key[0,.length] === 
             parsed_headers[parsed_key] = value
          end
        
      end
    end
  end

  parsed_headers
end

.parse_params(params) ⇒ Object

Helper function to parser parameters and create single string of params added to questy string

Parameters

  • params - Hash of parameters

Returns

String – containing all parameters joined in one params string, i.e. param1=val&param2&param3=0



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/sndacs/connection.rb', line 111

def self.parse_params(params)
  interesting_keys = [:max_keys, :prefix, :marker, :delimiter, :location]

  result = []
  params.each do |key, value|
    if interesting_keys.include?(key)
      parsed_key = key.to_s.gsub("_", "-")

      case value
      when nil
        result << parsed_key
      else
        result << "#{parsed_key}=#{value}"
      end
    end
  end

  result.join("&")
end

Instance Method Details

#request(method, options) ⇒ Object

Makes request with given HTTP method, sets missing parameters, adds signature to request header and returns response object (Net::HTTPResponse)

Parameters

  • method - HTTP Method symbol, can be :get, :put, :delete

Options:

  • :host - Hostname to connecto to, defaults to storage.grandcloud.cn

  • :path - path to send request to (REQUIRED)

  • :body - Request body, only meaningful for :put request

  • :params - Parameters to add to query string for request, can be String or Hash

  • :headers - Hash of headers fields to add to request header

Returns

Net::HTTPResponse object – response from the server



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sndacs/connection.rb', line 61

def request(method, options)
  host = options.fetch(:host, Config.host)
  path = options.fetch(:path)
  body = options.fetch(:body, nil)
  params = options.fetch(:params, {})
  headers = options.fetch(:headers, {})
  subresource = options.fetch(:subresource,nil)
  # Must be done before adding params
  # Encodes all characters except forward-slash (/) and explicitly legal URL characters
  path = URI.escape(path, /[^#{URI::REGEXP::PATTERN::UNRESERVED}\/]/)
  if subresource
     path << "?#{subresource}"
  end
  if params
    params = params.is_a?(String) ? params : self.class.parse_params(params)

    if params != ''
      path << "?#{params}"
    end
  end

  request = Request.new(@chunk_size, method.to_s.upcase, !!body, method.to_s.upcase != "HEAD", path)

  headers = self.class.parse_headers(headers)
  headers.each do |key, value|
    request[key] = value
  end

  if body
    if body.respond_to?(:read)
      request.body_stream = body
    else
      request.body = body
    end

    request.content_length = body.respond_to?(:lstat) ? body.stat.size : body.size
  end

  send_request(host, request)
end