Class: MIMEBuilder::JSON

Inherits:
Object
  • Object
show all
Defined in:
lib/mime_builder/json.rb

Constant Summary collapse

BASE64_DEFAULT =
true
CONTENTID_DEFAULT =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, opts = {}) ⇒ JSON

Returns a new instance of JSON.



13
14
15
16
17
18
19
20
# 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

Instance Attribute Details

#jsonObject

Returns the value of attribute json.



11
12
13
# File 'lib/mime_builder/json.rb', line 11

def json
  @json
end

#mimeObject

Returns the value of attribute mime.



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

def mime
  @mime
end

Instance Method Details

#build_json_part(json) ⇒ Object



28
29
30
31
32
33
34
# 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) ⇒ Object



22
23
24
25
26
# 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?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
# 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?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
# 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