accounts_client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,29 @@
1
+ # wrapper for PADMA-Accounts Account API interaction
2
+ # Configuration for LogicalModel on /config/initializers/logical_model.rb
3
+ class PadmaAccount < LogicalModel
4
+ self.hydra = Accounts::HYDRA
5
+ self.use_ssl = (Rails.env=="production")
6
+
7
+ self.resource_path = "/v0/accounts"
8
+ self.attribute_keys = [:id, :name, :enabled ]
9
+ self.use_api_key = true
10
+ self.api_key_name = "token"
11
+ self.api_key = Accounts::API_KEY
12
+ self.host = Accounts::HOST
13
+
14
+ TIMEOUT = 5500 # milisecons
15
+ PER_PAGE = 9999
16
+
17
+ def enabled?
18
+ self.enabled
19
+ end
20
+
21
+ def users
22
+ PadmaUser.paginate :params => { :account_name => self.name }
23
+ end
24
+
25
+ def contacts
26
+ PadmaContact.paginate :params => { :account_name => self.name }
27
+ end
28
+
29
+ end
@@ -0,0 +1,36 @@
1
+ class PadmaUser < LogicalModel
2
+ self.hydra = Accounts::HYDRA
3
+ self.use_ssl = (Rails.env=="production")
4
+
5
+ self.resource_path = "/v0/users"
6
+ self.attribute_keys = [:username, :drc_login, :email, :locale, :accounts, :roles, :verbose_help ] # drc_login is OBSOLETE. remove.
7
+ self.use_api_key = true
8
+ self.api_key_name = "token"
9
+ self.api_key = Accounts::API_KEY
10
+ self.host = Accounts::HOST
11
+
12
+ TIMEOUT = 5500 # milisecons
13
+ PER_PAGE = 9999
14
+
15
+ # LogicalModel expects an id to create resource_uri
16
+ def id
17
+ self.username
18
+ end
19
+
20
+ # Returns my accounts as Padma::Account objects
21
+ # @return [Array <PadmaAccount>]
22
+ def padma_accounts
23
+ self.accounts.map{|a|PadmaAccount.new(a)}
24
+ end
25
+
26
+ # Returns me enabled accounts as Padma::Account objects
27
+ # @return [Array <PadmaAccount>] enabled accounts
28
+ def enabled_accounts
29
+ return [] if self.accounts.nil?
30
+ self.accounts.reject{|a|!a['enabled']}.map{|a|PadmaAccount.new(a)}
31
+ end
32
+
33
+ def verbose_help?
34
+ !!self.verbose_help
35
+ end
36
+ end
@@ -0,0 +1,17 @@
1
+ if(defined?(Rails))
2
+ module Accounts
3
+
4
+ unless defined? HYDRA
5
+ HYDRA = Typhoeus::Hydra.new
6
+ end
7
+
8
+ HOST = case Rails.env
9
+ when "production"
10
+ "padma-accounts.heroku.com"
11
+ when "development"
12
+ "localhost:3001"
13
+ when "test"
14
+ "localhost:3001"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,52 @@
1
+ # this module assumes base class has aa account_name attribute.
2
+ module Accounts
3
+ module BelongsToAccount
4
+
5
+ def self.included(base)
6
+ base.send(:validate, :padma_account_setted_correctly)
7
+ end
8
+
9
+ attr_accessor :padma_account
10
+ ##
11
+ # Returns associated account.
12
+ #
13
+ # account is stored in instance variable padma_account. This allows for it to be setted in a Mass-Load.
14
+ #
15
+ # @param options [Hash]
16
+ # @option options [TrueClass] decorated - returns decorated account
17
+ # @option options [TrueClass] force_service_call - forces call to accounts-ws
18
+ # @return [PadmaAccount / PadmaAccountDecorator]
19
+ def account(options={})
20
+ if self.padma_account.nil? || options[:force_service_call]
21
+ self.padma_account = PadmaAccount.find(account_name)
22
+ end
23
+ ret = padma_account
24
+ if options[:decorated] && padma_account
25
+ ret = PadmaAccountDecorator.decorate(padma_account)
26
+ end
27
+ ret
28
+ end
29
+
30
+ private
31
+
32
+ # If padma_account is setted with a PadmaAccount that doesn't match
33
+ # account_id an exception will be raised
34
+ # @raises 'This is the wrong account!'
35
+ # @raises 'This is not a account!'
36
+ def padma_account_setted_correctly
37
+ return if self.padma_account.nil?
38
+ unless padma_account.is_a?(PadmaAccount)
39
+ raise 'This is not a account!'
40
+ end
41
+ if padma_account.name != self.account_name
42
+ if self.account_name.nil?
43
+ # if they differ because account_id is nil we set it here
44
+ self.account_name = self.padma_account.name
45
+ else
46
+ raise 'This is the wrong account!'
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+
@@ -0,0 +1,54 @@
1
+ # this module assumes base class has a username attribute.
2
+ #
3
+ # Including this module adds Class#user to access PadmaUser stored in memory in Class#padma_user
4
+ module Accounts
5
+ module BelongsToUser
6
+
7
+ def self.included(base)
8
+ base.send(:validate, :padma_user_setted_correctly)
9
+ end
10
+
11
+ attr_accessor :padma_user
12
+ ##
13
+ # Returns associated user.
14
+ #
15
+ # user is stored in instance variable padma_user. This allows for it to be setted in a Mass-Load.
16
+ #
17
+ # @param options [Hash]
18
+ # @option options [TrueClass] decorated - returns decorated user
19
+ # @option options [TrueClass] force_service_call - forces call to users-ws
20
+ # @return [PadmaUser / PadmaUserDecorator]
21
+ def user(options={})
22
+ if self.padma_user.nil? || options[:force_service_call]
23
+ self.padma_user = PadmaUser.find(username)
24
+ end
25
+ # TODO cache
26
+ ret = padma_user
27
+ if options[:decorated] && padma_user
28
+ ret = PadmaUserDecorator.decorate(padma_user)
29
+ end
30
+ ret
31
+ end
32
+
33
+ private
34
+
35
+ # If padma_user is setted with a PadmaUser that doesn't match
36
+ # Class#username an exception will be raised
37
+ # @raises 'This is the wrong user!'
38
+ # @raises 'This is not a user!'
39
+ def padma_user_setted_correctly
40
+ return if self.padma_user.nil?
41
+ unless padma_user.is_a?(PadmaUser)
42
+ raise 'This is not a user!'
43
+ end
44
+ if padma_user.username != self.username
45
+ if self.username.nil?
46
+ # if they differ because user_id is nil we set it here
47
+ self.username = self.padma_user.username
48
+ else
49
+ raise 'This is the wrong user!'
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,55 @@
1
+ module Accounts
2
+ module IsAUser
3
+
4
+ def self.included(base)
5
+ base.send(:validate, :has_access_to_current_account)
6
+ base.send(:include, Accounts::BelongsToUser)
7
+ base.send(:include, Accounts::BelongsToAccount)
8
+ base.send(:delegate, :email, to: :padma_user)
9
+ base.send(:delegate, :verbose_help?, to: :padma_user)
10
+ end
11
+
12
+ def padma(cache=true)
13
+ self.user(force_service_call: !cache)
14
+ end
15
+
16
+ # Returns true if this user
17
+ # has enabled padma accounts
18
+ # @return [TrueClass]
19
+ def padma_enabled?
20
+ ea = self.enabled_accounts
21
+ !(ea.nil? || ea.empty?)
22
+ end
23
+
24
+ # Returns locale for this user retrieving it from PADMA ACCOUNTS
25
+ # @return [String] locale
26
+ def locale
27
+ self.padma.try :locale
28
+ end
29
+
30
+ # Returns enabled accounts associated with this user from PADMA ACCOUNTS
31
+ #
32
+ # @return [Array <PadmaAccount>]
33
+ def enabled_accounts
34
+ self.padma.try(:enabled_accounts)
35
+ end
36
+
37
+ private
38
+
39
+ # Validates that current_account_name
40
+ # checking that user belongs to such account
41
+ def has_access_to_current_account
42
+ return if self.current_account.nil? || !self.current_account_id_changed?
43
+
44
+ if self.enabled_accounts.nil?
45
+ # nil means error in connection
46
+ self.errors.add(:current_account_id, I18n.t('user.couldnt_connect_to_check_current_account'))
47
+ else
48
+ unless self.current_account.name.in?(self.enabled_accounts.map{|ea|ea.name})
49
+ self.errors.add(:current_account_id, I18n.t('user.invalid_current_account'))
50
+ end
51
+ end
52
+
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,4 @@
1
+ module AccountsClient
2
+ class Engine < Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,7 @@
1
+ require 'logical_model'
2
+ if defined?(Rails)
3
+ require 'accounts/railties'
4
+ require 'accounts/is_a_user'
5
+ require 'accounts/belongs_to_account'
6
+ require 'accounts/belongs_to_user'
7
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: accounts_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dwayne Macgowan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: railties
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.1'
30
+ - !ruby/object:Gem::Dependency
31
+ name: logical_model
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: This is client library for padma-Accounts-ws
47
+ email:
48
+ - dwaynemac@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - app/models/padma_user.rb
54
+ - app/models/padma_account.rb
55
+ - lib/accounts/belongs_to_account.rb
56
+ - lib/accounts/belongs_to_user.rb
57
+ - lib/accounts/is_a_user.rb
58
+ - lib/accounts/railties.rb
59
+ - lib/accounts_client.rb
60
+ - config/initializers/accounts_client.rb
61
+ homepage: ''
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 1.8.24
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Client library for padma-Accounts-ws
85
+ test_files: []
86
+ has_rdoc: