Module: PINS::Helper
- Defined in:
- lib/postmark-inbound/helper.rb
Instance Method Summary collapse
-
#catch_pm_headers(headers) ⇒ Hash
Extract Postmark related headers into a Hash.
-
#downcase_keys(json) ⇒ String
Convert JSON keys to lowercase.
-
#json_format_value(val) ⇒ Object
Return Ruby object/value to JSON standard format.
-
#json_with_object(obj, pretty: true, opts: nil) ⇒ String
Convert object into JSON, optionally pretty-format.
- #lookup_email_headers(headers, name) ⇒ Object
- #parse_json(json) ⇒ Object
-
#parse_pm(hash) ⇒ Object
Additional parsing/conversions.
Instance Method Details
#catch_pm_headers(headers) ⇒ Hash
Extract Postmark related headers into a Hash
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/postmark-inbound/helper.rb', line 29 def catch_pm_headers(headers) return {} unless Array === headers caught = {} headers.each do |h| case h[:name] when 'X-Spam-Status' caught[:spam_status] = h[:value].downcase.start_with?('yes') when 'X-Spam-Score' caught[:spam_score] = h[:value].to_f when 'Received-SPF' caught[:received_spf_status] = (h[:value].split)[0].downcase end end return caught end |
#downcase_keys(json) ⇒ String
Convert JSON keys to lowercase
16 17 18 |
# File 'lib/postmark-inbound/helper.rb', line 16 def downcase_keys(json) json.gsub(/"\w+":/) { |m| m.downcase } end |
#json_format_value(val) ⇒ Object
Return Ruby object/value to JSON standard format
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/postmark-inbound/helper.rb', line 68 def json_format_value(val) case val when Array val.map { |v| json_format_value(v) } when Hash val.reduce({}) { |h, (k, v)| h.merge({k => json_format_value(v)}) } when String val.encode!('UTF-8', {invalid: :replace, undef: :replace}) when Time val.utc.iso8601 else val end end |
#json_with_object(obj, pretty: true, opts: nil) ⇒ String
Convert object into JSON, optionally pretty-format
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/postmark-inbound/helper.rb', line 52 def json_with_object(obj, pretty: true, opts: nil) return '{}' if obj.nil? if pretty opts = { indent: ' ', space: ' ', object_nl: "\n", array_nl: "\n" } end JSON.fast_generate(json_format_value(obj), opts) end |
#lookup_email_headers(headers, name) ⇒ Object
45 46 |
# File 'lib/postmark-inbound/helper.rb', line 45 def lookup_email_headers(headers, name) end |
#parse_json(json) ⇒ Object
8 9 10 11 |
# File 'lib/postmark-inbound/helper.rb', line 8 def parse_json(json) obj = JSON.parse(downcase_keys(json), {symbolize_names: true}) return parse_pm(obj).merge!(catch_pm_headers(obj[:headers])) end |
#parse_pm(hash) ⇒ Object
Additional parsing/conversions
21 22 23 24 |
# File 'lib/postmark-inbound/helper.rb', line 21 def parse_pm(hash) hash[:date] = Time.parse(hash[:date]) if hash[:date] return hash end |