elephant-drive-service 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,39 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ require 'savon'
4
+
5
+ require_relative 'elephant-drive-service/result_extractor'
6
+ require_relative 'elephant-drive-service/message_builder'
7
+ require_relative 'elephant-drive-service/response_builder'
8
+ require_relative 'elephant-drive-service/config'
9
+ require_relative 'elephant-drive-service/base_service'
10
+ require_relative 'elephant-drive-service/provisioning_service'
11
+ require_relative 'elephant-drive-service/report_service'
12
+
13
+ module ElephantDriveService
14
+ extend self
15
+
16
+ def ws_config
17
+ @ws_config
18
+ end
19
+
20
+ def ws_config= config
21
+ @ws_config = config
22
+ end
23
+
24
+ def user_portal_domain
25
+ "https://#{@domain || 'locaweb.vaultservices.net'}"
26
+ end
27
+
28
+ def user_portal_domain= domain
29
+ @domain = domain
30
+ end
31
+
32
+ def change_password_path token
33
+ "/account/change_password.aspx?id=#{token}"
34
+ end
35
+
36
+ def self.configure
37
+ yield self
38
+ end
39
+ end
@@ -0,0 +1,44 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module ElephantDrive
4
+ class BaseService
5
+ include MessageBuilder
6
+ include ResponseBuilder
7
+
8
+ def initialize custom_config=nil
9
+ @config = custom_config ? custom_config : ElephantDrive::ServiceConfig.new(ElephantDriveService.ws_config)
10
+ full_config = @config.to_hash
11
+ full_config = full_config.merge(proxy: ENV['http_proxy']) if ENV && ENV['http_proxy']
12
+ @client = Savon.client(full_config)
13
+ end
14
+
15
+ private
16
+ def partner_login_message
17
+ {ac: {user: @config.username, pass: @config.hashed_password, salt: Time.now.strftime('%Y/%m/%d %H:%M:%S'), code: { g_product_code: @config.product_code, version: @config.version}}}
18
+ end
19
+
20
+ def execute! action_name
21
+ extractor = execute(action_name) do
22
+ { message: yield }
23
+ end
24
+ raise extractor[:method_detail] unless extractor[:method_result] == 'Success'
25
+ extractor
26
+ end
27
+
28
+ def execute action
29
+ authenticate_and_go do |session_id|
30
+ call_params = yield
31
+ call_params[:message] = { 'gSessionID' => session_id }.merge(call_params[:message])
32
+ ElephantDrive::ResultExtractor.new(action, @client.call(action, call_params))
33
+ end
34
+ end
35
+
36
+ def authenticate_and_go
37
+ raise ArgumentError, 'a block is needed for post actions' unless block_given?
38
+
39
+ extractor = ElephantDrive::ResultExtractor.new(:partner_login, @client.call(:partner_login, message: partner_login_message) )
40
+ raise 'not authorized' unless extractor[:method_result] == 'Success'
41
+ yield extractor[:g_session_id]
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module ElephantDrive
4
+ class ServiceConfig
5
+ def initialize yaml
6
+ yaml[:ws].each do |key,value|
7
+ var_name = "@#{key.to_s}"
8
+ self.instance_variable_set(var_name,value)
9
+ self.class.class_eval { attr_reader key }
10
+ end
11
+ end
12
+
13
+ def to_hash
14
+ Hash[instance_variables.map { |var| [var[1..-1].to_sym, instance_variable_get(var)] }].select { |k,v| [:wsdl, :log_level, :log, :ssl_verify_mode].include? k }
15
+ end
16
+
17
+ def hashed_password
18
+ elephant_hash(password)
19
+ end
20
+
21
+ private
22
+ def elephant_hash(pw, hash=true)
23
+ return pw if pw.empty?
24
+ pw = Digest::MD5.hexdigest(pw) if hash
25
+ pw =~ /(..)(.*)/
26
+ $1.hex.to_s + elephant_hash($2, false)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,103 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module ElephantDrive
4
+ module MessageBuilder
5
+ def add_customer_message customer, campaign_id
6
+ {
7
+ 'strIP' => '127.0.0.1',
8
+ 'iCampID' => campaign_id,
9
+ 'strCompany' => '',
10
+ 'strEmail' => customer.email,
11
+ 'strDisplayName' => '',
12
+ 'strPswd' => '123inicial',
13
+ 'iSubType' => customer.plan.subscription_type_id,
14
+ 'strOrderId' => '',
15
+ 'strCustomTrack' => ''
16
+ }
17
+ end
18
+
19
+ def modify_subscription_message provisioning, new_plan
20
+ {
21
+ 'strIP' => '127.0.0.1',
22
+ 'gUserID' => provisioning.installed_user_id,
23
+ 'iSubType' => new_plan.subscription_type_id
24
+ }
25
+ end
26
+
27
+ def deactivate_customer_message installed_user_id
28
+ {
29
+ 'strIP' => '127.0.0.1',
30
+ 'gUserID' => installed_user_id,
31
+ 'asAcctState' => 'SuspendedbyAccountAdministrator',
32
+ 'strDesc' => ''
33
+ }
34
+ end
35
+
36
+ def reactivate_customer_message provisioning
37
+ {
38
+ 'gUserID' => provisioning.installed_user_id,
39
+ 'strEmail' => '',
40
+ 'strDisplayName' => '',
41
+ 'strIP' => '',
42
+ 'strDesc' => ''
43
+ }
44
+ end
45
+
46
+ def search_customer_message email
47
+ {
48
+ 'strSearchText' => email,
49
+ 'scrCriterion' => 'Exact',
50
+ 'scCategory' => 'Email',
51
+ }
52
+ end
53
+
54
+ def check_username_availability_message email
55
+ {
56
+ 'user' => email,
57
+ 'iType' => 1
58
+ }
59
+ end
60
+
61
+ def customer_list_message params
62
+ {
63
+ 'iPartnerID' => params[:partner_id],
64
+ 'dtStartReg' => params[:starting_registration_date],
65
+ 'dtEndReg' => params[:ending_registration_date],
66
+ 'iMaxRow' => params[:max_rows],
67
+ 'zSendEmail' => params[:send_email],
68
+ 'strEmail' => params[:email_to_send],
69
+ 'rf' => "csv",
70
+ 'iCampIDs' => {
71
+ 'int' => params[:campaign_id]
72
+ }
73
+ }
74
+ end
75
+
76
+ def base_user_message provisioning
77
+ {
78
+ 'gUserID' => provisioning.installed_user_id,
79
+ }
80
+ end
81
+
82
+ def reset_password_message installed_user_id
83
+ {
84
+ 'gUserID' => installed_user_id,
85
+ 'strIP' => '127.0.0.1'
86
+ }
87
+ end
88
+
89
+ def get_customer_list_by_reg_date_message partner_id, end_date
90
+ {
91
+ 'iPartnerID' => partner_id,
92
+ 'dtStartReg' => Time.utc(1970,1,01,0,0,0).iso8601,
93
+ 'dtEndReg' => end_date.iso8601,
94
+ 'iMaxRow' => -1,
95
+ 'zSendEmail' => false,
96
+ 'strEmail' => '',
97
+ 'rf' => 'csv',
98
+ 'iCampIDs' => { 'int' => 1010 }
99
+ }
100
+ end
101
+
102
+ end
103
+ end
@@ -0,0 +1,66 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module ElephantDrive
4
+ class ProvisioningService < BaseService
5
+
6
+ def add_a_customer customer
7
+ add_extractor = execute(:add_customer_with_partner_billing) {
8
+ { message: add_customer_message(customer, @config.campaign_id), soap_action: 'http://svc.elephantdrive.com/ese/AddCustomerWithPartnerBillingCustomTrack' }
9
+ }.ending_with(:custom_track)
10
+
11
+ generate_add_customer_response add_extractor, generate_reset_password_token(add_extractor[:g_user_id])
12
+ end
13
+
14
+ def modify_subscription provisioning, new_plan
15
+ execute! :modify_sub_with_partner_billing do
16
+ modify_subscription_message(provisioning, new_plan)
17
+ end
18
+ end
19
+
20
+ def deactivate_customer provisioning
21
+ execute! :deactivate_customer do
22
+ deactivate_customer_message provisioning.installed_user_id
23
+ end
24
+ end
25
+
26
+ def reactivate_customer provisioning
27
+ execute! :reactivate_customer do
28
+ reactivate_customer_message provisioning
29
+ end
30
+ end
31
+
32
+ def delete_customer provisioning
33
+ execute! :deactivate_customer do
34
+ deactivate_customer_message(provisioning.installed_user_id).merge('asAcctState' => 'CancelledbyAccountAdministrator')
35
+ end
36
+ end
37
+
38
+ def get_user_usage provisioning
39
+ extractor = execute :get_partner_user_usage do
40
+ { message: base_user_message(provisioning) }
41
+ end
42
+ raise extractor[:state] unless extractor[:state] == 'OK'
43
+ generate_usage_statistics extractor
44
+ end
45
+
46
+ def username_available? email
47
+ extractor = execute(:check_username_availability) do
48
+ { message: check_username_availability_message(email), soap_action: 'http://svc.elephantdrive.com/ese/SCheckUsernameAvailability' }
49
+ end
50
+ !extractor.starting_with(:s)[:result]
51
+ end
52
+
53
+ private
54
+ def generate_reset_password_token installed_user_id
55
+ execute!(:log_customer_reset_pswd_request) do
56
+ reset_password_message(installed_user_id)
57
+ end
58
+ rescue => e
59
+ execute! :deactivate_customer do
60
+ deactivate_customer_message(installed_user_id).merge('asAcctState' => 'CancelledbyAccountAdministrator')
61
+ end
62
+
63
+ raise e
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module ElephantDrive
4
+ class ReportService < BaseService
5
+
6
+ def average_usage_report snapshot_date
7
+ extractor = execute(:get_customer_list) {
8
+ { message: get_customer_list_by_reg_date_message(@config.partner_id,snapshot_date),
9
+ soap_action: "http://svc.elephantdrive.com/ese/GetCustomerListByRegDateAndMaxRow" }
10
+ }.ending_with(:by_reg_date_and_max_row)
11
+
12
+ raise extractor[:method_detail] unless extractor[:method_result] == 'Success'
13
+
14
+ generate_average_usage_report_response extractor
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,55 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module ElephantDrive
4
+ module ResponseBuilder
5
+ def generate_usage_statistics extractor
6
+ total_capacity = extractor[:l_capacity_bytes].to_f
7
+ total_usage = [extractor[:l_active_length].to_f,extractor[:l_version_length].to_f,extractor[:l_trash_length].to_f].inject(:+)
8
+
9
+ { total_capacity: total_capacity, total_usage: total_usage, usage_rate: total_usage/total_capacity }
10
+ end
11
+
12
+ def generate_add_customer_response add_extractor, password_change_extractor
13
+ { installed_user_id: add_extractor[:g_user_id], reset_password_url: ElephantDriveService.user_portal_domain + ElephantDriveService.change_password_path(password_change_extractor[:message]) }
14
+ end
15
+
16
+ def generate_average_usage_report_response extractor
17
+ {data: [],
18
+ total_capacity: 0.0,
19
+ total_active_usage: 0.0,
20
+ total_inactive_usage: 0.0,
21
+ total_usage_percentage: 0.0,
22
+ total_active_usage_percentage: 0.0,
23
+ total_inactive_usage_percentage: 0.0}.tap do |report|
24
+ [extractor[:ds][:diffgram].fetch(:new_data_set,{}).fetch(:table,[])].flatten.select { |h| h[:str_email] }.each do |user|
25
+ {
26
+ display_name: user[:str_display_name],
27
+ email: user[:str_email],
28
+ installed_user_id: user[:g_user_id],
29
+ status: user[:str_account_state],
30
+ total_capacity: user[:l_capacity_b].to_f,
31
+ total_usage: user[:l_usage_b].to_f || 0.0
32
+ }.tap { |entry|
33
+ entry[:usage_percentage] = entry[:total_usage] / entry[:total_capacity]
34
+
35
+ if entry[:status] =~ /^Suspended|Active/i
36
+ report[:total_capacity] += entry[:total_capacity]
37
+ case entry[:status]
38
+ when /^Active/i
39
+ report[:total_active_usage] += entry[:total_usage]
40
+ report[:total_active_usage_percentage] = entry[:total_usage] / entry[:total_capacity]
41
+ when /^Suspended/i
42
+ report[:total_inactive_usage] += entry[:total_usage]
43
+ report[:total_inactive_usage_percentage] = entry[:total_usage] / entry[:total_capacity]
44
+ end
45
+ end
46
+
47
+ report[:data] << entry
48
+ }
49
+ end
50
+
51
+ report[:total_usage_percentage] = ((report[:total_active_usage] + report[:total_inactive_usage]) / report[:total_capacity]).round(2) if report[:total_capacity] > 0.0
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,33 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module ElephantDrive
4
+ class ResultExtractor
5
+ def initialize action, response
6
+ @action = action
7
+ @response_name = "#{@action}_response".to_sym
8
+ @result_name = "#{@action}_result".to_sym
9
+ @response = response
10
+ end
11
+
12
+ def [] name
13
+ case name
14
+ when :result
15
+ @response.body[@response_name][@result_name]
16
+ else
17
+ @response.body[@response_name][@result_name][name]
18
+ end
19
+ end
20
+
21
+ def ending_with suffix
22
+ @response_name = "#{@action}_#{suffix}_response".to_sym
23
+ @result_name = "#{@action}_#{suffix}_result".to_sym
24
+ self
25
+ end
26
+
27
+ def starting_with prefix
28
+ @response_name = "#{prefix}_#{@action}_response".to_sym
29
+ @result_name = "#{prefix}_#{@action}_result".to_sym
30
+ self
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module ElephantDriveService
4
+ VERSION = "0.1.5"
5
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elephant-drive-service
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.5
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Equipe de soluções de atendimento
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: savon
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
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: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
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
+ - !ruby/object:Gem::Dependency
47
+ name: vcr
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: webmock
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.8.3
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.8.3
78
+ - !ruby/object:Gem::Dependency
79
+ name: pry
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rake
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Client SOAP para integração com os serviços do ElephantDrive
111
+ email:
112
+ - saas-dev@locaweb.com.br
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - lib/elephant-drive-service/base_service.rb
118
+ - lib/elephant-drive-service/config.rb
119
+ - lib/elephant-drive-service/message_builder.rb
120
+ - lib/elephant-drive-service/provisioning_service.rb
121
+ - lib/elephant-drive-service/report_service.rb
122
+ - lib/elephant-drive-service/response_builder.rb
123
+ - lib/elephant-drive-service/result_extractor.rb
124
+ - lib/elephant-drive-service/version.rb
125
+ - lib/elephant-drive-service.rb
126
+ homepage: ''
127
+ licenses: []
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ! '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 1.8.24
147
+ signing_key:
148
+ specification_version: 3
149
+ summary: ''
150
+ test_files: []