Class: Imaginary::Client

Inherits:
Object
  • Object
show all
Includes:
HTTMultiParty
Defined in:
lib/imaginary.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
# File 'lib/imaginary.rb', line 8

def initialize(url, options = {})
  @base_url = url
  @auth = { username: options[:username], password: options[:password] }
  @bucket = options[:bucket]
  @secret = options[:secret]
end

Instance Method Details

#add_image_from_file(file, name = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/imaginary.rb', line 20

def add_image_from_file(file, name = nil)
  r = post("#{@base_url}buckets/#{@bucket}/images.json", body: { image: {
    name: name,
    image: file
  }})

  if r["errors"]
    raise "Failed. #{r["errors"]}"
  else
    r["name"]
  end
end

#add_image_from_url(url, name = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/imaginary.rb', line 33

def add_image_from_url(url, name = nil)
  r = post("#{@base_url}buckets/#{@bucket}/images.json", body: { image: {
    name: name,
    image_url: url
  }})

  if r["errors"]
    raise "Failed. #{r["errors"]}"
  else
    r["name"]
  end
end

#image_url(name, options = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/imaginary.rb', line 46

def image_url(name, options = nil)
  parts = ['x', @bucket]

  if options
    if options.is_a?(String)
      parts << options
    elsif options.is_a?(Array)
      parts += options
    else
      raise "Invalid options."
    end
  end

  # add image name
  parts << name

  # build path
  path = parts.join('/')

  # add signature
  if @secret
    signature_string = [path, @secret].join('/')
    signature = Digest::SHA1.hexdigest(signature_string)[0..15]
    path << "-#{signature}"
  end

  @base_url + path
end

#post(url, params) ⇒ Object



15
16
17
18
# File 'lib/imaginary.rb', line 15

def post(url, params)
  params[:basic_auth] ||= @auth
  self.class.post(url, params)
end