naranya_ecm-sdk 0.0.14 → 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/lib/naranya_ecm/behaviors/localizable.rb +27 -31
  3. data/lib/naranya_ecm/behaviors/mediable.rb +25 -0
  4. data/lib/naranya_ecm/behaviors/timestampable.rb +18 -15
  5. data/lib/naranya_ecm/cache/key.rb +32 -56
  6. data/lib/naranya_ecm/cache/methods.rb +50 -63
  7. data/lib/naranya_ecm/lifecycles/content_lifecycle.rb +39 -16
  8. data/lib/naranya_ecm/models/category.rb +11 -30
  9. data/lib/naranya_ecm/models/content.rb +73 -73
  10. data/lib/naranya_ecm/models/content_version.rb +50 -29
  11. data/lib/naranya_ecm/models/download_authorization.rb +22 -26
  12. data/lib/naranya_ecm/models/media_resource.rb +49 -15
  13. data/lib/naranya_ecm/rest/associations.rb +156 -0
  14. data/lib/naranya_ecm/rest/client.rb +4 -0
  15. data/lib/naranya_ecm/rest/errors.rb +53 -0
  16. data/lib/naranya_ecm/rest/finder_methods.rb +50 -0
  17. data/lib/naranya_ecm/rest/model.rb +215 -0
  18. data/lib/naranya_ecm/rest/persistence.rb +122 -0
  19. data/lib/naranya_ecm/rest/relation.rb +54 -0
  20. data/lib/naranya_ecm/search/hit.rb +19 -14
  21. data/lib/naranya_ecm/search/methods.rb +18 -20
  22. data/lib/naranya_ecm/search/query.rb +229 -230
  23. data/lib/naranya_ecm/search/results.rb +136 -139
  24. data/lib/naranya_ecm-sdk/version.rb +1 -1
  25. data/lib/naranya_ecm-sdk.rb +54 -13
  26. data/naranya_ecm-sdk.gemspec +1 -1
  27. data/spec/models/category_spec.rb +7 -2
  28. data/spec/models/content_spec.rb +11 -2
  29. data/spec/models/media_spec.rb +1 -1
  30. data/spec/spec_helper.rb +1 -1
  31. data/spec/support/naranya_ecms_shared_specs.rb +0 -12
  32. metadata +15 -19
  33. data/lib/naranya_ecm/behaviors/resourceable.rb +0 -22
  34. data/lib/naranya_ecm/behaviors.rb +0 -10
  35. data/lib/naranya_ecm/cache.rb +0 -9
  36. data/lib/naranya_ecm/has_many_patch.rb +0 -105
  37. data/lib/naranya_ecm/lifecycles/lifecycleable.rb +0 -43
  38. data/lib/naranya_ecm/lifecycles/version_lifecycle.rb +0 -75
  39. data/lib/naranya_ecm/lifecycles.rb +0 -10
  40. data/lib/naranya_ecm/models/embedded_hash.rb +0 -10
  41. data/lib/naranya_ecm/models/embedded_localized_hash.rb +0 -38
  42. data/lib/naranya_ecm/models/lifecycle.rb +0 -34
  43. data/lib/naranya_ecm/models.rb +0 -15
  44. data/lib/naranya_ecm/search.rb +0 -14
@@ -1,170 +1,167 @@
1
- module NaranyaEcm
2
- module Search
3
- class Results
4
-
5
- DEFAULT_MATERIALIZER = Proc.new do |resource_class, hits|
6
-
7
- # Extract the object ids for ActiveResource loading:
8
- unloaded_resource_ids = hits.map(&:id)
9
-
10
- materialized_elements = []
11
-
12
- # Load from cache any matching element caching_key:
13
- hits.each do |hit|
14
- cached_element = resource_class.find_cached(resource_class.cache_key_for id: hit.id, timestamp: hit.updated_at)
15
- if cached_element.present?
16
- cached_element.merge_search_hit_data(hit)
17
- materialized_elements << cached_element
18
- unloaded_resource_ids.delete cached_element.id
19
- end
20
- end
21
-
22
- # Load from server all elements not in cache:
23
- resource_class.find(:all, params: { id: unloaded_resource_ids }).each do |element|
24
- hit = hits.detect { |hit| hit.id == element.id }
25
-
26
- # Add the hit score and order to each element:
27
- element.merge_search_hit_data(hit)
28
-
29
- # Add it to the loaded_elements:
30
- materialized_elements << element
31
- end unless unloaded_resource_ids.empty?
32
-
33
- materialized_elements
34
- end
1
+ require 'naranya_ecm/rest/relation'
35
2
 
