Class: Braspag::Crypto::JarWebservice

Inherits:
Object
  • Object
show all
Defined in:
lib/baby-braspag/crypto/jar_webservice.rb

Class Method Summary collapse

Class Method Details

.decrypt(encrypted, fields) ⇒ Object



36
37
38
39
40
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
# File 'lib/baby-braspag/crypto/jar_webservice.rb', line 36

def self.decrypt(encrypted, fields)
  crypto_key = Braspag::Connection.instance.crypto_key
  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



4
5
6
7
8
9
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
# File 'lib/baby-braspag/crypto/jar_webservice.rb', line 4

def self.encrypt(map)
  crypto_key = Braspag::Connection.instance.crypto_key
  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