naranya_ecm-sdk 0.0.17 → 0.0.18

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cb455d97321126300a191b589db3204014254115
4
- data.tar.gz: 912aa46d7321a71136aa42568927e0749715a955
3
+ metadata.gz: 32f1bd7a2e3f7b53d53c6468361ed0e9a1dd3f7a
4
+ data.tar.gz: 7b073faaa781f9d97fe1b7992d45980696973a9a
5
5
  SHA512:
6
- metadata.gz: 1c2b38a2edaa91ee6782e19843eba65f3809014c176daa18f495058f2210894277300f6ab9e7aacd98c1781a4e2fa616a84242dfc209084b09ee4bfcb5d0a20a
7
- data.tar.gz: a4f1363812dc63eceb161577aa450a6dcef942a1badb92043790a24c28bc4a5715920b87398f4bf83d78c3bf7b1475effbfb631e52667d5cae38f6f4549fa673
6
+ metadata.gz: 83ac78eb692f78afe2af0edaacbf78c5af3aff392170d4f8a11ff1759509ef3881434c5d5661aa1bdea22e9a1a01b28921d34b02e04af8441bc87c60fdcf8d56
7
+ data.tar.gz: f4fc65d122c667b529ee78c5b961ecab1a478f8c4bdda01f1c1bc0405614bfbb0752844d906b96f90511e8b488516a2fffec28203fbcfc748ec62b1e380454b3
@@ -0,0 +1,28 @@
1
+ = NaranyaEcm
2
+
3
+ TODO: Write a gem description
4
+
5
+ == Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'naranya_ecm-sdk'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install naranya_ecm
18
+
19
+ == Development: Running on console:
20
+
21
+ $ bundle console
22
+
23
+ == Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ == Contributing
28
+
@@ -20,6 +20,7 @@ module NaranyaEcm
20
20
  module Rest
21
21
  extend ActiveSupport::Autoload
22
22
  autoload :Client
23
+ autoload :Errors
23
24
  autoload :Persistence
24
25
  autoload :Associations
25
26
  autoload :Model
@@ -58,6 +59,7 @@ module NaranyaEcm
58
59
 
59
60
  DEFAULT_CONFIG = {
60
61
  site: "http://ecm.naranya.net:5000",
62
+ cache_store: []
61
63
  }.freeze
62
64
 
63
65
  class << self
@@ -79,6 +81,11 @@ module NaranyaEcm
79
81
  config
80
82
  end
81
83
 
84
+ def cache
85
+ auto_conf unless config.present?
86
+ @cache ||= ActiveSupport::Cache.lookup_store *options[:cache]
87
+ end
88
+
82
89
  def load_config_from_yml(yml_path, env="production")
83
90
  raise "YAML file doesn't extst" unless File.exist?(yml_path)
84
91
  yml_config = DEFAULT_CONFIG.merge(YAML::load(ERB.new(File.read(yml_path)).result)[env])
@@ -93,6 +100,9 @@ module NaranyaEcm
93
100
  if defined?(Rails)
94
101
  yml_path = File.join(Rails.root, 'config', 'naranya_ecm.yml')
95
102
  load_config_from_yml(yml_path, Rails.env)
103
+
104
+ # Use the Rails cache:
105
+ @cache = Rails.cache
96
106
  elsif File.exists?('./naranya_ecm.yml')
97
107
  load_config_from_yml('./naranya_ecm.yml')
98
108
  end
@@ -1,3 +1,3 @@
1
1
  module NaranyaEcm
2
- VERSION = "0.0.17"
2
+ VERSION = "0.0.18"
3
3
  end
@@ -1,4 +1,60 @@
1
+ require 'httparty'
2
+ require 'naranya_ecm/rest/errors'
3
+ require 'active_support/core_ext/hash/indifferent_access.rb'
4
+
1
5
  module NaranyaEcm::Rest
2
6
  class Client
