Sha256: 5b2ed182260f7f1761627362f153b449bde465e976b79828d31fbdef9a5f03f5

Contents?: true

Size: 1.99 KB

Versions: 4

Compression:

Stored size: 1.99 KB

Contents

module Corkboard
  class PostsController < Corkboard::ApplicationController
    def create
      send(:"create_for_#{provider}")
    end

    private

      def provider
        @provider ||= params[:provider]
      end

      def create_for_instagram
        instagram_challenge || instagram_callback || instagram_refresh
      end

      def instagram_challenge
        if request.get? && params['hub.challenge'].present?
          render(:text => params['hub.challenge'])
          return true
        end

        false
      end

      def instagram_callback
        if request.post?
          # TODO: cache "friends"
          # TODO: move to Subscription model
          client  = Corkboard.client(:instagram, { :access_token => instagram_authorization.token })
          friends = client.user_follows.map(&:username)

          ::Instagram.process_subscription(params['_json'].to_json) do |handler|
            handler.on_tag_changed do |tag, change|
              # Get "my" recent feed items... includes updates from my "friends".
              response = client.recent

              # Filter those based on configured "interests".
              relevant = response.data.select do |entry|
                friendly    = friends.include?(entry.user.username)
                interesting = (entry.tags.map(&:intern) & Corkboard.settings(:instagram)[:interests]).present?
                friendly && interesting
              end

              Corkboard.publish!(relevant)
            end
          end

          head :ok and return true
        end

        false
      end

      def instagram_refresh
        if params[:refresh]
          client = Corkboard.client(:instagram, { :access_token => instagram_authorization.token })
          response = client.recent

          Corkboard.publish!(response)

          head :ok and return true
        end

        false
      end

      def instagram_authorization
        @instagram_authorization ||= Corkboard::Authorization.find_by_provider('instagram')
      end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
corkboard-0.1.3 app/controllers/corkboard/posts_controller.rb
corkboard-0.1.2 app/controllers/corkboard/posts_controller.rb
corkboard-0.1.1 app/controllers/corkboard/posts_controller.rb
corkboard-0.1.0 app/controllers/corkboard/posts_controller.rb