twm-cms-bindings 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README ADDED
@@ -0,0 +1,7 @@
1
+ TWM CMS v3 Bindings
2
+ ~~~~~~~~~~~~~~~~~~~
3
+
4
+ This is the Ruby binding library to communicate with the TWM CMS v3 platform.
5
+
6
+ It is packaged as a gem
7
+
data/lib/CMSBinding.rb ADDED
@@ -0,0 +1,207 @@
1
+ # CMSBinding.rb
2
+
3
+ require 'net/http'
4
+ require 'rexml/document'
5
+ require 'rexml/xpath'
6
+ require 'rubygems'
7
+ require 'memcache'
8
+
9
+ module CMSBinding
10
+
11
+ class Base
12
+ def get_text( element )
13
+ return '' unless element.kind_of? REXML::Element
14
+ return_text = ''
15
+
16
+ if( element.has_elements? )
17
+ formatter = REXML::Formatters::Default.new(false)
18
+ REXML::XPath.each( element, "*") {|subElement|
19
+ tmp = ''
20
+ formatter.write(subElement, tmp)
21
+ return_text += tmp
22
+ }
23
+ elsif ( element.has_text? )
24
+ element.texts.each {|text|
25
+ return_text += text.value
26
+ }
27
+ end
28
+
29
+ return_text
30
+ end
31
+ end
32
+
33
+ class Category < Base
34
+ attr_reader :parent, :id, :name, :description, :category_weblink,
35
+ :article_weblink, :cdn_image_url, :cdn_thumbnail_url,
36
+ :sub_categories, :articles
37
+
38
+ def initialize (topCategory, source, partial)
39
+ @source = source;
40
+ @articles = []
41
+ @sub_categories = []
42
+ @partial = partial
43
+
44
+ @id = topCategory.attributes['id'].to_str
45
+ @name = get_text( REXML::XPath.first( topCategory, "name" ) )
46
+ @description = get_text( REXML::XPath.first( topCategory, "description" ) )
47
+ @category_weblink = get_text( REXML::XPath.first( topCategory, "category_weblink" ) )
48
+ @article_weblink = get_text( REXML::XPath.first( topCategory, "article_weblink" ) )
49
+
50
+ if ( partial == false)
51
+ @cdn_image_url = get_text( REXML::XPath.first( topCategory, "cdn_image_url" ) )
52
+
53
+ @cdn_thumbnail_url = get_text( REXML::XPath.first( topCategory, "cdn_thumbnail_url" ) )
54
+ REXML::XPath.each( topCategory, "subcategories/category") {|subCategory|
55
+ @sub_categories << CMSBinding::Category.new( subCategory, source, true )
56
+ }
57
+
58
+ REXML::XPath.each( topCategory, "articles/article" ) {|article|
59
+ @articles << CMSBinding::Article.new( article, source, true )
60
+ }
61
+ end
62
+ end
63
+ end
64
+
65
+ class Queue
66
+ attr_reader :queue_name, :queue_type, :articles
67
+
68
+ def initialize (queue_dom, source, queue_name, queue_type)
69
+ @source = source
70
+ @queue_name = queue_name
71
+ @queue_type = queue_type
72
+ @articles = []
73
+
74
+ REXML::XPath.each(queue_dom, "article") { |article|
75
+ @articles << CMSBinding::Article.new( article, source, false )
76
+ }
77
+ end
78
+ end
79
+
80
+ class Article < Base
81
+ attr_reader :categoryID, :id, :headline, :small_intro, :large_intro, :date,
82
+ :contents, :keywords, :attachments, :fields
83
+
84
+ def initialize (article, source, partial)
85
+ @source = source
86
+ @attachments = []
87
+ @fields = {}
88
+ @partial = partial
89
+
90
+ @id = article.attributes['id'].to_str
91
+ @headline = get_text( REXML::XPath.first( article, "headline" ) )
92
+
93
+ if( partial == false )
94
+ @categoryID = REXML::XPath.first( article, "category" ).attributes['id'].to_str
95
+ @small_intro = get_text( REXML::XPath.first( article, "smallintro" ) )
96
+ @large_intro = get_text( REXML::XPath.first( article, "largeintro" ) )
97
+ @contents = get_text( REXML::XPath.first( article, "body" ) )
98
+ @keywords = get_text( REXML::XPath.first( article, "keywords" ) ).split(',')
99
+ @date = get_text( REXML::XPath.first( article, "date" ) )
100
+
101
+ REXML::XPath.each( article, "field") {|field|
102
+ key = field.attributes['name'].to_s
103
+ value = get_text( REXML::XPath.first( field, "."))
104
+ @fields[key.strip] = value.strip
105
+ }
106
+ REXML::XPath.each( article, "attachments/attachment" ) {|attachment|
107
+ @attachments << CMSBinding::ArticleAttachment.new(attachment, @source)
108
+ }
109
+ end
110
+ end
111
+ end
112
+
113
+ class ArticleAttachment
114
+ attr_reader :id, :mime_type, :filename
115
+
116
+ def initialize(attachment_dom, source)
117
+ @source = source
118
+
119
+ @id = attachment_dom.attributes['id'].to_str
120
+ @mime_type = attachment_dom.attributes['mimetype'].to_str
121
+ @filename = attachment_dom.attributes['filename'].to_str
122
+ end
123
+ end
124
+
125
+ class CMSSource
126
+ CMS_AP = "/cms";
127
+
128
+ def initialize(config)
129
+ if config[:site].nil?
130
+ raise ArgumentError, ":site required"
131
+ else
132
+ @site = config[:site]
133
+ end
134
+
135
+ @server = config[:server] || 'cms.hostingoperationscentre.com'
136
+ @port = config[:port] || '80'
137
+
138
+ if config[:cache].nil?
139
+ @cache_server = config[:cache_server] || nil
140
+ unless @cache_server.nil?
141
+ @mcache = MemCache.new(@cache_server)
142
+ @cache_timeout = config[:cache_timeout] || 3600
143
+ end
144
+ else
145
+ @mcache = config[:cache]
146
+ @cache_timeout = config[:cache_timeout] || 3600
147
+ end
148
+ end
149
+
150
+ def category(id)
151
+ xml = get_response( "#{base_url}xml/category/id/#{id}" )
152
+ document = REXML::Document.new(xml);
153
+ category = REXML::XPath.first( document, '/category' )
154
+ CMSBinding::Category.new(category, self, false)
155
+ end
156
+
157
+ def article(id)
158
+ xml = get_response( "#{base_url}xml/article/id/#{id}/full" )
159
+ document = REXML::Document.new(xml)
160
+ first_article = REXML::XPath.first( document, "articles/article")
161
+ CMSBinding::Article.new(first_article, self, false)
162
+ end
163
+
164
+ def queue(queue_name, queue_type)
165
+ xml = get_response( "#{base_url}xml/article/#{queue_type}/#{queue_name}/all/full")
166
+ document = REXML::Document.new(xml)
167
+ articles = REXML::XPath.first( document, "articles")
168
+ CMSBinding::Queue.new(articles, self, queue_name, queue_type)
169
+ end
170
+
171
+ def article_attachment_link(attachment)
172
+ if attachment.kind_of? CMSBinding::ArticleAttachment
173
+ attachment_id = attachment.id
174
+ else
175
+ attachment_id = attachment.to_str
176
+ end
177
+
178
+ "#{base_url}binary/article/attachment/#{attachment_id}/normal"
179
+ end
180
+
181
+ private
182
+
183
+ def base_url
184
+ "http://#{@server}:#{@port}#{CMS_AP}/content/#{@site}/"
185
+ end
186
+
187
+ def get_response(strURL)
188
+ content = @mcache[strURL] unless @cache.nil?
189
+ if content.nil?
190
+ url = URI.parse(strURL)
191
+ req = Net::HTTP::Get.new(url.path)
192
+ content = Net::HTTP.start(url.host, url.port) {|http|
193
+ res = http.request(req)
194
+ if(! res.instance_of?(Net::HTTPOK) )
195
+ raise "Error communicating with CMS Instance '#{res.message}'"
196
+ else
197
+ res.body
198
+ end
199
+ }
200
+ unless @cache.nil?
201
+ @mcache.set(strURL, content, @cache_timeout)
202
+ end
203
+ end
204
+ content
205
+ end
206
+ end
207
+ end
@@ -0,0 +1,29 @@
1
+ # CMSBindingTest.rb
2
+ # June 29, 2007
3
+ #
4
+
5
+
6
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
7
+
8
+ require 'test/unit'
9
+ require 'CMSBinding'
10
+ require 'rubygems'
11
+ require 'memcache'
12
+
13
+ class TestCMSBindingTest < Test::Unit::TestCase
14
+
15
+ def setup
16
+ @source = CMSBinding::CMSSource.new({:site => '11N-0'})#, :cache_server => '127.0.0.1'})
17
+ end
18
+
19
+ def teardown
20
+ end
21
+
22
+ def test_simple_article
23
+ queue = @source.queue 'MemberProfiles', 'historyqueue'
24
+ article = queue.articles.each{|article|
25
+ puts "#{article.headline} - #{article.fields['Phone']}"
26
+ puts "LOGO" unless article.attachments.empty?
27
+ }
28
+ end
29
+ end
@@ -0,0 +1,56 @@
1
+ # CMSBindingTest.rb
2
+ # June 29, 2007
3
+ #
4
+
5
+
6
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
7
+
8
+ require 'test/unit'
9
+ require 'CMSBinding'
10
+
11
+ class TestCMSBindingTest < Test::Unit::TestCase
12
+
13
+ def setup
14
+ @cms = CMSBinding::CMSSource.new({:site => '0'})
15
+ end
16
+
17
+ def teardown
18
+ end
19
+
20
+ def test_simple_article
21
+ article = @cms.article 'AU0-1'
22
+ puts article.inspect
23
+ assert_not_nil article.contents, 'Contents should not be nil'
24
+ assert_match "Test Article - DO NOT MODIFY OR DELETE", article.headline, 'Wrong headline'
25
+ assert_match "Test article for Nagios monitoring", article.contents, 'Wrong contents'
26
+ end
27
+
28
+ def test_simple_category
29
+ category = @cms.category 'AU0-0'
30
+ assert_equal("nagios", category.name)
31
+
32
+ article = category.articles.first
33
+ assert_match "Test Article - DO NOT MODIFY OR DELETE", article.headline, 'Wrong headline'
34
+ assert_nil article.contents, 'Article should be a partial'
35
+
36
+ article = @cms.article article.id
37
+ assert_match "Test Article - DO NOT MODIFY OR DELETE", article.headline, 'Wrong headline'
38
+ assert_match "Test article for Nagios monitoring", article.contents, 'Wrong contents'
39
+ end
40
+
41
+ def test_simple_queue
42
+ queue = @cms.queue 'nagios', 'historyqueue'
43
+ article = queue.articles.first
44
+ assert_match "Test Article - DO NOT MODIFY OR DELETE", article.headline, 'Wrong headline'
45
+ assert_match "Test article for Nagios monitoring", article.contents, 'Wrong contents'
46
+ end
47
+
48
+ def test_with_cache
49
+ cms = CMSBinding::CMSSource.new({:site => '0', :cache_server => '127.0.0.1'})
50
+ queue = cms.queue 'nagios', 'historyqueue'
51
+ article = queue.articles.first
52
+ assert_match "Test Article - DO NOT MODIFY OR DELETE", article.headline, 'Wrong headline'
53
+ assert_match "Test article for Nagios monitoring", article.contents, 'Wrong contents'
54
+ end
55
+
56
+ end
data/test/cachetest.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'memcache'
3
+
4
+ memcache = MemCache.new('127.0.0.1')
5
+ test = memcache.set("test", "data", 3600)
6
+ data = memcache.get("test")
7
+ puts data
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twm-cms-bindings
3
+ version: !ruby/object:Gem::Version
4
+ hash: 7
5
+ prerelease: false
6
+ segments:
7
+ - 3
8
+ - 0
9
+ - 0
10
+ version: 3.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Three Wise Men
14
+ autorequire: memcache-client
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-26 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email: info @nospam@ threewisemen.ca
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - README
30
+ files:
31
+ - lib/CMSBinding.rb
32
+ - test/cachetest.rb
33
+ - test/CMSBindingDITATest.rb
34
+ - test/CMSBindingTest.rb
35
+ - README
36
+ has_rdoc: true
37
+ homepage:
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ hash: 3
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.7
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Binding library for TWM CMS v3 platform
70
+ test_files:
71
+ - test/cachetest.rb
72
+ - test/CMSBindingDITATest.rb
73
+ - test/CMSBindingTest.rb