7
+
8
+ TOKEN_CACHE_KEY = 'naranya_ecm/client/client_token'
9
+
10
+ class << self
11
+
12
+ %w(get post put patch delete).map(&:to_sym).each do |method_symbol|
13
+ define_method method_symbol do |path, options = {}|
14
+ execute_request method_symbol, path, options
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def execute_request(method, path, options = {})
21
+ url = "#{NaranyaEcm.options[:site]}#{path}"
22
+
23
+ options[:headers] = {} unless options.has_key? :headers
24
+ options[:headers]['Authorization'] = authorization_header
25
+
26
+ response = HTTParty.send method, url, options
27
+
28
+ # Retry if response code is 422 and a client token is present:
29
+ if response.code == 401 && authorization_header.present?
30
+ options[:headers]['Authorization'] = authorization_header(true)
31
+ response = HTTParty.send method, url, options
32
+ end
33
+ ::NaranyaEcm::Rest::RestError.raise_by_code(response.code) unless response.success?
34
+
35
+ response
36
+ end
37
+
38
+ # https://github.com/applicake/doorkeeper/wiki/Client-Credentials-flow
39
+ def authorization_header(clear = false)
40
+
41
+ if clear
42
+ @authorization_header = nil
43
+ NaranyaEcm.cache.delete TOKEN_CACHE_KEY
44
+ end
45
+
46
+ @authorization_header ||= NaranyaEcm.cache.fetch(TOKEN_CACHE_KEY) do
47
+ token_response = HTTParty.post "#{NaranyaEcm.options[:site]}/oauth/token",
48
+ basic_auth: { username: NaranyaEcm.options[:api_key], password: NaranyaEcm.options[:api_secret] },
49
+ body: { grant_type: "client_credentials" }
50
+
51
+ ::NaranyaEcm::Rest::RestError.raise_by_code(token_response.code) unless token_response.success?
52
+
53
+ "Bearer #{token_response['access_token']}"
54
+ end
55
+ end
56
+
57
+ end
58
+
3
59
  end
4
60
  end
@@ -3,6 +3,18 @@ module NaranyaEcm::Rest
3
3
  #
4
4
  # Generic REST exception class.
5
5
  class RestError < StandardError
6
+
7
+ def self.raise_by_code(code, message = "")
8
+ klass = case code
9
+ when 401 then AuthorizationRequired
10
+ when 403 then Forbidden
11
+ when 404 then ResourceNotFound
12
+ when 422 then UnprocessableEntity
13
+ else RemoteFailed
14
+ end
15
+ raise klass, "Server responded with code #{code}. Message: #{message}"
16
+ end
17
+
6
18
  end
7
19
 
8
20
  # Raised when an object assigned to an association has an incorrect type.
@@ -35,19 +47,22 @@ module NaranyaEcm::Rest
35
47
 
36
48
  # Raised when connection to the database could not been established (for example when <tt>connection=</tt>
37
49
  # is given a nil object).
38
- class ConnectionNotEstablished < RestError
39
- end
50
+ class ConnectionNotEstablished < RestError; end
40
51
 
41
52
  # Raised when REST cannot find record by given id or set of ids.
42
- class ResourceNotFound < RestError
43
- end
53
+ class ResourceNotFound < RestError; end
44
54
 
45
55
  # Raised by ActiveRecord::Base.save! and ActiveRecord::Base.create! methods when record cannot be
46
56
  # saved because record is invalid.
47
- class ResourceNotSaved < RestError
48
- end
57
+ class ResourceNotSaved < RestError; end
49
58
 
50
59
  # Raised by ActiveRecord::Base.destroy! when a call to destroy would return false.
51
- class ResourceNotDestroyed < RestError
52
- end
60
+ class ResourceNotDestroyed < RestError; end
61
+
62
+ class AuthorizationRequired < RestError; end
63
+
64
+ class Forbidden < RestError; end
65
+ class UnprocessableEntity < RestError; end
66
+ class RemoteFailed < RestError; end
67
+
53
68
  end
@@ -1,4 +1,6 @@
1
1
  require 'naranya_ecm/rest/errors'
2
+ require 'naranya_ecm/rest/client'
3
+
2
4
  module NaranyaEcm::Rest
3
5
 
4
6
  module FinderMethods
@@ -22,15 +24,7 @@ module NaranyaEcm::Rest
22
24
 
23
25
  def fetch_one(id)
24
26
  format = NaranyaEcm.options[:format] || 'json'
25
-
26
- response = self.get("#{path}/#{id}.#{format}")
27
-
28
- case response.code
29
- when 404
30
- raise ResourceNotFound
31
- when 200
32
- response.to_hash
33
- end
27
+ Client.get("#{path}/#{id}.#{format}").to_hash
34
28
  end
35
29
  end
36
30
  end
@@ -62,10 +62,6 @@ module NaranyaEcm::Rest
62
62
  # Ignore the server resource's url attribute:
63
63
  attr_accessor :url
64
64
 
65
- include HTTParty
66
- base_uri NaranyaEcm.options[:site]
67
- #debug_output $stderr
68
-
69
65
  include NaranyaEcm::Rest::Associations
70
66
  include NaranyaEcm::Rest::Persistence
71
67
 
