Class: Shaman
- Inherits:
-
Object
- Object
- Shaman
- Defined in:
- lib/shaman.rb,
lib/shaman/version.rb
Constant Summary collapse
- VERSION =
"0.3.2"
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
Instance Method Summary collapse
- #body_hash ⇒ Object
-
#initialize(body) ⇒ Shaman
constructor
A new instance of Shaman.
- #parsed_form ⇒ Object
- #parsed_json ⇒ Object
- #parsed_xml ⇒ Object
- #prepped_body ⇒ Object
- #sha ⇒ Object
- #sorted_hash(obj) ⇒ Object
- #valid_hash? ⇒ Boolean
- #valid_json? ⇒ Boolean (also: #valid_json_body?)
- #valid_xml? ⇒ Boolean
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
#body ⇒ Object (readonly)
Returns the value of attribute body.
8 9 10 |
# File 'lib/shaman.rb', line 8 def body @body end |
Instance Method Details
#body_hash ⇒ Object
31 32 33 34 |
# File 'lib/shaman.rb', line 31 def body_hash return body if valid_hash? false end |
#parsed_form ⇒ Object
48 49 50 |
# File 'lib/shaman.rb', line 48 def parsed_form Rack::Utils.parse_nested_query(body) end |
#parsed_json ⇒ Object
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_xml ⇒ Object
42 43 44 45 46 |
# File 'lib/shaman.rb', line 42 def parsed_xml Hash.from_xml(body) rescue REXML::ParseException false end |
#prepped_body ⇒ Object
52 53 54 |
# File 'lib/shaman.rb', line 52 def prepped_body body_hash or parsed_json or parsed_xml or parsed_form end |
#sha ⇒ Object
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
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?
18 19 20 |
# File 'lib/shaman.rb', line 18 def valid_json? parsed_json.class == Hash end |
#valid_xml? ⇒ Boolean
23 24 25 |
# File 'lib/shaman.rb', line 23 def valid_xml? parsed_xml.class == Hash end |