naranya_ecm-sdk 0.0.42 → 0.0.43

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.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/.rspec +3 -0
  4. data/Gemfile +1 -2
  5. data/lib/aasm/persistence/rest_persistence.rb +11 -6
  6. data/lib/naranya_ecm-sdk.rb +4 -65
  7. data/lib/naranya_ecm-sdk/version.rb +1 -1
  8. data/lib/naranya_ecm/models/media_resource.rb +1 -1
  9. data/lib/naranya_ecm/models/notification.rb +2 -2
  10. data/lib/naranya_ecm/rest/errors.rb +16 -3
  11. data/lib/naranya_ecm/rest/finder_methods.rb +4 -4
  12. data/lib/naranya_ecm/rest/model.rb +87 -19
  13. data/lib/naranya_ecm/rest/persistence.rb +234 -46
  14. data/lib/naranya_ecm/rest/relation.rb +15 -5
  15. data/lib/naranya_ecm/search/results.rb +19 -17
  16. data/lib/ncontent-sdk-testing.rb +13 -0
  17. data/lib/ncontent-sdk.rb +85 -0
  18. data/lib/ncontent/sdk/config.rb +75 -0
  19. data/lib/ncontent/sdk/faraday_middleware.rb +4 -0
  20. data/lib/ncontent/sdk/faraday_middleware/required_response_format.rb +14 -0
  21. data/lib/ncontent/sdk/faraday_middleware/response_parser.rb +35 -0
  22. data/lib/ncontent/sdk/faraday_middleware/rest_api_call_benchmark.rb +49 -0
  23. data/lib/ncontent/sdk/railtie.rb +59 -0
  24. data/lib/ncontent/sdk/rest_client.rb +131 -0
  25. data/lib/ncontent/sdk/testing/server_mock.rb +316 -0
  26. data/naranya_ecm-sdk.gemspec +24 -10
  27. metadata +85 -68
  28. data/lib/naranya_ecm/rest/client.rb +0 -92
  29. data/spec/models/category_spec.rb +0 -16
  30. data/spec/models/content_spec.rb +0 -20
  31. data/spec/models/content_version_spec.rb +0 -7
  32. data/spec/models/download_authorization.rb +0 -7
  33. data/spec/models/media_spec.rb +0 -7
  34. data/spec/models/module_spec.rb +0 -18
  35. data/spec/spec_helper.rb +0 -47
  36. data/spec/support/naranya_ecms_shared_specs.rb +0 -21
@@ -1,5 +1,4 @@
1
1
  require 'naranya_ecm/rest/errors'
2
- require 'naranya_ecm/rest/client'
3
2
 
4
3
  module NaranyaEcm::Rest
5
4
  class Relation
@@ -26,8 +25,7 @@ module NaranyaEcm::Rest
26
25
  end
27
26
 
28
27
  def fetch_from_server
29
- format = NaranyaEcm.options[:format] || 'json'
30
- Client.get("#{klass.path}.#{format}", query: conditions).to_a
28
+ NContent::SDK::RESTClient.get("#{klass.path}.json", query: conditions).body
31
29
  end
32
30
 
33
31
  def load(given_list)
@@ -36,7 +34,15 @@ module NaranyaEcm::Rest
36
34
  raise "Type Mismatch: some elements are not a hash nor associated_class"
37
35
  end
38
36
 
39
- @elements = given_list.map { |e| e.is_a?(Hash) ? @klass.load(e) : e }
37
+ @elements = given_list.map do |element|
38
+ if element.is_a?(Hash)
39
+ loaded_element = @klass.load(element)
40
+ loaded_element.instance_variable_set("@new_resource", false)
41
+ loaded_element
42
+ else
43
+ element
44
+ end
45
+ end
40
46
  end
41
47
 
42
48
  private
@@ -45,7 +51,11 @@ module NaranyaEcm::Rest
45
51
  end
46
52
 
47
53
  def materialize!
48
- @elements = fetch_from_server.map { |s| klass.load s }
54
+ @elements = fetch_from_server.map do |s|
55
+ resource = klass.load s
56
+ resource.instance_variable_set("@new_resource", false)
57
+ resource
58
+ end
49
59
  end
50
60
 
51
61
  end
@@ -1,11 +1,10 @@
1
1
  require 'naranya_ecm/rest/relation'
2
- require 'naranya_ecm/rest/client'
3
2
 
4
3
  module NaranyaEcm::Search
5
4
  class Results < NaranyaEcm::Rest::Relation
6
5
 
7
6
  DEFAULT_MATERIALIZER = Proc.new do |search_results|
8
-
7
+
9
8
  # Duplicate the hits array - we'll replace each element with the
10
9
  # resource:
11
10
  result_elements = search_results.hits.dup
@@ -15,11 +14,11 @@ module NaranyaEcm::Search
15
14
  pending_hits.map(&:klass).uniq.each do |hit_klass|