36
- include Enumerable
37
- delegate :to_yaml, :all?, :each, to: :to_a
3
+ module NaranyaEcm::Search
4
+ class Results < NaranyaEcm::Rest::Relation
38
5
 
39
- # The array of actual elements returned by index actions
40
- attr_reader :elements, :resource_class
6
+ include HTTParty
7
+ base_uri NaranyaEcm.options[:site]
41
8
 
42
- attr_accessor :materializer
43
-
44
- def initialize(given_dsl, given_resource_class = nil)
45
- @dsl, @resource_class = given_dsl, given_resource_class
9
+ DEFAULT_MATERIALIZER = Proc.new do |search_results|
10
+
11
+ # Duplicate the hits array - we'll replace each element with the
12
+ # resource:
13
+ result_elements = search_results.hits.dup
14
+
15
+ # Load from cache any matching element caching_key:
16
+ result_elements.each_index do |index|
17
+ hit_data = result_elements[index].dup
18
+ fetched_data = result_elements[index].cache_key.read
19
+ if fetched_data.present?
20
+ puts "===== Cache READ: '#{result_elements[index].cache_key}'"
21
+ result_elements[index] = hit_data.klass.load(fetched_data)
22
+ result_elements[index].merge_search_hit_data(hit_data)
23
+ end
46
24
  end
47
25
 
48
- def total_hits
49
- do_search! unless @total_hits
50
- @total_hits
51
- end
52
- alias_method :total_count, :total_hits
53
-
54
- def digest
55
- [
56
- 'naranya_ecm',
57
- 'search',
58
- 'results',
59
- Digest::MD5.hexdigest(@dsl.to_s)
60
- ]
61
- end
26
+ # Load from server all elements yet to materialize:
27
+ pending_hits = result_elements.select { |e| e.is_a? NaranyaEcm::Search::Hit }
28
+ pending_hits.map(&:klass).uniq.each do |hit_klass|
29
+ # Obtain the ids for this hit_klass:
30
+ hit_ids = pending_hits.select { |e| e.klass == hit_klass }.map(&:id)
31
+
32
+ # Fetch the data from server:
33
+ hit_klass.where(id: hit_ids).fetch_from_server.each do |fetched_data|
34
+ r = hit_klass.load fetched_data
35
+
36
+ hit_data = result_elements.detect { |e| e.is_a?(NaranyaEcm::Search::Hit) && e.klass == r.class && e.id == r.id }
37
+ hit_index = result_elements.find_index hit_data
62
38
 
63
- def hits
64
- do_search! unless @hits
65
- @hits
39
+ puts "===== Cache WRITE: '#{r.cache_key}'"
40
+ r.cache_key.write fetched_data
41
+ r.merge_search_hit_data hit_data
42
+ result_elements[hit_index] = r
43
+ end
66
44
  end
67
45
 
68
- def count
69
- hits.count
70
- end
46
+ result_elements
47
+ end
71
48
 
72
- def any?
73
- count > 0
74
- end
49
+ attr_accessor :materializer
75
50
 
76
- def empty?
77
- count < 1
78
- end
51
+ def total_hits
52
+ do_search! unless @total_hits
53
+ @total_hits
54
+ end
55
+ alias_method :total_count, :total_hits
56
+
57
+ def digest
58
+ [
59
+ 'naranya_ecm',
60
+ 'search',
61
+ 'results',
62
+ Digest::MD5.hexdigest(@dsl.to_s)
63
+ ]
64
+ end
79
65
 
