Skip to content

Commit 08cd5aa

Browse files
Alex TharpAlex Tharp
authored andcommitted
Merge pull request #1 from cortex-cms/singleton-pattern
Use Singleton Pattern
2 parents 1f5a646 + aa354fe commit 08cd5aa

File tree

3 files changed

+45
-31
lines changed

3 files changed

+45
-31
lines changed

cortex-snippets-client-ruby.gemspec

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
lib = File.expand_path('../lib', __FILE__)
33
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
44
require 'cortex/snippets/version'
5+
require 'cortex-client'
6+
require 'connection_pool'
57

68
Gem::Specification.new do |spec|
79
spec.name = 'cortex-snippets-client-ruby'
@@ -18,7 +20,7 @@ Gem::Specification.new do |spec|
1820
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
1921
spec.require_paths = ['lib']
2022

21-
spec.add_dependency 'cortex-client', '~> 0.4.3'
23+
spec.add_dependency 'cortex-client', '~> 0.4.5'
2224
spec.add_dependency 'connection_pool', '~> 2.2.0'
2325

2426
spec.add_development_dependency 'bundler', '~> 1.10'

lib/cortex/snippets/client.rb

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
module Cortex
22
module Snippets
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]) }
3+
module Client
4+
class << self
5+
def cortex_client
6+
if ENV['CORTEX_SNIPPET_ACCESS_TOKEN'].nil? || ENV['CORTEX_SNIPPET_ACCESS_TOKEN'].empty?
7+
@cortex_client ||= ConnectionPool::Wrapper.new(size: 5, timeout: 3) { Cortex::Client.new(access_token: ENV['CORTEX_SNIPPET_ACCESS_TOKEN']) }
8+
else
9+
@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']) }
10+
end
1111
end
12-
end
1312

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)
13+
def current_webpage(request)
14+
if defined?(Rails)
15+
Rails.cache.fetch("webpages/#{request_url(request)}", expires_in: 30.minutes) do
16+
cortex_client.webpages.get_feed(request_url(request))
17+
end
18+
else
19+
raise 'Your Web framework is not supported. Supported frameworks: Rails'
1820
end
19-
else
20-
raise 'Your Web framework is not supported. Supported frameworks: Rails'
2121
end
22-
end
2322

24-
private
23+
private
2524

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
25+
def request_url(request)
26+
# TODO: Should be grabbing request URL in a framework-agnostic manner, but this is fine for now
27+
request.original_url
28+
end
2929
end
3030
end
3131
end

lib/cortex/snippets/view_helpers.rb

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,52 @@
11
module Cortex
22
module Snippets
33
module ViewHelpers
4-
def snippet(id)
5-
content_tag(:snippet, current_webpage[:snippets].find { |snippet| snippet.name == id }, id: id)
4+
def snippet(options = {}, &block)
5+
snippet = webpage[:snippets].find { |snippet| snippet.name == options[:id] }
6+
7+
if snippet.empty?
8+
content_tag(:snippet, capture(&block), id: options[:id])
9+
else
10+
content_tag(:snippet, snippet, id: options[:id])
11+
end
612
end
713

814
def seo_title
9-
current_webpage[:seo_title]
15+
webpage[:seo_title]
1016
end
1117

1218
def seo_description
13-
current_webpage[:seo_description]
19+
webpage[:seo_description]
1420
end
1521

1622
def noindex
17-
current_webpage[:noindex]
23+
webpage[:noindex]
1824
end
1925

2026
def nofollow
21-
current_webpage[:nofollow]
27+
webpage[:nofollow]
2228
end
2329

2430
def noodp
25-
current_webpage[:noodp]
31+
webpage[:noodp]
2632
end
2733

2834
def nosnippet
29-
current_webpage[:nosnippet]
35+
webpage[:nosnippet]
3036
end
3137

3238
def noarchive
33-
current_webpage[:noarchive]
39+
webpage[:noarchive]
3440
end
3541

3642
def noimageindex
37-
current_webpage[:noimageindex]
43+
webpage[:noimageindex]
44+
end
45+
46+
private
47+
48+
def webpage
49+
Client::current_webpage(request)
3850
end
3951
end
4052
end

0 commit comments

Comments
 (0)