Class: Imager::ServerInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/imager/server_interface.rb

Class Method Summary collapse

Class Method Details

.clientObject



81
82
83
84
85
86
# File 'lib/imager/server_interface.rb', line 81

def self.client
  unless Imager::ServerClient.base_uri
    Imager::ServerClient.base_uri Imager.base_uri + '/' + Imager.manager_path
  end
  Imager::ServerClient
end

.delete(collection, album, file_id) ⇒ Object

Parameters:

  • Collection (String)

    for save the image

  • Album (String)

    for save the image

  • File (String)

    id

Raises:

  • (ImagerError)

    if collection or album or file_id is wrong

  • (ArgumentError)

    when something with server comunication is wrong



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/imager/server_interface.rb', line 66

def self.delete(collection, album, file_id)
  options = {}
  options[:collection] = collection.to_s
  options[:album]      = album.to_s
  options[:file_id]    = file_id.to_s

  options_json = JSON.generate options

  query = {}
  query[:auth]    = auth_token(options_json)
  query[:options] = options_json

  return parse client.post('/delete.php', multipart: true, body: query), true
end

.post(collection, album, file, sizes, file_id = nil) ⇒ void

This method returns an undefined value.

Parameters:

  • Collection (String)

    for save the image

  • Album (String)

    for save the image

  • The (UploadIO, File, String)

    image file or the path to it

  • Sizes (Hash)
  • File (String)

    id. if passed file_id will be this instead of file.original_filename or File.basename(file).

Raises:

  • (ImagerError)

    if some server validation failed.

  • (ArgumentError)

    when something with server comunication is wrong



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
50
51
52
53
54
55
56
57
58
59
# File 'lib/imager/server_interface.rb', line 19

def self.post(collection, album, file, sizes, file_id = nil)
  raise Imager::ImagerError "File is empty", caller unless file

  if file.is_a?(String)
    raise Imager::ImagerError, "File is not a file", caller unless File.file?(file)
    file = File.new(file)
  end

  options = {}
  options[:collection] = collection.to_s
  options[:album]      = album.to_s
  options[:sizes]      = sizes

  options[:file_id]    = file_id
  options[:file_id]  ||= file.original_filename if file.respond_to?(:original_filename)
  options[:file_id]  ||= File.basename(file)

  # Remove file extension
  options[:file_id].gsub!(/(\..{3,4})\z/i, '')

  begin
    options[:file_md5]   = Digest::MD5.file(file)
    options[:file_sha1]  = Digest::SHA1.file(file)
  rescue
    raise Imager::ImagerError, "Cannot read the file", caller unless file.respond_to?(:read)

    # Fix for rubinius
    options[:file_md5]  = Digest::MD5.hexdigest(file.read)
    options[:file_sha1] = Digest::SHA1.hexdigest(file.read)
  end

  options_json = JSON.generate(options);

  query = {
    file: file,
    options: options_json,
    auth: auth_token(options_json)
  };

  return parse client.post('/post.php', multipart: true, body: query)
end