80
- def facts
81
- do_search! unless @facts
82
- @facts
83
- end
66
+ def cache_keys
67
+ hits.map(&:cache_key)
68
+ end
84
69
 
85
- def max_score
86
- do_search! unless @max_score
87
- @max_score
88
- end
70
+ def hits
71
+ do_search! unless @hits
72
+ @hits
73
+ end
89
74
 
90
- def query_process_time
91
- do_search! unless @query_process_time
92
- @query_process_time
93
- end
75
+ def count
76
+ hits.count
77
+ end
94
78
 
95
- def query_total_time
96
- do_search! unless @query_total_time
97
- @query_total_time
98
- end
79
+ def any?
80
+ count > 0
81
+ end
99
82
 
100
- def materializer
101
- @materializer || DEFAULT_MATERIALIZER
102
- end
103
-
104
- def to_a
105
- materialize! unless @elements
106
- elements
107
- end
83
+ def empty?
84
+ count < 1
85
+ end
108
86
 
109
- private
87
+ def facts
88
+ do_search! unless @facts
89
+ @facts
90
+ end
110
91
 
111
- def do_search!
92
+ def max_score
93
+ do_search! unless @max_score
94
+ @max_score
95
+ end
112
96
 
113
- start_time = Time.now
114
-
115
- # Strip '.json' from collection path:
116
- search_path = resource_class.present? ? resource_class.collection_path.split('.').first : ''
117
- search_path += '/_search'
97
+ def query_process_time
98
+ do_search! unless @query_process_time
99
+ @query_process_time
100
+ end
118
101
 
119
- search_site = URI(NaranyaEcm.options.site)
102
+ def query_total_time
103
+ do_search! unless @query_total_time
104
+ @query_total_time
105
+ end
120
106
 
121
- search_connection = Net::HTTP.new search_site.host, search_site.port
107
+ def materializer
108
+ @materializer || DEFAULT_MATERIALIZER
109
+ end
110
+
111
+ def to_a
112
+ materialize! unless @elements
113
+ elements
114
+ end
122
115
 
123
- search_get_url = URI("#{search_site}#{search_path}")
116
+ private
124
117
 
125
- search_request = Net::HTTP::Get.new search_get_url
126
- search_request.body = ActiveSupport::JSON.encode @dsl
118
+ def do_search!
127
119
 
128
- puts "curl --XGET '#{search_get_url}?pretty=true' -d '#{search_request.body}'"
120
+ start_time = Time.now
121
+
122
+ # Strip '.json' from collection path:
123
+ search_path = klass.present? ? klass.path : ''
124
+ search_path += '/_search'
129
125
 
130
- # Use the class connection to get the search results,
131
- # using any authentication / headers configured:
132
- #search_response = connection.get(search_path, headers)
133
- search_response = search_connection.request search_request
134
-
135
- # Parse the search results:
136
- @facts = ActiveSupport::JSON.decode(search_response.body)
137
- .with_indifferent_access
126
+ # Use the class connection to get the search results,
127
+ # using any authentication / headers configured:
128
+ #search_response = connection.get(search_path, headers)
129
+ search_body = ActiveSupport::JSON.encode(conditions)
130
+ search_response = self.class.get search_path, body: search_body
131
+
132
+ puts "curl -XGET '#{NaranyaEcm.options[:site]}#{search_path}?pretty=true' -d '#{search_body}'"
133
+
134
+ # Parse the search results:
135
+ @facts = search_response.to_hash.with_indifferent_access
138
136
 
139
- # Obtain the query processing time, which is given in miliseconds as 'took' key:
140
- @query_process_time = (Float(@facts.delete(:took)) / 1000)
137
+ # Obtain the query processing time, which is given in miliseconds as 'took' key:
138
+ @query_process_time = (Float(@facts.delete(:took)) / 1000)
141
139
 
