Class: Imaginary::Client
- Inherits:
-
Object
- Object
- Imaginary::Client
- Includes:
- HTTMultiParty
- Defined in:
- lib/imaginary.rb
Instance Method Summary collapse
- #add_image_from_file(file, name = nil) ⇒ Object
- #add_image_from_url(url, name = nil) ⇒ Object
- #image_url(name, options = nil) ⇒ Object
-
#initialize(url, options = {}) ⇒ Client
constructor
A new instance of Client.
- #post(url, params) ⇒ Object
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, = {}) @base_url = url @auth = { username: [:username], password: [:password] } @bucket = [:bucket] @secret = [: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, = nil) parts = ['x', @bucket] if if .is_a?(String) parts << elsif .is_a?(Array) parts += 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 |