infopark_cloud_connector 6.8.0.beta.200.621.4c8e1b0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,78 @@
1
+ require 'json'
2
+ require 'restclient'
3
+
4
+ module RailsConnector
5
+
6
+ # This class provides a basic implementation for accessing a elasticsearch server.
7
+ # It can be activated by making it the superclass of SearchRequest.
8
+ # It should be customized by subclassing.
9
+ class ElasticsearchRequest
10
+ # Takes +query_string+ and +options+ for accessing Elasticsearch.
11
+ #
12
+ # +options+ is a hash and may include:
13
+ #
14
+ # <tt>:limit</tt>:: The maximum number of hits
15
+ # <tt>:offset</tt>:: The search offset
16
+ # <tt>:url</tt>:: A non-default Elasticsearch index URL
17
+ def initialize(query_string, options = {})
18
+ @query_string = query_string
19
+ @options = Configuration.search_options.merge(options)
20
+ end
21
+
22
+ # Accesses Elasticsearch using #query and fetches search hits.
23
+ def fetch_hits
24
+ the_request = request_body
25
+ (the_request[:fields] ||= []) << :obj_id
26
+ the_request[:fields].uniq!
27
+ the_request[:from] = @options[:offset] if @options[:offset]
28
+ the_request[:size] = @options[:limit] if @options[:limit]
29
+
30
+ hits = JSON.parse(
31
+ RestClient::Request.execute(:method => :get, :url => url, :payload => the_request.to_json)
32
+ )['hits']
33
+
34
+ result = SES::SearchResult.new(hits['total'])
35
+ hits['hits'].each do |hit|
36
+ result << SES::Hit.new(hit['fields']['obj_id'], hit['_score'] / hits['max_score'], hit)
37
+ end
38
+ result
39
+ end
40
+
41
+ def request_body
42
+ {
43
+ :query => query,
44
+ :filter => filter
45
+ }
46
+ end
47
+
48
+ def query
49
+ {
50
+ :query_string => {
51
+ :query => @query_string.
52
+ gsub(/[^\w\*]/, ' ').
53
+ gsub(/\b(and|or|not)\b/i, '').
54
+ gsub(/\s+/, ' ').strip
55
+ }
56
+ }
57
+ end
58
+
59
+ def filter
60
+ now = Time.now
61
+ {
62
+ :and => [
63
+ {:not => {:term => {:obj_type => :image}}},
64
+ {:not => {:term => {:suppress_export => true}}},
65
+ {:not => {:range => {:valid_from => {:from => now.succ.to_iso, :to => '*'}}}},
66
+ {:not => {:range => {:valid_until => {:from => '*', :to => now.to_iso}}}}
67
+ ]
68
+ }
69
+ end
70
+
71
+ private
72
+
73
+ def url
74
+ "#{@options[:url]}/_search"
75
+ end
76
+ end
77
+
78
+ end
@@ -0,0 +1,9 @@
1
+ module RailsConnector
2
+
3
+ class RailsConnectorError < StandardError
4
+ end
5
+
6
+ class ResourceNotFound < RailsConnectorError
7
+ end
8
+
9
+ end # module RailsConnector
@@ -0,0 +1,135 @@
1
+ module RailsConnector
2
+ # This class provides an interfaces for handling CMS Links.
3
+ # To format a link for rendering in an html page, use the +cms_path+ or +cms_url+ methods.
4
+ class Link
5
+
6
+ extend ActiveModel::Naming
7
+
8
+ def initialize(link_data, destination_object = nil) #:nodoc:
9
+ @link_data = link_data.symbolize_keys
10
+ @destination_object = destination_object
11
+ end
12
+
13
+ # The link's external url. Only available for external links.
14
+ # Warning: Do not output the url directly unless you know what you are doing.
15
+ # Normally you want to use the +cms_path+ or +cms_url+ methods to format a link.
16
+ def url
17
+ @link_data[:url]
18
+ end
19
+
20
+ # The link's title.
21
+ def title
22
+ @link_data[:title]
23
+ end
24
+
25
+ # Returns the link's query string as in "index.html?query_string".
26
+ # See RFC3986 for details (http://www.ietf.org/rfc/rfc3986.txt).
27
+ def query
28
+ @link_data[:query]
29
+ end
30
+
31
+ # Depricated: use Link#query instead.
32
+ # Returns the link's query string as in "index.html?query_string".
33
+ # See RFC3986 for details (http://www.ietf.org/rfc/rfc3986.txt).
34
+ def search
35
+ query
36
+ end
37
+
38
+ # Returns the link's anchor as in "index.html#anchor".
39
+ # See RFC3986 for details (http://www.ietf.org/rfc/rfc3986.txt).
40
+ def fragment
41
+ @link_data[:fragment]
42
+ end
43
+
44
+ # Returns the browser window or browser frame to be used as a target for this link.
45
+ # Example: Links that should be opened in a new window will return "_blank" as their target.
46
+ def target
47
+ @link_data[:target]
48
+ end
49
+
50
+ def id #:nodoc:
51
+ @link_data[:link_id]
52
+ end
53
+
54
+ def tag_name # :nodoc:
55
+ @link_data[:tag_name]
56
+ end
57
+
58
+ def markdown_type # :nodoc:
59
+ @link_data[:markdown_type]
60
+ end
61
+
62
+ def markdown? # :nodoc:
63
+ 'markdown' == @link_data[:source_format]
64
+ end
65
+
66
+ # Returns the file extension (e.g. zip, pdf) of this link's (internal or external) target.
67
+ # Returns an empty string if the file extension is can not be determined.
68
+ def file_extension
69
+ if internal?
70
+ destination_object ? destination_object.file_extension : ""
71
+ else
72
+ path = URI.parse(url).path rescue nil
73
+ path.blank? ? "" : File.extname(path)[1..-1] || ""
74
+ end
75
+ end
76
+
77
+ # Returns the id of the Links' destination_object.
78
+ def destination_object_id
79
+ destination
80
+ end
81
+
82
+ # Returns the title of this Link if it is set.
83
+ # Otherwise it returns the display_title of the destination object for internal Links
84
+ # or the URL for external Links.
85
+ def display_title
86
+ dt = title
87
+ dt = destination_object.display_title if dt.blank? && !external?
88
+ dt = url if dt.blank?
89
+ dt
90
+ end
91
+
92
+ # Returns true this Link links to a CMS Object.
93
+ def internal?
94
+ url.nil?
95
+ end
96
+
97
+ # Returns true if this Link links to an external URL.
98
+ def external?
99
+ !internal?
100
+ end
101
+
102
+ # An internal Link is active if it's destination object is active.
103
+ # An external Link is always active.
104
+ def active?
105
+ external? || (destination_object && destination_object.active?)
106
+ end
107
+
108
+ def external_prefix? #:nodoc:
109
+ nil != (url =~ /\s?external:/)
110
+ end
111
+
112
+ def resolved? #:nodoc:
113
+ external? || resolved_internal?
114
+ end
115
+
116
+ # Returns the destination object (+Obj+) of this Link.
117
+ def destination_object
118
+ @destination_object ||= Obj.find(destination) if resolved_internal?
119
+ end
120
+
121
+ def to_liquid # :nodoc:
122
+ LiquidSupport::LinkDrop.new(self)
123
+ end
124
+
125
+ private
126
+
127
+ def resolved_internal?
128
+ internal? && !destination.nil?
129
+ end
130
+
131
+ def destination
132
+ @link_data[:destination]
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,29 @@
1
+ module RailsConnector
2
+ class LogSubscriber < ActiveSupport::LogSubscriber
3
+ def self.runtime=(value)
4
+ Thread.current["rails_connector_cms_load_runtime"] = value
5
+ end
6
+
7
+ def self.runtime
8
+ Thread.current["rails_connector_cms_load_runtime"] ||= 0
9
+ end
10
+
11
+ def self.reset_runtime
12
+ rt, self.runtime = runtime, 0
13
+ rt
14
+ end
15
+
16
+ def cms_load(event)
17
+ self.class.runtime += event.duration
18
+ return unless logger.debug?
19
+
20
+ name = '%s (%.1fms)' % [event.payload[:name], event.duration]
21
+ debug " #{name} #{event.payload[:index]} #{event.payload[:keys].inspect}"
22
+ end
23
+
24
+ def logger
25
+ Rails.logger
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,72 @@
1
+ module RailsConnector
2
+
3
+ # This class provides methods used to retrieve objects from CMS based an entry
4
+ # in CMS of the obj_class <tt>NamedLink</tt>.
5
+
6
+ class NamedLink < Obj
7
+
8
+ class NotFound < StandardError
9
+ end
10
+
11
+ @@named_links_cache = nil
12
+
13
+ # Generates a cache of named links based on the CMS objects related link list.
14
+ # Raises exceptions if zero or more than one objects have this obj_class
15
+ def self.generate_named_links_cache #:nodoc:
16
+ return if @@named_links_cache
17
+
18
+ found_object = find_named_link_obj
19
+ if found_object.nil?
20
+ raise NotFound, "Couldn't find NamedLink CMS Object!"
21
+ else
22
+ @@named_links_cache = found_object.
23
+ related_links.
24
+ flatten(1).
25
+ each_with_object({}) do |link_obj, temp|
26
+ temp[link_obj.title] = link_obj.destination_object
27
+ end
28
+ end
29
+
30
+ return nil
31
+ end
32
+
33
+ # This method will be called to retrieve the NamedLink Obj.
34
+ # By default it will look for the Obj at the path "_named_links".
35
+ # Overwrite this method only if you know what you are doing.
36
+ def self.find_named_link_obj
37
+ Obj.find_by_path("/_named_links")
38
+ end
39
+
40
+ def self.cache_expiry_time=(value) #:nodoc:
41
+ raise "NamedLink.cache_expiry_time is deprecated. NamedLink no longer has a separate cache."
42
+ end
43
+
44
+ def self.cache_expired? #:nodoc:
45
+ true
46
+ end
47
+
48
+ # Returns the CMS object mapped to the given title or nil.
49
+ # The title can be a string of symbol.
50
+ def self.get_object(title, options = {})
51
+ object = named_links[title.to_s]
52
+ raise NotFound, "The NamedLink '#{title.to_s}' does not exist" if object.nil?
53
+ object
54
+ end
55
+
56
+ def self.reset_cache #:nodoc:
57
+ @@named_links_cache = nil
58
+ end
59
+
60
+ def self.named_links #:nodoc:
61
+ reset_cache if cache_expired?
62
+ generate_named_links_cache unless named_links_cache
63
+ named_links_cache
64
+ end
65
+
66
+ def self.named_links_cache #:nodoc:
67
+ @@named_links_cache
68
+ end
69
+
70
+ end
71
+
72
+ end