Class: Flickr::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/flickr-store.rb,
lib/flickr-store/version.rb

Constant Summary collapse

DICT_FILE =
Dir.home + '/.flickr-store'
VERSION =
"0.0.3"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, secret, access_token, access_secret) ⇒ Store

Returns a new instance of Store.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/flickr-store.rb', line 42

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?(DICT_FILE)
    begin
      @dict = Marshal.load File.read(DICT_FILE)
    rescue
      FileUtils.touch(DICT_FILE)
      @dict = {}
    end
  else
    FileUtils.touch(DICT_FILE)
    @dict = {}
  end
end

Class Method Details

.authenticate(key, secret) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/flickr-store.rb', line 18

def self.authenticate(key, secret)
  FlickRaw.api_key = key
  FlickRaw.shared_secret = secret

  token = flickr.get_request_token
  auth_url = flickr.get_authorize_url(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)
     = flickr.test.

    puts "Authentication complete! Logged in as #{.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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/flickr-store.rb', line 81

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



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/flickr-store.rb', line 67

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