class MIMEBuilder::JSON

Constants

BASE64_DEFAULT
CONTENTID_DEFAULT

Attributes

json[RW]
mime[RW]

Public Class Methods

new(content, opts = {}) click to toggle source
# File lib/mime_builder/json.rb, line 13
def initialize(content, opts = {})
  @opts = opts

  @json = content_to_json content

  @json = Base64.encode64(@json) if encode_base64?
  @mime = build_json_part @json
end

Public Instance Methods

build_json_part(json) click to toggle source
# File lib/mime_builder/json.rb, line 28
def build_json_part(json)
  json_part = MIME::Text.new(json)
  json_part.headers.delete('Content-Id') if disable_content_id?
  json_part.headers.set('Content-Type', 'application/json')
  json_part.headers.set('Content-Transfer-Encoding', 'base64') if encode_base64?
  return json_part
end
content_to_json(content) click to toggle source
# File lib/mime_builder/json.rb, line 22
def content_to_json(content)
  content = MultiJson.encode(content) if content.is_a?(Array) || content.is_a?(Hash)
  content = content.to_s unless content.is_a? String
  content
end
disable_content_id?() click to toggle source
# File lib/mime_builder/json.rb, line 43
def disable_content_id?
  if @opts.key? :content_id_disable
    return @opts[:content_id_disable] ? true : false
  end
  return !CONTENTID_DEFAULT
end
encode_base64?() click to toggle source
# File lib/mime_builder/json.rb, line 36
def encode_base64?
  if @opts.key? :encode_base64
    return @opts[:encode_base64] ? true : false
  end
  return BASE64_DEFAULT
end