Class: Flickr::Store
- Inherits:
-
Object
- Object
- Flickr::Store
- Defined in:
- lib/flickr-store.rb,
lib/flickr-store/version.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Class Method Summary collapse
Instance Method Summary collapse
- #fetch(name, outfile) ⇒ Object
-
#initialize(key, secret, access_token, access_secret) ⇒ Store
constructor
A new instance of Store.
- #upload(file) ⇒ Object
Constructor Details
#initialize(key, secret, access_token, access_secret) ⇒ Store
Returns a new instance of Store.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/flickr-store.rb', line 40 def initialize(key, secret, access_token, access_secret) FlickRaw.api_key = key FlickRaw.shared_secret = secret flickr.access_token = access_token flickr.access_secret = access_secret if File.exists?('./.flickr-store') begin @dict = Marshal.load File.read('./.flickr-store') rescue FileUtils.touch('./.flickr-store') @dict = {} end else FileUtils.touch('./.flickr-store') @dict = {} end end |
Class Method Details
.authenticate(key, secret) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/flickr-store.rb', line 16 def self.authenticate(key, secret) FlickRaw.api_key = key FlickRaw.shared_secret = secret token = flickr.get_request_token auth_url = flickr.(token['oauth_token'], perms: 'delete') `open #{auth_url}` puts "Visit this URL in your browser: #{auth_url}" puts "Paste the number given after logging in:" verify = gets.strip begin flickr.get_access_token(token['oauth_token'], token['oauth_token_secret'], verify) login = flickr.test.login puts "Authentication complete! Logged in as #{login.username}." rescue FlickRaw::FailedRepsonse => e puts "Authentication failed: #{e.msg}" end self.new(key, secret, flickr.access_token, flickr.access_secret) end |
Instance Method Details
#fetch(name, outfile) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/flickr-store.rb', line 79 def fetch(name, outfile) path = File.realpath(name) id = @dict[path] sizes = flickr.photos.getSizes(photo_id: id) image = sizes.select { |s| s['label'].downcase == 'original' }.first url = image['source'] file = Tempfile.new(SecureRandom.hex) file.write open(url).read file.flush PNG.decode(file, outfile) file.close! file.unlink end |
#upload(file) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/flickr-store.rb', line 65 def upload(file) original_file = File.new(file, 'r') outfile = Tempfile.new(SecureRandom.hex) encoded_file = PNG.encode(original_file, outfile) id = flickr.upload_photo encoded_file, title: File.basename(original_file.path) outfile.close! outfile.unlink @dict[File.realpath(original_file.path)] = id update_dict! end |