16
15
  # Obtain the ids for this hit_klass:
17
16
  hit_ids = pending_hits.select { |e| e.klass == hit_klass }.map(&:id)
18
-
17
+
19
18
  # Fetch the data from server:
20
19
  hit_klass.where(id: hit_ids).fetch_from_server.each do |fetched_data|
21
20
  r = hit_klass.load fetched_data
22
-
21
+
23
22
  hit_data = result_elements.detect { |e| e.is_a?(NaranyaEcm::Search::Hit) && e.klass == r.class && e.id == r.id }
24
23
  hit_index = result_elements.find_index hit_data
25
24
 
@@ -31,6 +30,8 @@ module NaranyaEcm::Search
31
30
  result_elements
32
31
  end
33
32
 
33
+ delegate :get, to: NContent::SDK::RESTClient
34
+
34
35
  attr_accessor :materializer
35
36
 
36
37
  def total_hits
@@ -88,7 +89,7 @@ module NaranyaEcm::Search
88
89
  def materializer
89
90
  @materializer || DEFAULT_MATERIALIZER
90
91
  end
91
-
92
+
92
93
  def to_a
93
94
  materialize! unless @elements
94
95
  elements
@@ -99,20 +100,21 @@ module NaranyaEcm::Search
99
100
  def do_search!
100
101
 
101
102
  start_time = Time.now
102
-
103
+
103
104
  # Strip '.json' from collection path:
104
105
  search_path = klass.present? ? klass.path : ''
105
106
  search_path += '/_search'
106
107
 
107
- search_body = ActiveSupport::JSON.encode(conditions)
108
- search_response = NaranyaEcm::Rest::Client.get search_path, body: search_body
109
-
110
- # puts "curl -XGET '#{NaranyaEcm.options[:site]}#{search_path}?pretty=true' -d '#{search_body}'"
111
-
108
+ search_response = get search_path do |req|
109
+ req.body = ActiveSupport::JSON.encode(conditions)
110
+ end
111
+
112
+ # puts "curl -XGET '#{NContent::SDK.config.api_host}#{search_path}?pretty=true' -d '#{search_body}'"
113
+
112
114
  # Parse the search results:
113
- @facts = search_response.to_hash.with_indifferent_access
115
+ @facts = search_response.body.with_indifferent_access
114
116
 
115
- # Obtain the query processing time, which is given in miliseconds as 'took' key:
117
+ # Obtain the query processing time, which is given in miliseconds as 'took' key:
116
118
  @query_process_time = (Float(@facts.delete(:took)) / 1000)
117
119
 
118
120
  result_hits = @facts.delete :hits
@@ -121,12 +123,12 @@ module NaranyaEcm::Search
121
123
  @max_score = result_hits.delete :max_score
122
124
 
123
125
  @hits = result_hits[:hits].each_with_index.map { |ht, o| Hit.new(ht.merge(order: o)) }
124
-
126
+
125
127
  @hits.freeze
126
128
  @facts.freeze
127
129
 
128
130
  @query_total_time = (Time.now - start_time)
129
-
131
+
130
132
  end
131
133
 
132
134
  def materialize!
@@ -138,8 +140,8 @@ module NaranyaEcm::Search
138
140
  .sort_by do |element|
139
141
  element.search_order
140
142
  end
141
-
143
+
142
144
  end
143
-
145
+
144
146
  end
145
147
  end
