Class: Verm::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/verm/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, port: 3404, timeout: 15) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
# File 'lib/verm/client.rb', line 11

def initialize(hostname, port: 3404, timeout: 15)
  @http_client = Net::HTTP.new(hostname, port)
  @http_client.open_timeout = timeout
  @http_client.read_timeout = timeout
  @http_client.ssl_timeout = timeout
end

Instance Attribute Details

#http_clientObject (readonly)

Returns the value of attribute http_client.



9
10
11
# File 'lib/verm/client.rb', line 9

def http_client
  @http_client
end

Class Method Details

.incompressible_typesObject



5
6
7
# File 'lib/verm/client.rb', line 5

def self.incompressible_types
  @incompressible_types ||= %w(image/jpeg image/png image/gif)
end

Instance Method Details

#load(path, initheader = {}, force_text_encoding: "UTF-8") ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/verm/client.rb', line 51

def load(path, initheader = {}, force_text_encoding: "UTF-8")
  response = http_client.request_get(path, initheader)
  response.error! unless response.is_a?(Net::HTTPSuccess)

  if force_text_encoding && response.content_type =~ /text\//
    [response.body.force_encoding(force_text_encoding), response.content_type]
  else
    [response.body, response.content_type]
  end
end

#store(directory, io_or_data, content_type, encoding: nil, autocompress: true) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/verm/client.rb', line 18

def store(directory, io_or_data, content_type, encoding: nil, autocompress: true)
  if %w(application/gzip application/x-gzip).include?(content_type) && encoding.nil?
    raise ArgumentError, "Pass the real content-type and encoding: 'gzip' for gzipped uploads" # see 'File compression' in README.md
  end

  if autocompress && encoding.nil? && !self.class.incompressible_types.include?(content_type)
    io_or_data, encoding = compress(io_or_data)
  end

  directory = "/#{directory}" unless directory[0] == '/'
  request = Net::HTTP::Post.new(directory, 'Content-Type' => content_type)
  request['Content-Encoding'] = encoding.to_s if encoding

  if io_or_data.respond_to?(:read)
    request.body_stream = io_or_data
    if io_or_data.respond_to?(:size)
      request['Content-Length'] = io_or_data.size
    else
      request['Transfer-Encoding'] = 'chunked'
    end

    io_or_data.rewind if io_or_data.respond_to?(:rewind)
    response = http_client.request(request)
  else
    response = http_client.request(request, io_or_data)
  end

  response.error! unless response.is_a?(Net::HTTPSuccess)
  raise "Got a HTTP #{response.code} when trying to store content - should always be 201" unless response.code.to_i == 201
  raise "No location was returned when trying to store content - should always be given" unless response['location']
  response['location']
end

#stream(path, initheader = {}) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/verm/client.rb', line 62

def stream(path, initheader = {})
  http_client.request_get(path, initheader) do |response|
    response.error! unless response.is_a?(Net::HTTPSuccess)
    response.read_body do |chunk|
      yield chunk
    end
  end
end