Class: Shaman

Inherits:
Object
  • Object
show all
Defined in:
lib/shaman.rb,
lib/shaman/version.rb

Constant Summary collapse

VERSION =
"0.3.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ Shaman

Returns a new instance of Shaman.



10
11
12
# File 'lib/shaman.rb', line 10

def initialize(body)
  @body = body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



8
9
10
# File 'lib/shaman.rb', line 8

def body
  @body
end

Instance Method Details

#body_hashObject



31
32
33
34
# File 'lib/shaman.rb', line 31

def body_hash
  return body if valid_hash?
  false
end

#parsed_formObject



48
49
50
# File 'lib/shaman.rb', line 48

def parsed_form
  Rack::Utils.parse_nested_query(body)
end

#parsed_jsonObject



36
37
38
39
40
# File 'lib/shaman.rb', line 36

def parsed_json
  Oj.load(body) if body
rescue Oj::ParseError
  false
end

#parsed_xmlObject



42
43
44
45
46
# File 'lib/shaman.rb', line 42

def parsed_xml
  Hash.from_xml(body)
rescue REXML::ParseException
  false
end

#prepped_bodyObject



52
53
54
# File 'lib/shaman.rb', line 52

def prepped_body
  body_hash or parsed_json or parsed_xml or parsed_form
end

#shaObject



14
15
16
# File 'lib/shaman.rb', line 14

def sha
  Digest::MD5.hexdigest(sorted_hash(prepped_body).to_s)
end

#sorted_hash(obj) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/shaman.rb', line 56

def sorted_hash(obj)
  return obj unless obj.is_a?(Hash)
  hash = Hash.new
  obj.each { |k, v| hash[k] = sorted_hash(v) }
  sorted = hash.sort { |a, b| a[0].to_s <=> b[0].to_s }
  Hash[sorted].stringify_keys
end

#valid_hash?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/shaman.rb', line 27

def valid_hash?
  body.class == Hash
end

#valid_json?Boolean Also known as: valid_json_body?

Returns:

  • (Boolean)


18
19
20
# File 'lib/shaman.rb', line 18

def valid_json?
  parsed_json.class == Hash
end

#valid_xml?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/shaman.rb', line 23

def valid_xml?
  parsed_xml.class == Hash
end