142
- result_hits = @facts.delete :hits
140
+ result_hits = @facts.delete :hits
143
141
 
144
- @total_hits = result_hits.delete :total
145
- @max_score = result_hits.delete :max_score
142
+ @total_hits = result_hits.delete :total
143
+ @max_score = result_hits.delete :max_score
146
144
 
147
- @hits = result_hits[:hits].each_with_index.map { |ht, o| Hit.new(ht.merge(order: o)) }
148
-
149
- @hits.freeze
150
- @facts.freeze
145
+ @hits = result_hits[:hits].each_with_index.map { |ht, o| Hit.new(ht.merge(order: o)) }
146
+
147
+ @hits.freeze
148
+ @facts.freeze
151
149
 
152
- @query_total_time = (Time.now - start_time)
153
-
154
- end
150
+ @query_total_time = (Time.now - start_time)
151
+
152
+ end
155
153
 
156
- def materialize!
157
- # Call the materializer and:
158
- # - Filter out elements that materialized as nulls
159
- # - Order the elements by relevance
160
- @elements = materializer.call(resource_class, hits)
161
- .select { |materialized_element| materialized_element.present? }
162
- .sort_by do |element|
163
- element.search_order
164
- end
165
-
166
- end
167
-
168
- end
154
+ def materialize!
155
+ # Call the materializer and:
156
+ # - Filter out elements that materialized as nulls
157
+ # - Order the elements by relevance
158
+ @elements = materializer.call(self)
159
+ .select { |materialized_element| materialized_element.present? }
160
+ .sort_by do |element|
161
+ element.search_order
162
+ end
163
+
164
+ end
165
+
169
166
  end
170
- end
167
+ end
@@ -1,3 +1,3 @@
1
1
  module NaranyaEcm
2
- VERSION = "0.0.14"
2
+ VERSION = "0.0.15"
3
3
  end
@@ -1,8 +1,9 @@
1
- require "naranya_ecm-sdk/version"
1
+ require 'naranya_ecm-sdk/version'
2
2
 
3
3
  require 'active_support/time'
4
4
  require 'active_support/configurable'
5
5
  require 'active_support/dependencies/autoload'
6
+ require 'active_support/core_ext/hash/indifferent_access.rb'
6
7
  require 'yaml'
7
8
 
8
9
  require 'fog'
@@ -11,17 +12,57 @@ require 'fog'
11
12
  module NaranyaEcm
12
13
 
13
14
  extend ActiveSupport::Autoload
14
-
15
- autoload :Cache
16
- autoload :Search
17
- autoload :Lifecycles
18
15
 
19
- autoload :Models
20
- autoload :Behaviors
16
+ autoload_under 'rest' do
17
+ autoload :Client
18
+ end
21
19
 
22
- include ActiveSupport::Configurable
20
+ module Rest
21
+ extend ActiveSupport::Autoload
22
+ autoload :Client
23
+ autoload :Persistence
24
+ autoload :Associations
25
+ autoload :Model
26
+ autoload :FinderMethods
27
+ autoload :Relation
28
+ end
29
+
30
+ autoload_under 'models' do
31
+ autoload :Category
32
+ autoload :Content
33
+ autoload :ContentVersion
34
+ autoload :DownloadAuthorization
35
+ autoload :MediaResource
36
+ end
37
+
38
+ module Behaviors
39
+ extend ActiveSupport::Autoload
40
+ autoload :Localizable
41
+ autoload :Timestampable
42
+ autoload :Mediable
43
+ end
44
+
45
+ module Cache
46
+ extend ActiveSupport::Autoload
47
+ autoload :Key
48
+ autoload :Methods
49
+ end
23
50
 
24
- include Models
51
+ autoload_under 'lifecycles' do
52
+ autoload :ContentLifecycle
53
+ end
54
+
55
+ module Search
56
+ extend ActiveSupport::Autoload
57
+ autoload :Hit
58
+ autoload :Methods
59
+ autoload :Query
60
+ autoload :Results
61
+ end
62
+
63
+ autoload :ActiveResourceBasePatch
64
+
65
+ include ActiveSupport::Configurable
25
66
 
