Module: Diversion::Decode::Json

Defined in:
lib/diversion/decode/json.rb

Class Method Summary collapse

Class Method Details

.get_hash(data, options) ⇒ Object



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
40
41
42
43
# File 'lib/diversion/decode/json.rb', line 11

def get_hash(data, options)
  arr = data.split('-')
  raise Error::BadUrlDataFormat unless arr.length.between?(1,2)

  # get json from data
  json_raw = Url::decode_url(arr.first)

  # parse the JSON and catch any error
  begin
  hash = MultiJson.load(json_raw)
  rescue 
    return {:parsed => false}
  end

  # ensure hash can be accessed with strings or symbols
  hash = HashWithIndifferentAccess.new.merge(hash)

  hash[:parsed] = true # we have parsed the json

  # if signed data then extract keys and verify, else do nothing but update hash
  if arr.length == 2
    hash[:signed] = true
    hash[:key_presented] = arr[1]
    hash[:key_expected] = Signing::sign_data(options[:sign_key], options[:sign_length], json_raw)
    hash[:key_verified] = hash[:key_presented] == hash[:key_expected]
  else
    hash[:signed] = false
    hash[:key_presented] = ""
    hash[:key_expected] = ""
    hash[:key_verified] = false
  end
  hash.symbolize_keys
end