@@ -5,7 +5,7 @@ module NaranyaEcm::Rest
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  included do
8
- define_model_callbacks :create, :update, :save
8
+ define_model_callbacks :create, :update, :save, :destroy
9
9
  end
10
10
 
11
11
  def save
@@ -17,6 +17,16 @@ module NaranyaEcm::Rest
17
17
  end
18
18
  end
19
19
 
20
+ def destroy
21
+ run_callbacks :destroy do
22
+ #raise ReadOnlyRecord if readonly?
23
+ #destroy_associations
24
+ self.class.delete(self.id) if persisted?
25
+ @destroyed = true
26
+ freeze
27
+ end
28
+ end
29
+
20
30
  def new_resource?; self.id.nil?; end
21
31
  alias_method :new?, :new_resource?
22
32
 
@@ -39,15 +49,10 @@ module NaranyaEcm::Rest
39
49
 
40
50
  def reload
41
51
  format = NaranyaEcm.options[:format] || 'json'
42
- get_path = "#{path}.#{format}"
43
- response = self.class.get get_path
44
- case response.code
45
- when 200
46
- load(response.to_hash)
47
- true
48
- else
49
- false
50
- end
52
+ response = Client.get "#{path}.#{format}"
53
+
54
+ load(response.to_hash)
55
+ true
51
56
  end
52
57
 
53
58
  def reload!; raise "Ja!" unless reload; end
@@ -65,13 +70,11 @@ module NaranyaEcm::Rest
65
70
  format = NaranyaEcm.options[:format] || 'json'
66
71
  create_path = "#{self.class.path}.#{format}"
67
72
 
68
- response = self.class.post create_path, persist_request_options(self.attributes)
69
-
70
- case response.code
71
- when 201
73
+ begin
74
+ response = Client.post create_path, persist_request_options(self.attributes)
72
75
  self.load(response.to_hash)
73
76
  true
74
- when 422 # Unprocessable Entity
77
+ rescue UnprocessableEntity
75
78
  self.load_server_errors(response.to_hash)
76
79
  false
77
80
  end
@@ -90,13 +93,12 @@ module NaranyaEcm::Rest
90
93
  format = NaranyaEcm.options[:format] || 'json'
91
94
  update_path = "#{self.path}.#{format}"
92
95
 
93
- response = self.class.put update_path, persist_request_options(attributes_to_save)
94
- case response.code
95
- when 204
96
+ begin
97
+ response = Client.put update_path, persist_request_options(attributes_to_save)
96
98
  self.reload!
97
99
  off_band_changes.clear if off_band_changes.present?
98
100
  true
99
- when 422 # Unprocessable Entity
101
+ rescue UnprocessableEntity
100
102
  self.load_server_errors(response.to_hash)
101
103
  false
102
104
  end
@@ -119,13 +121,8 @@ module NaranyaEcm::Rest
119
121
  end
120
122
 
121
123
  def delete(id, options={})
122
- response = HTTParty.delete("#{NaranyaEcm.options[:site]}#{self.path}/#{id}.json")
123
- case response.code
124
- when 204
125
- true
126
- else
127
- false
128
- end
124
+ response = Client.delete("#{self.path}/#{id}.json")
125
+ true
129
126
  end
130
127
 
131
128
  end
@@ -1,3 +1,6 @@
1
+ require 'naranya_ecm/rest/errors'
2
+ require 'naranya_ecm/rest/client'
3
+
1
4
  module NaranyaEcm::Rest
2
5
  class Relation
3
6
  include Enumerable
@@ -24,13 +27,7 @@ module NaranyaEcm::Rest
24
27
 
25
28
  def fetch_from_server
26
29
  format = NaranyaEcm.options[:format] || 'json'
27
- response = klass.get("#{klass.path}.#{format}", query: conditions)
28
- case response.code
29
- when 404
30
- raise ResourceNotFound
31
- when 200
32
- response.to_a
33
- end
30
+ Client.get("#{klass.path}.#{format}", query: conditions).to_a
34
31
  end
35
32
 
36
33
  def load(given_list)
@@ -1,11 +1,9 @@
1
1
  require 'naranya_ecm/rest/relation'
2
+ require 'naranya_ecm/rest/client'
2
3
 
3
4
  module NaranyaEcm::Search
4
5
  class Results < NaranyaEcm::Rest::Relation
5
6
 
6
- include HTTParty
7
- base_uri NaranyaEcm.options[:site]
8
-
9
7
  DEFAULT_MATERIALIZER = Proc.new do |search_results|
10
8
 