@@ -0,0 +1,13 @@
1
+ require 'naranya_ecm-sdk'
2
+ require 'ncontent-sdk'
3
+
4
+ module NContent
5
+ module SDK
6
+
7
+ module Testing
8
+ extend ActiveSupport::Autoload
9
+ autoload :ServerMock, 'ncontent/sdk/testing/server_mock'
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,85 @@
1
+
2
+ # Require the Railtie if loaded from Rails app:
3
+ require 'ncontent/sdk/railtie' if defined? Rails
4
+ require 'active_support/core_ext/module'
5
+
6
+ module NContent
7
+
8
+ module SDK
9
+
10
+ extend ActiveSupport::Autoload
11
+
12
+ autoload :Config, "ncontent/sdk/config"
13
+ autoload :RESTClient, "ncontent/sdk/rest_client"
14
+
15
+ def self.config
16
+ @@config ||= NContent::SDK::Config.new
17
+ end
18
+
19
+ def self.configure(config_hash = {})
20
+ if block_given?
21
+ yield config
22
+ else
23
+ config_hash.each do |config_key, config_value|
24
+ if config.respond_to? "#{config_key}="
25
+ config.public_send "#{config_key}=", config_value
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ mattr_writer :logger
32
+ def self.logger
33
+ @@logger ||= Logger.new($stdout)
34
+ end
35
+
36
+ def self.storage
37
+ @@storage ||= Fog::Storage.new \
38
+ provider: config.bucket_provider,
39
+ aws_access_key_id: config.bucket_api_key,
40
+ aws_secret_access_key: config.bucket_api_secret
41
+
42
+ end
43
+
44
+ def self.root_directory
45
+ @@root_directory ||= storage.directories.get(config.bucket_name)
46
+ end
47
+
48
+ mattr_writer :cache
49
+ def self.cache
50
+ @@cache ||= ActiveSupport::Cache.lookup_store *config.cache
51
+ end
52
+
53
+ def self.api_client
54
+ @@api_client ||= begin
55
+ require 'oauth2'
56
+ require 'ncontent/sdk/faraday_middleware'
57
+
58
+ OAuth2::Client.new(config.api_key, config.api_secret, site: config.api_host) do |faraday|
59
+ ### Oauth2 Client (Faraday) builder:
60
+
61
+ faraday.request :url_encoded # them posts...
62
+
63
+ faraday.response :logger, NContent::SDK.logger
64
+
65
+ # faraday.use NContent::SDK::FaradayMiddleware::ResponseParser
66
+ faraday.use NContent::SDK::FaradayMiddleware::RESTAPICallBenchmark
67
+
68
+ if defined?(Patron)
69
+ faraday.adapter :patron # Prefer patron if exists
70
+ else
71
+ faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
72
+ end
73
+ end
74
+ end
75
+ end
76
+
77
+ def self.api_client_token
78
+ @@api_client_token ||= begin
79
+
80
+ end
81
+ end
82
+
83
+ end
84
+
85
+ end
@@ -0,0 +1,75 @@
1
+ require "active_support/configurable"
2
+ require 'i18n/core_ext/hash'
3
+ require 'yaml'
4
+ require 'erb'
5
+
6
+ module NContent
7
+ module SDK
8
+
9
+ class Config
10
+
11
+ include ActiveSupport::Configurable
12
+
13
+ config_accessor :api_host do
14
+ ENV['NCONTENT_API_HOST'] || ENV['NARANYA_ECM_SITE'] || 'http://proy-cms-1.herokuapp.com'
15
+ end
16
+
17
+ config_accessor :api_key do
18
+ ENV['NCONTENT_API_KEY'] || ENV['NARANYA_ECM_API_KEY']
19
+ end
20
+
21
+ config_accessor :api_secret do
22
+ ENV['NCONTENT_API_SECRET'] || ENV['NARANYA_ECM_API_SECRET']
23
+ end
24
+
25
+ # API Call Timeout in seconds:
26
+ config_accessor :api_call_timeout do
27
+ ENV.fetch("NCONTENT_API_CALL_TIMEOUT", '30').to_i
28
+ end
29
+
30
+ # API Call Open Timeout in seconds:
31
+ config_accessor :api_call_open_timeout do
32
+ ENV.fetch("NCONTENT_API_CALL_OPEN_TIMEOUT", '5').to_i
33
+ end
34
+
35
+ config_accessor :bucket_provider do
36
+ ENV['NCONTENT_BUCKET_PROVIDER'] || ENV['NARANYA_ECM_STORAGE_PROVIDER'] || 'AWS'
37
+ end
38
+
39
+ # (AWS) Bucket name
40
+ config_accessor :bucket_name do
41
+ ENV['NCONTENT_BUCKET_NAME'] || ENV['NARANYA_ECM_STORAGE_DIR']
42
+ end
43
+
44
+ config_accessor :bucket_api_key do
45
+ ENV['NCONTENT_BUCKET_API_KEY'] || ENV['NARANYA_ECM_STORAGE_KEY']
46
+ end
47
+
48
+ config_accessor :bucket_api_secret do
49
+ ENV['NCONTENT_BUCKET_API_SECRET'] || ENV['NARANYA_ECM_STORAGE_SECRET']
50
+ end
51
+
52
+ config_accessor(:media_profiles) { {} }
53
+
54
+ config_accessor (:extra_attributes) { {} }
55
+
56
+ config_accessor(:cache) { :memory_store }
57
+
58
+ config_accessor :client_notify_url do
59
+ 'http://www.example.com/ncontent/notifications'
60
+ end
61
+
62
+ config_accessor(:notification_processing_modules) { [] }
63
+
64
+ def self.parse_yaml(yaml_path)
65
+ YAML.load(ERB.new(File.read(yaml_path)).result) if File.exist?(yaml_path)
66
+ end
67
+
68
+ delegate :parse_yaml, to: :class
69
+
70
+ protected
71
+
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,4 @@
1
+
2
+ require "ncontent/sdk/faraday_middleware/required_response_format"
3
+ require "ncontent/sdk/faraday_middleware/response_parser"
4
+ require "ncontent/sdk/faraday_middleware/rest_api_call_benchmark"
@@ -0,0 +1,14 @@
1
+ module NContent
2
+ module SDK
3
+ module FaradayMiddleware
4
+
5
+ # Adds the required response format to the request parameters, in order for
6
+ # the SDK to work:
7
+ class RequiredResponseFormat < Faraday::Middleware
8
+ def call(env)
9
+ @app.call(env)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,35 @@
1
+ require "benchmark"
2
+
3
+ module NContent
4
+ module SDK
5
+ module FaradayMiddleware
6
+ # Parses the responses to models:
7
+ class ResponseParser < Faraday::Middleware
8
+
9
+ def call(request_env)
10
+ # do something with the request
11
+ # request_env[:request_headers].merge!(...)
12
+
13
+ @app.call(request_env).on_complete do |response_env|
14
+
15
+ unless request_env.url.path =~ /\A\/oauth/i ||
16
+ response_env.response_headers["Content-Type"] !~ /\Aapplication\/json/i ||
17
+ response_env.body.strip.empty?
18
+
19
+ benchmark = Benchmark.measure "api_response_parsing" do
20
+ # replace body with parsed_data:
21
+ response_env[:body] = ActiveSupport::JSON.decode(
22
+ response_env[:body]
23
+ )
24
+ end
25
+
26
+ response_env[:ncontent_api_response_parsing_tms] = benchmark
27
+
28
+ end
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,49 @@
1
+ require "benchmark"
2
+
3
+ module NContent
4
+ module SDK
5
+ module FaradayMiddleware
6
+ # Parses the responses to models:
7
+ class RESTAPICallBenchmark < Faraday::Middleware
8
+
9
+ def call(request_env)
10
+
11
+ # Get the start times:
12
+ t0 = Process.times
13
+ r0 = if defined?(Benchmark::BENCHMARK_CLOCK)
14
+ Process.clock_gettime(Benchmark::BENCHMARK_CLOCK)
15
+ else
16
+ Time.now
17
+ end
18
+
19
+ @app.call(request_env).on_complete do |response_env|
20
+
21
+ response_env[:ncontent_rest_api_response_datetime] = Time.parse(
22
+ response_env.response_headers["Date"]
23
+ ) if response_env.response_headers.key? "Date"
24
+
25
+ # Get the finish times:
26
+ t1 = Process.times
27
+ r1 = if defined?(Benchmark::BENCHMARK_CLOCK)
28
+ Process.clock_gettime(Benchmark::BENCHMARK_CLOCK)
29
+ else
30
+ Time.now
31
+ end
32
+
33
+ response_env[:ncontent_rest_api_calling_tms] = ::Benchmark::Tms.new(
34
+ t1.utime - t0.utime,
35
+ t1.stime - t0.stime,
36
+ t1.cutime - t0.cutime,
37
+ t1.cstime - t0.cstime,
38
+ r1 - r0,
39
+ "api_calling"
40
+ )
41
+
42
+
43
+ end
44
+ end
45
+
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,59 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module NContent
4
+ module SDK
5
+
6
+ class Railtie < Rails::Railtie
7
+
8
+ def self.app_name
9
+ Rails.application.railtie_name.sub(/_application$/, '')
10
+ end
11
+
12
+ initializer "ncontent.configure_rails" do
13
+
14
+ config_from_yaml = NContent::SDK.config.parse_yaml(
15
+ Rails.root.join('config/ncontent.yml').to_s
16
+ ) || NContent::SDK.config.parse_yaml(
17
+ Rails.root.join('config/naranya_ecm.yml').to_s
18
+ ) || {}
19
+
20
+ if config_from_yaml.key? Rails.env
21
+ config_from_yaml = config_from_yaml[Rails.env]
22
+ end
23
+
24
+ # config from whatever was parsed or not:
25
+ NContent::SDK.configure config_from_yaml
26
+
27
+ # Setup the logger:
28
+ NContent::SDK.logger = Rails.logger
29
+
30
+ # Setup the cache:
31
+ NContent::SDK.cache = Rails.cache
32
+
33
+ end
34
+
35
+ #initializer "cequel.add_new_relic" do
36
+ # if configuration.fetch(:newrelic_enabled, true)
37
+ # begin
38
+ # require 'new_relic/agent/method_tracer'
39
+ # rescue LoadError => e
40
+ # Rails.logger.debug(
41
+ # "New Relic not installed; skipping New Relic integration")
42
+ # else
43
+ # require 'cequel/metal/new_relic_instrumentation'
44
+ # end
45
+ # end
46
+ #end
47
+
48
+ #rake_tasks do
49
+ # require "cequel/record/tasks"
50
+ #end
51
+
52
+ #generators do
53
+ # require 'cequel/record/configuration_generator'
54
+ # require 'cequel/record/record_generator'
55
+ #end
56
+
57
+ end
58
+ end
59
+ end