Class: Braspag::Crypto::JarWebservice

Inherits:
Object
  • Object
show all
Defined in:
lib/rbraspag/crypto/jar_webservice.rb

Instance Method Summary collapse

Constructor Details

#initialize(crypto_key, connection_uri) ⇒ JarWebservice



5
6
7
8
# File 'lib/rbraspag/crypto/jar_webservice.rb', line 5

def initialize(crypto_key, connection_uri)
  @crypto_key = crypto_key
  @connection_uri = connection_uri
end

Instance Method Details

#decrypt(encrypted, fields) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rbraspag/crypto/jar_webservice.rb', line 41

def decrypt(encrypted, fields)
  raise Braspag::InvalidEncryptedKey if encrypted.nil?
  raise Braspag::InvalidEncryptedKey unless encrypted.is_a?(String)
  
  raise Braspag::IncompleteParams if fields.nil?
  raise Braspag::IncompleteParams unless fields.is_a?(Array)
  
  
  request = ::HTTPI::Request.new decrypt_uri
  request.body = {
    "key" => @crypto_key,
    "encrypted" => encrypted,
    "fields" => fields
  }.to_json

  request.headers["Content-Type"] = "application/json"

  response = ::HTTPI.post request

  begin
    response = JSON.parse(response.body)
  rescue Exception => e
    raise UnknownError
  end
  
  raise IncompleteParams if (
    response["msg"] == "INVALID FORMAT" || 
    response["msg"] == "INVALID FIELDS" 
  )
  
  raise InvalidEncryptedKey if response["msg"] == "INVALID ENCRYPTED STRING"

  raise InvalidCryptKey if response["msg"] == "INVALID KEY"
          
  map = {}
  response["fields"].each do |key,value|
    map[key.downcase.to_sym] = value
  end
  map
end

#encrypt(map) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rbraspag/crypto/jar_webservice.rb', line 10

def encrypt(map)
  raise Braspag::IncompleteParams if map.nil?
  raise Braspag::IncompleteParams unless map.is_a?(Hash)
  
  request = ::HTTPI::Request.new encrypt_uri
  
  data = {:key => @crypto_key, :fields => map}
                      
  request.headers["Content-Type"] = "application/json"
  
  request.body = data.to_json
  
  response = ::HTTPI.post request
  
  begin
    response = JSON.parse(response.body)
  rescue Exception => e
    raise UnknownError
  end
  
  raise IncompleteParams if (
    response["msg"] == "INVALID FORMAT" || 
    response["msg"] == "INVALID FIELDS" 
  )
  
  raise InvalidEncryptedKey if response["msg"] == "INVALID ENCRYPTED STRING"
  raise InvalidCryptKey if response["msg"] == "INVALID KEY"
  
  response["encrypt"]
end