1. Koen Van der Auwera
  2. amanda

Commits

Koen Van der Auwera  committed 587a716

Add Dropbox with authentication

  • Participants
  • Parent commits 85386f0
  • Branches master

Comments (0)

Files changed (4)

File Gemfile

View file
  • Ignore whitespace
 gem 'mab', '0.0.2'
 gem 'thin'
 gem 'foreman'
-gem 'redis', '3.0.3'
+gem 'redis', '3.0.3'
+gem 'dropbox-sdk', '1.5.1'

File Gemfile.lock

View file
  • Ignore whitespace
       mab
       rack (>= 1.0)
     daemons (1.1.9)
+    dropbox-sdk (1.5.1)
+      json
     eventmachine (1.0.3)
     foreman (0.62.0)
       thor (>= 0.13.6)
+    json (1.7.7)
     mab (0.0.2)
     rack (1.5.2)
     redis (3.0.3)
 DEPENDENCIES
   bundler
   camping (= 2.1.523)
+  dropbox-sdk
   foreman
   mab (= 0.0.2)
   redis (= 3.0.3)

File amanda.rb

View file
  • Ignore whitespace
 require_relative 'amanda/post'
 require_relative 'amanda/store'
 
-require 'camping'
 require 'rack'
+require 'camping'
+require 'camping/session'
+require 'dropbox_sdk'
 
 Camping.goes :Amanda
 
 STORE = Amanda::Store.new("amanda.yml")
 
+module Amanda
+  set :secret, "Some super secret, even more. No. MORE!"
+  include Camping::Session
+end
+
 module Amanda::Controllers
   class Index < R '/'
     def get
       render :archive
     end
   end
+
+  class Authorize < R '/authorize'
+    def get
+      session = DropboxSession.new(STORE.dropbox_settings.fetch("app_key"), STORE.dropbox_settings.fetch("app_secret"))
+      session.get_request_token
+      @state["dropbox"] = session
+      redirect session.get_authorize_url(URL("/authorized").to_s)
+    end
+  end
+
+  class Authorized < R '/authorized'
+    def get
+      if session = @state["dropbox"]
+        session.get_access_token
+        STORE.dropbox_session(session)
+        redirect "/"
+      else
+        "TUUT"
+      end
+    end
+  end
+
 end
 
 module Amanda::Views
   end
 
   def index
-    STORE.redis.keys "*"
+    STORE.keys "*"
   end
 
   def archive
     ul do
-      STORE.redis.keys("post:*").map {|p| li(Amanda::Post.from_json(STORE.redis.get(p)).title)}
+      STORE.posts.map {|p| li(p.title)}
     end
   end
 end

File amanda/store.rb

View file
  • Ignore whitespace
 require 'yaml'
 require 'json'
 require 'redis'
+require 'dropbox_sdk'
 
 module Amanda
 
     POSTS_KEY = "posts"
     POSTS_LAST_KEY = "posts:last"
     POSTS_ALL_KEY = "posts:all"
+    DROPBOX = "dropbox"
 
     attr_accessor :config_file
 
       @path ||= config.fetch("posts_path")
     end
 
+    def dropbox_settings
+      config.fetch("dropbox")
+    end
+
+    def dropbox_session(session=nil)
+      if session
+        redis.set(DROPBOX, session.serialize)
+      end
+      DropboxSession.deserialize(redis.get(DROPBOX))
+    end
+
     def redis
       @redis ||= ::Redis.new(host: config.fetch("redis").fetch("host", "localhost"),
                              port: config.fetch("redis").fetch("port", 6379))
     end
 
     def store_posts_in_redis
-      read_posts_from_disk.each do |post|
-        redis.set "#{POST_KEY_PREFIX}#{post.id}", post.to_json
-        redis.sadd POSTS_KEY, "#{POST_KEY_PREFIX}#{post.id}"
-        redis.set POSTS_LAST_KEY, "#{POST_KEY_PREFIX}#{post.id}"
-        redis.rpush POSTS_ALL_KEY, "#{POST_KEY_PREFIX}#{post.id}"
+      rposts = read_posts_from_disk
+      if rposts.any?
+        redis.flushdb
+        rposts.each do |post|
+          redis.set "#{POST_KEY_PREFIX}#{post.id}", post.to_json
+          redis.sadd POSTS_KEY, "#{POST_KEY_PREFIX}#{post.id}"
+          redis.set POSTS_LAST_KEY, "#{POST_KEY_PREFIX}#{post.id}"
+          redis.rpush POSTS_ALL_KEY, "#{POST_KEY_PREFIX}#{post.id}"
+        end
       end
     end