Skip to content

Commit 1f5a646

Browse files
committed
Snippets client initial implementation
1 parent f9d0dcb commit 1f5a646

File tree

13 files changed

+99
-43
lines changed

13 files changed

+99
-43
lines changed

bin/console

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env ruby
22

33
require 'bundler/setup'
4-
require 'cortex/snippets/client'
4+
require 'cortex/snippets/snippets-client'
55

66
# You can add fixtures and/or initialization code here to make experimenting
77
# with your gem easier. You can also use a different console, if you like.

cortex-snippets-client-ruby.gemspec

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# coding: utf-8
22
lib = File.expand_path('../lib', __FILE__)
33
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4-
require 'cortex/snippets/client/version'
4+
require 'cortex/snippets/version'
55

66
Gem::Specification.new do |spec|
77
spec.name = 'cortex-snippets-client-ruby'
8-
spec.version = Cortex::Snippets::Client::VERSION
8+
spec.version = Cortex::Snippets::VERSION
99
spec.authors = ['CB Content Enablement']
1010
spec.email = ['[email protected]']
1111
spec.license = 'Apache-2.0'
@@ -18,6 +18,9 @@ Gem::Specification.new do |spec|
1818
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
1919
spec.require_paths = ['lib']
2020

21+
spec.add_dependency 'cortex-client', '~> 0.4.3'
22+
spec.add_dependency 'connection_pool', '~> 2.2.0'
23+
2124
spec.add_development_dependency 'bundler', '~> 1.10'
2225
spec.add_development_dependency 'rake', '~> 10.0'
2326
spec.add_development_dependency 'rspec'

lib/cortex/snippets-client.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require 'cortex/snippets/version'
2+
require 'cortex/snippets/client'
3+
require 'cortex/snippets/view_helpers'
4+
require 'cortex/snippets/railtie' if defined?(Rails)

lib/cortex/snippets/client.rb

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
1-
require 'cortex/snippets/client/version'
2-
require 'cortex/snippets/client/view_helpers'
3-
require 'cortex/snippets/client/railtie' if defined?(Rails)
4-
51
module Cortex
62
module Snippets
7-
module Client
3+
class Client
4+
attr_accessor :current_webpage
5+
6+
def initialize(hasharg)
7+
if hasharg.has_key? :access_token
8+
@cortex_client = ConnectionPool::Wrapper.new(size: 5, timeout: 3) { Cortex::Client.new(access_token: hasharg[:access_token]) }
9+
else
10+
@cortex_client = ConnectionPool::Wrapper.new(size: 5, timeout: 3) { Cortex::Client.new(key: hasharg[:key], secret: hasharg[:secret], base_url: hasharg[:base_url]) }
11+
end
12+
end
13+
14+
def current_webpage
15+
if defined?(Rails)
16+
Rails.cache.fetch("webpages/#{request_url}", expires_in: 30.minutes) do
17+
@cortex_client.webpages.get_feed(request_url)
18+
end
19+
else
20+
raise 'Your Web framework is not supported. Supported frameworks: Rails'
21+
end
22+
end
23+
24+
private
825

26+
def request_url
27+
# TODO: Should be turbo-easy to grab request URL from Rack, but this is fine for now
28+
request.original_url
29+
end
930
end
1031
end
1132
end

lib/cortex/snippets/client/railtie.rb

Lines changed: 0 additions & 11 deletions
This file was deleted.

lib/cortex/snippets/client/version.rb

Lines changed: 0 additions & 7 deletions
This file was deleted.

lib/cortex/snippets/client/view_helpers.rb

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/cortex/snippets/railtie.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Cortex
2+
module Snippets
3+
class Railtie < Rails::Railtie
4+
initializer 'cortex.snippets.view_helpers' do
5+
ActiveSupport.on_load( :action_view ){ include ViewHelpers }
6+
end
7+
end
8+
end
9+
end

lib/cortex/snippets/version.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Cortex
2+
module Snippets
3+
VERSION = '0.1.0'
4+
end
5+
end

lib/cortex/snippets/view_helpers.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module Cortex
2+
module Snippets
3+
module ViewHelpers
4+
def snippet(id)
5+
content_tag(:snippet, current_webpage[:snippets].find { |snippet| snippet.name == id }, id: id)
6+
end
7+
8+
def seo_title
9+
current_webpage[:seo_title]
10+
end
11+
12+
def seo_description
13+
current_webpage[:seo_description]
14+
end
15+
16+
def noindex
17+
current_webpage[:noindex]
18+
end
19+
20+
def nofollow
21+
current_webpage[:nofollow]
22+
end
23+
24+
def noodp
25+
current_webpage[:noodp]
26+
end
27+
28+
def nosnippet
29+
current_webpage[:nosnippet]
30+
end
31+
32+
def noarchive
33+
current_webpage[:noarchive]
34+
end
35+
36+
def noimageindex
37+
current_webpage[:noimageindex]
38+
end
39+
end
40+
end
41+
end

0 commit comments

Comments
 (0)