Completed
Push — master ( 76d3af...eda300 )
by John
01:23
created

Fax.add_parts()   B

Complexity

Conditions 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
dl 0
loc 24
rs 8.9713
c 1
b 0
f 0
1
require 'base64'
2
require 'mime'
3
require 'mime/types'
4
require 'mime_builder'
5
require 'multi_json'
6
7
module RingCentralSdk::REST::Request
8
  class Fax < RingCentralSdk::REST::Request::Base
9
    attr_reader :msg
10
11
    attr_reader :account_id
12
    attr_reader :extension_id
13
14
    def initialize(opts={})
15
      @metadata_part_encode_base64 = true
16
17
      @msg = MIME::Multipart::Mixed.new
18
      @msg.headers.delete('Content-Id')
19
20
      add_path(opts)
21
      add_part_meta(opts)
22
      add_part_text(opts[:text])
23
      add_parts(opts[:files])
24
      add_parts(opts[:parts])
25
    end
26
27
    def add_path(opts={})
28
      @account_id = opts[:accountId] ||= '~'
29
      @extension_id = opts[:extensionId] ||= '~'
30
    end
31
32
    def add_part_meta(opts={})
33
      meta = create_metadata opts
34
      json = MultiJson.encode meta
35
      json = Base64.encode64(json) if @metadata_part_encode_base64
36
      json_part = MIME::Text.new(json)
37
      json_part.headers.delete('Content-Id')
38
      json_part.headers.set('Content-Type', 'application/json')
39
      json_part.headers.set('Content-Transfer-Encoding', 'base64') if @metadata_part_encode_base64
40
      @msg.add(json_part)
41
      true
42
    end
43
44
    def create_metadata(opts={})
45
      meta = {}
46
      return meta unless opts.is_a?(Hash)
47
48
      inf = RingCentralSdk::REST::Request::Inflator::ContactInfo.new
49
      meta[:to] = inf.inflate_to_array opts[:to]
50
51
      processed = {
52
        accountId: 1,
53
        extensionId: 1,
54
        to: 1,
55
        text: 1,
56
        files: 1,
57
        parts: 1
58
      }
59
60
      opts.each do |k,v|
61
        meta[k] = v unless processed.key? k
62
      end
63
64
      meta
65
    end
66
67
    def add_part_text(text=nil, opts={})
68
      return unless !text.nil? && text.to_s.length>0
69
      opts[:content_id_disable] = true
70
      text_part = MIMEBuilder::Text.new(text, opts)
71
      @msg.add text_part.mime
72
    end
73
74
    def add_parts(parts=[])
75
      return if parts.nil?
76
      unless parts.is_a? Array
77
        raise 'invalid parameter[0]. needs to be an array'
78
      end
79
      parts.each do |part|
80
        if part.is_a? MIME::Media
81
          @msg.add part
82
        elsif part.is_a?(String)
83
          file_part = MIMEBuilder::Filepath.new(part)
84
          @msg.add file_part.mime
85
        elsif part.is_a? Hash
86
          part[:content_id_disable] = true
87
          part[:is_attachment] = true
88
          if part.key? :filename
89
            file_part = MIMEBuilder::Filepath.new(part[:filename], part)
90
            @msg.add file_part.mime
91
          elsif part.key? :text
92
            text_part = MIMEBuilder::Text.new(part[:text], part)
93
            @msg.add text_part.mime
94
          end
95
        end
96
      end
97
    end
98
99
    def method()
100
      'post'
101
    end
102
103
    def url()
104
      "account/#{@account_id.to_s}/extension/#{@extension_id.to_s}/fax"
105
    end
106
107
    def content_type()
108
      @msg.headers.get('Content-Type').to_s
109
    end
110
111
    def body()
112
      @msg.body.to_s
113
    end
114
  end
115
end
116