Class: EPochtaService::EPochtaBase

Inherits:
Object
  • Object
show all
Defined in:
lib/e_pochta_base.rb

Direct Known Subclasses

EPochtaEmail, EPochtaSMS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ EPochtaBase

options hash: public_key, private_key



7
8
9
10
11
12
13
14
# File 'lib/e_pochta_base.rb', line 7

def initialize(options)
	if options.is_a? Hash
		self.public_key  = options[:public_key]
		self.private_key = options[:private_key]
	else
		raise ArgumentError, "expected Hash argument, got #{options.class}"
	end
end

Instance Attribute Details

#parametersObject

Returns the value of attribute parameters.



4
5
6
# File 'lib/e_pochta_base.rb', line 4

def parameters
  @parameters
end

#private_keyObject

Returns the value of attribute private_key.



4
5
6
# File 'lib/e_pochta_base.rb', line 4

def private_key
  @private_key
end

#public_keyObject

Returns the value of attribute public_key.



4
5
6
# File 'lib/e_pochta_base.rb', line 4

def public_key
  @public_key
end

Instance Method Details

#calculate_md5(params) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/e_pochta_base.rb', line 16

def calculate_md5(params)
	result = ''
	params['key'] = self.public_key					
	#stringify_keys
	stringified_params = {}
	params.each do |key, value|
		# This is hack for correct md5 summ calculating
		# when we have array value in param, we must concatenate with 'Array' string
		# instead of value of this array =\
		# because of PHP origin of EPochta engine:
		
		# this is PHP example of control summ calculating, which is correct

		# ksort($arrayRequest);
      #    $sum = '';
      #    foreach ($arrayRequest as $v)
      #       $sum .= $v; // if $v is Array then it evaluates to 'Array' string value

		if value.is_a?(Array) 
			stringified_params[key.to_s] = 'Array'
			next
		end
		# =========				
		stringified_params[key.to_s] = value.to_s
	end
	
	#sort & concatenate all values    
	stringified_params.sort.each {|value|	result = result + value[1] }
	result = result + self.private_key
	Digest::MD5.hexdigest( result )
end

#exec_command(params) ⇒ Object



58
59
60
61
# File 'lib/e_pochta_base.rb', line 58

def exec_command(params)
	uri = form_request(params)		
	result = Net::HTTP.post_form(uri, self.parameters)
end

#form_request(params) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/e_pochta_base.rb', line 48

def form_request(params)
	params['version']	= '3.0'
	params['sum'] = calculate_md5 params
	self.parameters = params.each {|k,v| v = URI.escape v.to_s }
	
	url = URI("#{self.class::URL}#{params['action']}")
	url.query = URI.encode_www_form params			
	url
end