Class: Flickrpu::Base

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(photo_class_name = "Photo", num_photos = 10, opts = {}, &klass_opts) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/flickrpu/base.rb', line 10

def initialize(photo_class_name = "Photo", num_photos = 10, opts = {}, &klass_opts)
  
  self.photo_class_name = photo_class_name
  self.num_photos = num_photos
  self.opts = opts
  self.klass_opts = klass_opts
  self.config = {
    'key' => opts[:key],
    'secret' => opts[:secret]
  }
  self
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/flickrpu/base.rb', line 8

def config
  @config
end

#flickrObject

Returns the value of attribute flickr.



8
9
10
# File 'lib/flickrpu/base.rb', line 8

def flickr
  @flickr
end

#klass_optsObject

Returns the value of attribute klass_opts.



8
9
10
# File 'lib/flickrpu/base.rb', line 8

def klass_opts
  @klass_opts
end

#num_photosObject

Returns the value of attribute num_photos.



8
9
10
# File 'lib/flickrpu/base.rb', line 8

def num_photos
  @num_photos
end

#optsObject

Returns the value of attribute opts.



8
9
10
# File 'lib/flickrpu/base.rb', line 8

def opts
  @opts
end

#photo_class_nameObject

Returns the value of attribute photo_class_name.



8
9
10
# File 'lib/flickrpu/base.rb', line 8

def photo_class_name
  @photo_class_name
end

Class Method Details

.copy_to_tempfile(dir, filename) ⇒ Object



70
71
72
73
74
75
# File 'lib/flickrpu/base.rb', line 70

def self.copy_to_tempfile(dir, filename)
  tmp_dir = File.join('.', 'tmp')
  Dir.mkdir(tmp_dir) if not File.directory?(tmp_dir)
  File.copy(File.join(dir, filename), File.join(tmp_dir, filename), verbose = true)
  temp_file
end

.from_file(dir, filename) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/flickrpu/base.rb', line 58

def self.from_file(dir, filename)
  temp_file = copy_to_tempfile(dir, filename)
  opt_obj = OpenStruct.new
  opt_obj.url = temp_file
  self.save(opt_obj)
  File.safe_unlink(temp_file)
end

Instance Method Details

#from_file(dir, filename) ⇒ Object



66
67
68
# File 'lib/flickrpu/base.rb', line 66

def from_file(dir, filename)
  self.class.from_file(dir, filename)
end

#klassObject



23
24
25
# File 'lib/flickrpu/base.rb', line 23

def klass
  Kernel.const_get(self.photo_class_name.capitalize)
end

#save(flickr_photo) ⇒ Object



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
# File 'lib/flickrpu/base.rb', line 27

def save(flickr_photo)
  flickr_url             = flickr_photo.url
  puts "Saving #{flickr_url}"
  
  photo                  = self.klass.new
  self.klass_opts.call(photo) if self.klass_opts
  filename               = rand(36**12).to_s(36)+'.jpg'
  amazon_file            = {
    :filename            => filename,
    :type                => 'image/jpeg',
    :tempfile            => open(flickr_url)
  }
  photo.amazon_file      = amazon_file

  if photo.valid?
    photo.save
  else
    puts "Couldn't save photo #{photo.inspect} :("
    photo.errors.each do |e|
      puts e
    end
  end
  1
rescue NoMethodError => e
  puts e.to_s
  0
rescue TypeError => e
  puts "Couldn't retrieve Flickr url: #{e.to_s}"
  exit
end

#searchObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/flickrpu/base.rb', line 77

def search
  puts self.config.inspect
  self.flickr = ::Flickr.new(self.config)
  this_photo = 0
  flickr_photos = self.flickr.photos.get_recent({:per_page=>self.num_photos})
  flickr_photos.each do |photo|
    return if this_photo >= self.num_photos
    this_photo += self.save(photo)
  end
end