26
67
  DEFAULT_CONFIG = {
27
68
  site: "http://ecm.naranya.net:5000",
@@ -41,15 +82,15 @@ module NaranyaEcm
41
82
  yield config
42
83
  end
43
84
 
85
+ def document_module
86
+ NaranyaEcm::Rest::Model
87
+ end
88
+
44
89
  def options
45
90
  auto_conf unless config.present?
46
91
  config
47
92
  end
48
93
 
49
- def cache_key_for(attributes = {})
50
- Cache::Key.new attributes
51
- end
52
-
53
94
  def load_config_from_yml(yml_path, env="production")
54
95
  raise "YAML file doesn't extst" unless File.exist?(yml_path)
55
96
  yml_config = DEFAULT_CONFIG.merge(YAML::load(ERB.new(File.read(yml_path)).result)[env])
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_dependency 'activesupport', '>= 4.0.4'
23
23
  spec.add_dependency 'activemodel', '>= 4.0.4'
24
- spec.add_dependency 'activeresource', '~> 4.0.0'
24
+ spec.add_dependency 'httparty'
25
25
  spec.add_dependency 'state_machine'
26
26
  spec.add_dependency 'fog'
27
27
 
@@ -4,8 +4,13 @@ describe NaranyaEcm::Category, vcr: true do
4
4
 
5
5
  it_behaves_like "a NaranyaEcm resource"
6
6
 
7
- it "responds to #names" do
8
- expect(subject).to respond_to :names
7
+ describe "#name" do
8
+ it "responds to #name" do
9
+ expect(subject).to respond_to :name
10
+ end
11
+ it "responds to #name_translations" do
12
+ expect(subject).to respond_to :name_translations
13
+ end
9
14
  end
10
15
 
11
16
  end
@@ -4,8 +4,17 @@ describe NaranyaEcm::Content, vcr: true do
4
4
 
5
5
  it_behaves_like "a NaranyaEcm resource"
6
6
 
7
- it "responds to #titles" do
8
- expect(subject).to respond_to :titles
7
+ describe "localized title" do
8
+ it "responds to #title" do
9
+ expect(subject).to respond_to :title
10
+ end
11
+ it "responds to #title_translations" do
12
+ expect(subject).to respond_to :title_translations
13
+ end
14
+ end
15
+
16
+ it "responds to #description" do
17
+ expect(subject).to respond_to :title
9
18
  end
10
19
 
11
20
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe NaranyaEcm::Media, vcr: true do
3
+ describe NaranyaEcm::MediaResource, vcr: true do
4
4
 
5
5
  it_behaves_like "a NaranyaEcm resource"
6
6
 
data/spec/spec_helper.rb CHANGED
@@ -26,7 +26,7 @@ VCR.configure do |config|
26
26
  config.default_cassette_options = { record: :new_episodes, erb: true }
27
27
  end
28
28
 
29
- require 'naranya_ecm'
29
+ require 'naranya_ecm-sdk'
30
30
 
31
31
  RSpec.configure do |config|
32
32
  # ## Mock Framework
@@ -6,18 +6,6 @@ shared_examples "a NaranyaEcm resource" do
6
6
 
7
7
  subject { described_class }
8
8
 
9
- it "responds to #site" do
10
- expect(subject).to respond_to :site
11
- end
12
-
13
- describe "#site" do
14
- subject { described_class.site }
15
-
16
- it "has the same URL as in the configuration options" do
17
- expect(subject.to_s).to eq NaranyaEcm.options.site
18
- end
19
- end
20
-
21
9
  it "responds to #find" do
22
10
  expect(subject).to respond_to :find
23
11
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: naranya_ecm-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.14
4
+ version: 0.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roberto Quintanilla
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-24 00:00:00.000000000 Z
11
+ date: 2014-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -39,19 +39,19 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: 4.0.4
41
41
  - !ruby/object:Gem::Dependency
42
- name: activeresource
42
+ name: httparty
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
- version: 4.0.0
47
+ version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
- version: 4.0.0
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: state_machine
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -168,28 +168,24 @@ files:
168
168
  - dev/search.rb
169
169
  - lib/naranya_ecm-sdk.rb
170
170
  - lib/naranya_ecm-sdk/version.rb
171
- - lib/naranya_ecm/behaviors.rb
172
171
  - lib/naranya_ecm/behaviors/localizable.rb
173
- - lib/naranya_ecm/behaviors/resourceable.rb
172
+ - lib/naranya_ecm/behaviors/mediable.rb
174
173
  - lib/naranya_ecm/behaviors/timestampable.rb
175
- - lib/naranya_ecm/cache.rb
176
174
  - lib/naranya_ecm/cache/key.rb
177
175
  - lib/naranya_ecm/cache/methods.rb
178
- - lib/naranya_ecm/has_many_patch.rb
179
- - lib/naranya_ecm/lifecycles.rb
180
176
  - lib/naranya_ecm/lifecycles/content_lifecycle.rb
181
- - lib/naranya_ecm/lifecycles/lifecycleable.rb
182
- - lib/naranya_ecm/lifecycles/version_lifecycle.rb
183
- - lib/naranya_ecm/models.rb
184
177
  - lib/naranya_ecm/models/category.rb
185
178
  - lib/naranya_ecm/models/content.rb
186
179
  - lib/naranya_ecm/models/content_version.rb
187
180
  - lib/naranya_ecm/models/download_authorization.rb
188
- - lib/naranya_ecm/models/embedded_hash.rb
189
- - lib/naranya_ecm/models/embedded_localized_hash.rb
190
- - lib/naranya_ecm/models/lifecycle.rb
191
181
  - lib/naranya_ecm/models/media_resource.rb
192
- - lib/naranya_ecm/search.rb
182
+ - lib/naranya_ecm/rest/associations.rb
183
+ - lib/naranya_ecm/rest/client.rb
184
+ - lib/naranya_ecm/rest/errors.rb
185
+ - lib/naranya_ecm/rest/finder_methods.rb
186
+ - lib/naranya_ecm/rest/model.rb
187
+ - lib/naranya_ecm/rest/persistence.rb
188
+ - lib/naranya_ecm/rest/relation.rb
193
189
  - lib/naranya_ecm/search/hit.rb
194
190
  - lib/naranya_ecm/search/methods.rb
195
191
  - lib/naranya_ecm/search/query.rb
@@ -1,22 +0,0 @@
1
- # estoy loco...
2
-
3
- module NaranyaEcm::Behaviors
4
- module Resourceable
5
- extend ActiveSupport::Concern
6
-
7
- included do
8
- self.site = NaranyaEcm.options.site
9
- before_save :normalize_attributes
10
- after_save :reload
11
- end
12
-
13
- def normalize_attributes
14
- # Remove associated objects; Keep only the ID's:
15
- self.reflections
16
- .select { |key, val| val.class.name == "ActiveResource::Reflection::AssociationReflection" }
17
- .keys.each { |attribute_name| self.attributes.delete attribute_name }
18
-
19
- end
20
-
21
- end
22
- end
@@ -1,10 +0,0 @@
1
- require 'active_support/dependencies/autoload'
2
-
3
- module NaranyaEcm
4
- module Behaviors
5
- extend ActiveSupport::Autoload
6
- autoload :Localizable
7
- autoload :Timestampable
8
- autoload :Resourceable
9
- end
10
- end
@@ -1,9 +0,0 @@
1
- require 'active_support/dependencies/autoload'
2
-
3
- module NaranyaEcm
4
- module Cache
5
- extend ActiveSupport::Autoload
6
- autoload :Key
7
- autoload :Methods
8
- end
9
- end