Class: Moceansdk::Modules::Transmitter

Inherits:
Object
  • Object
show all
Defined in:
lib/moceansdk/modules/transmitter.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Transmitter

Returns a new instance of Transmitter.



5
6
7
8
9
10
11
# File 'lib/moceansdk/modules/transmitter.rb', line 5

def initialize(options = nil)
  @options = default_options

  unless options.nil?
    @options = @options.merge(options)
  end
end

Instance Method Details

#default_optionsObject



13
14
15
16
17
18
19
# File 'lib/moceansdk/modules/transmitter.rb', line 13

def default_options
  {
      base_url: 'https://rest.moceanapi.com',
      version: '2',
      verify_ssl: true
  }
end

#format_response(response_text, is_xml = false, uri = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/moceansdk/modules/transmitter.rb', line 50

def format_response(response_text, is_xml = false, uri = nil)
  raw_response = response_text

  # format for v1
  if !uri.nil? && @options[:version] == '1' && is_xml
    if uri == '/account/pricing'
      response_text = response_text
                          .sub('<data>', '<destinations>')
                          .sub('</data>', '</destinations>')
    elsif uri == '/sms'
      response_text = response_text
                          .sub('<result>', '<result><messages>')
                          .sub('</result>', '</messages></result>')
    end
  end

  processed_response = ResponseFactory.create_object(
      response_text
          .sub('<verify_request>', '')
          .sub('</verify_request>', '')
          .sub('<verify_check>', '')
          .sub('</verify_check>', '')
  )

  if processed_response['status'] && processed_response['status'] != '0' && processed_response['status'] != 0
    processed_response.raw_response = raw_response
    raise Moceansdk::Exceptions::MoceanError.new(processed_response['err_msg'], processed_response)
  end

  # format for v1
  if !uri.nil? && is_xml
    if uri == '/account/pricing'
      processed_response.destinations = processed_response.destinations.destination
    elsif uri == '/sms'
      unless processed_response.messages.message.is_a? Array
        processed_response.messages.message = [processed_response.messages.message]
      end
      processed_response.messages = processed_response.messages.message
    elsif uri == '/voice/dial'
      unless processed_response.calls.call.is_a? Array
        processed_response.calls.call = [processed_response.calls.call]
      end
      processed_response.calls = processed_response.calls.call
    end
  end

  processed_response.raw_response = raw_response
  processed_response
end

#get(uri, params) ⇒ Object



21
22
23
# File 'lib/moceansdk/modules/transmitter.rb', line 21

def get(uri, params)
  request_and_parse_body('get', uri, params)
end

#make_ssl_contextObject



100
101
102
103
104
105
106
107
108
# File 'lib/moceansdk/modules/transmitter.rb', line 100

def make_ssl_context
  if @options[:verify_ssl]
    return nil
  end

  ctx = OpenSSL::SSL::SSLContext.new
  ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
  ctx
end

#post(uri, params) ⇒ Object



25
26
27
# File 'lib/moceansdk/modules/transmitter.rb', line 25

def post(uri, params)
  request_and_parse_body('post', uri, params)
end

#request(method, uri, params) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/moceansdk/modules/transmitter.rb', line 35

def request(method, uri, params)
  params[:'mocean-medium'] = 'RUBY-SDK'

  # use json if default not set
  params[:'mocean-resp-format'] = 'json' unless params[:'mocean-resp-format']

  url = @options[:base_url] + '/rest/' + @options[:version] + uri

  if method.casecmp('get').zero?
    HTTP.follow.get(url, params: params, ssl_context: make_ssl_context)
  else
    HTTP.post(url, form: params, ssl_context: make_ssl_context)
  end
end

#request_and_parse_body(method, uri, params) ⇒ Object



29
30
31
32
33
# File 'lib/moceansdk/modules/transmitter.rb', line 29

def request_and_parse_body(method, uri, params)
  res = request(method, uri, params)

  format_response(res.to_s, params[:'mocean-resp-format'] == 'xml', uri)
end