Class: ImgShark

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

Defined Under Namespace

Classes: Image

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ImgShark

Returns a new instance of ImgShark.



16
17
18
19
20
21
22
23
24
25
# File 'lib/imgshark.rb', line 16

def initialize(opts = {})
  AWS::S3::Base.establish_connection!(
    :access_key_id     => opts.delete(:amazon_access_key_id) || ENV['AMAZON_ACCESS_KEY_ID'], 
    :secret_access_key => opts.delete(:amazon_secret_access_key) || ENV['AMAZON_SECRET_ACCESS_KEY']
  )
  redis_url = opts.delete(:redis_url) || ENV['REDISTOGO_URL']
  @redis = redis_url ? Redis.new(redis_url) : Redis.new()
  @json = Yajl::Parser.new
  @bucket = opts.delete(:amazon_bucket) || ENV['AMAZON_BUCKET']
end

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket.



14
15
16
# File 'lib/imgshark.rb', line 14

def bucket
  @bucket
end

#jsonObject (readonly)

Returns the value of attribute json.



13
14
15
# File 'lib/imgshark.rb', line 13

def json
  @json
end

#redisObject (readonly)

Returns the value of attribute redis.



12
13
14
# File 'lib/imgshark.rb', line 12

def redis
  @redis
end

Instance Method Details

#find(key, response_type = {object: true}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/imgshark.rb', line 41

def find(key, response_type = {object: true})
  response = redis.get(key)
  if response_type[:object]
    ImgShark::Image.new(self, json.parse(response)) if response && response.kind_of?(String)
  elsif response_type[:json]
    json.parse response
  else
    response
  end
end

#get_url(url, height, width) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/imgshark.rb', line 27

def get_url(url, height, width)
  @image = ImgShark::Image.new(self, url: url, h: height, w: width)
  if @image.exists?
    find(@image.key).amazon_url
  else
    if !@image.on_amazon?
      @image.resize
      @image.upload
    end
    @image.save
    @image.amazon_url
  end
end