Sha256: 8489a53eed03519bc90cead166511b568192793d5830c1fbf6567315bdf4de0b

Contents?: true

Size: 1.47 KB

Versions: 5

Compression:

Stored size: 1.47 KB

Contents

require 'cortex/snippets/client/helper'
require 'cortex/snippets/client/railtie' if defined?(Rails)
require 'cortex-client'
require 'connection_pool'
require 'addressable/template'

module Cortex
  module Snippets
    module Client
      class << self
        def cortex_client
          if ENV['CORTEX_SNIPPET_ACCESS_TOKEN'].nil? || ENV['CORTEX_SNIPPET_ACCESS_TOKEN'].empty?
            @cortex_client ||= ConnectionPool::Wrapper.new(size: 5, timeout: 3) { Cortex::Client.new(key: ENV['CORTEX_SNIPPET_KEY'], secret: ENV['CORTEX_SNIPPET_SECRET'], base_url: ENV['CORTEX_SNIPPET_BASE_URL'], scopes: ENV['CORTEX_SNIPPET_SCOPES']) }
          else
            @cortex_client ||= ConnectionPool::Wrapper.new(size: 5, timeout: 3) { Cortex::Client.new(access_token: ENV['CORTEX_SNIPPET_ACCESS_TOKEN']) }
          end
        end

        def current_webpage(request)
          if defined?(Rails)
            sanitized_url = sanitized_webpage_url(request.original_url)
            Rails.cache.fetch("webpages/#{sanitized_url}", race_condition_ttl: 10) do
              cortex_client.webpages.get_feed(sanitized_url).contents
            end
          else
            raise 'Your Web framework is not supported. Supported frameworks: Rails'
          end
        end

        def sanitized_webpage_url(url)
          uri = Addressable::URI.parse(url)
          path = uri.path == '/' ? uri.path : uri.path.chomp('/')
          "#{uri.scheme}://#{uri.authority}#{path}"
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
cortex-snippets-client-0.8.1 lib/cortex/snippets/client.rb
cortex-snippets-client-0.8.0 lib/cortex/snippets/client.rb
cortex-snippets-client-0.7.0 lib/cortex/snippets/client.rb
cortex-snippets-client-0.6.0 lib/cortex/snippets/client.rb
cortex-snippets-client-0.5.3 lib/cortex/snippets/client.rb