Class: Azure::Core::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/azure/core/service.rb

Overview

A base class for Service implementations

Direct Known Subclasses

FilteredService

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = '') ⇒ Service

Create a new instance of the Service

host - String. The hostname. (optional, Default empty)



25
26
27
# File 'lib/azure/core/service.rb', line 25

def initialize(host='')
  @host = host
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



29
30
31
# File 'lib/azure/core/service.rb', line 29

def host
  @host
end

Instance Method Details

#call(method, uri, body = nil, headers = nil) {|request| ... } ⇒ Object

Yields:

  • (request)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/azure/core/service.rb', line 31

def call(method, uri, body=nil, headers=nil)
  if headers && !body.nil?
    if headers['Content-Encoding'].nil?
      headers['Content-Encoding'] = body.encoding.to_s
    else 
      body.force_encoding(headers['Content-Encoding']) 
    end
  end

  request = Core::Http::HttpRequest.new(method, uri, body)
  request.headers.merge!(headers) if headers

  request.headers['connection'] = 'keep-alive' if request.respond_to? :headers

  yield request if block_given?

  response = request.call

  if !response.nil? && !response.body.nil? && response.headers['content-encoding']
    response.body.force_encoding(response.headers['content-encoding']) 
  end

  response
end

#generate_uri(path = '', query = {}) ⇒ Object



56
57
58
59
60
# File 'lib/azure/core/service.rb', line 56

def generate_uri(path='', query={})
  uri = URI.parse(File.join(host, path))
  uri.query = URI.encode_www_form(query) unless query == nil or query.empty?
  uri
end