| Total Complexity | 15 |
| Total Lines | 44 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
| 1 | require 'base64' |
||
| 6 | class JSON |
||
| 7 | BASE64_DEFAULT = true |
||
| 8 | CONTENTID_DEFAULT = false |
||
| 9 | |||
| 10 | attr_accessor :mime |
||
| 11 | attr_accessor :json |
||
| 12 | |||
| 13 | def initialize(content, opts = {}) |
||
| 14 | @opts = opts |
||
| 15 | |||
| 16 | @json = content_to_json content |
||
| 17 | |||
| 18 | @json = Base64.encode64(@json) if encode_base64? |
||
| 19 | @mime = build_json_part @json |
||
| 20 | end |
||
| 21 | |||
| 22 | def content_to_json(content) |
||
| 23 | content = MultiJson.encode(content) if content.is_a?(Array) || content.is_a?(Hash) |
||
| 24 | content = content.to_s unless content.is_a? String |
||
| 25 | content |
||
| 26 | end |
||
| 27 | |||
| 28 | def build_json_part(json) |
||
| 29 | json_part = MIME::Text.new(json) |
||
| 30 | json_part.headers.delete('Content-Id') if disable_content_id? |
||
| 31 | json_part.headers.set('Content-Type', 'application/json') |
||
| 32 | json_part.headers.set('Content-Transfer-Encoding', 'base64') if encode_base64? |
||
| 33 | return json_part |
||
| 34 | end |
||
| 35 | |||
| 36 | def encode_base64? |
||
| 37 | if @opts.key? :encode_base64 |
||
| 38 | return @opts[:encode_base64] ? true : false |
||
| 39 | end |
||
| 40 | return BASE64_DEFAULT |
||
| 41 | end |
||
| 42 | |||
| 43 | def disable_content_id? |
||
| 44 | if @opts.key? :content_id_disable |
||
| 45 | return @opts[:content_id_disable] ? true : false |
||
| 46 | end |
||
| 47 | return !CONTENTID_DEFAULT |
||
| 48 | end |
||
| 49 | end |
||
| 50 | end |
||
| 51 |