11
9
  # Duplicate the hits array - we'll replace each element with the
@@ -106,13 +104,10 @@ module NaranyaEcm::Search
106
104
  search_path = klass.present? ? klass.path : ''
107
105
  search_path += '/_search'
108
106
 
109
- # Use the class connection to get the search results,
110
- # using any authentication / headers configured:
111
- #search_response = connection.get(search_path, headers)
112
107
  search_body = ActiveSupport::JSON.encode(conditions)
113
- search_response = self.class.get search_path, body: search_body
108
+ search_response = NaranyaEcm::Rest::Client.get search_path, body: search_body
114
109
 
115
- puts "curl -XGET '#{NaranyaEcm.options[:site]}#{search_path}?pretty=true' -d '#{search_body}'"
110
+ # puts "curl -XGET '#{NaranyaEcm.options[:site]}#{search_path}?pretty=true' -d '#{search_body}'"
116
111
 
117
112
  # Parse the search results:
118
113
  @facts = search_response.to_hash.with_indifferent_access
metadata CHANGED
@@ -1,153 +1,153 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: naranya_ecm-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.17
4
+ version: 0.0.18
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-04-03 00:00:00.000000000 Z
11
+ date: 2014-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 4.0.4
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.0.4
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activemodel
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 4.0.4
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 4.0.4
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: httparty
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
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
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: state_machine
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: fog
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: bundler
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ~>
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
89
  version: '1.5'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ~>
94
+ - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: '1.5'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rake
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '>='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - '>='
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: rspec
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - ~>
115
+ - - "~>"
116
116
  - !ruby/object:Gem::Version
117
117
  version: 3.0.0.beta2
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - ~>
122
+ - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: 3.0.0.beta2
125
125
  - !ruby/object:Gem::Dependency
126
126
  name: vcr
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
- - - '>='
129
+ - - ">="
130
130
  - !ruby/object:Gem::Version
131
131
  version: '0'
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
- - - '>='
136
+ - - ">="
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0'
139
139
  - !ruby/object:Gem::Dependency
140
140
  name: webmock
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
- - - ~>
143
+ - - "~>"
144
144
  - !ruby/object:Gem::Version
145
145
  version: '1.16'
146
146
  type: :development
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
- - - ~>
150
+ - - "~>"
151
151
  - !ruby/object:Gem::Version
152
152
  version: '1.16'
153
153
  description: Cliente Ruby de NaranyaEcm
@@ -157,10 +157,10 @@ executables: []
157
157
  extensions: []
158
158
  extra_rdoc_files: []
159
159
  files:
160
- - .gitignore
160
+ - ".gitignore"
161
161
  - Gemfile
162
162
  - LICENSE.txt
163
- - README.md
163
+ - README.rdoc
164
164
  - Rakefile
165
165
  - dev/create_cvm.rb
166
166
  - dev/init.rb
@@ -207,17 +207,17 @@ require_paths:
207
207
  - lib
208
208
  required_ruby_version: !ruby/object:Gem::Requirement
209
209
  requirements:
210
- - - '>='
210
+ - - ">="
211
211
  - !ruby/object:Gem::Version
212
212
  version: '0'
213
213
  required_rubygems_version: !ruby/object:Gem::Requirement
214
214
  requirements:
215
- - - '>='
215
+ - - ">="
216
216
  - !ruby/object:Gem::Version
217
217
  version: '0'
218
218
  requirements: []
219
219
  rubyforge_project:
220
- rubygems_version: 2.0.14
220
+ rubygems_version: 2.2.2
221
221
  signing_key:
222
222
  specification_version: 4
223
223
  summary: Cliente Ruby de NaranyaEcm
data/README.md DELETED
@@ -1,33 +0,0 @@
1
- # NaranyaEcm
2
-
3
- TODO: Write a gem description
4
-
5
- ## Installation
6
-
7
- Add this line to your application's Gemfile:
8
-
9
- gem 'naranya_ecm'
10
-
11
- And then execute:
12
-
13
- $ bundle
14
-
15
- Or install it yourself as:
16
-
17
- $ gem install naranya_ecm
18
-
19
- ## Development: Running on console:
20
-
21
- $ bundle console
22
-
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
- ## Contributing
28
-
29
- 1. Fork it ( http://github.com/<my-github-username>/naranya_ecm/fork )
30
- 2. Create your feature branch (`git checkout -b my-new-feature`)
31
- 3. Commit your changes (`git commit -am 'Add some feature'`)
32
- 4. Push to the branch (`git push origin my-new-feature`)
33
- 5. Create new Pull Request