Module: Cortex::Snippets::Client

Defined in:
lib/cortex/snippets/client.rb,
lib/cortex/snippets/client/helper.rb,
lib/cortex/snippets/client/railtie.rb,
lib/cortex/snippets/client/version.rb

Defined Under Namespace

Modules: Helper Classes: Railtie

Constant Summary collapse

VERSION =
'0.5.0'

Class Method Summary collapse

Class Method Details

.cortex_clientObject



11
12
13
14
15
16
17
# File 'lib/cortex/snippets/client.rb', line 11

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

.current_webpage(request) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/cortex/snippets/client.rb', line 19

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

.request_url(request) ⇒ Object



29
30
31
32
33
34
# File 'lib/cortex/snippets/client.rb', line 29

def request_url(request)
  # TODO: Should be grabbing request URL in a framework-agnostic manner, but this is fine for now
  uri = Addressable::URI.parse(request.original_url)
  path = uri.path == "/" ? uri.path : uri.path.chomp("/")
  "#{uri.scheme}://#{uri.authority}#{path}"
end