Completed
Push — master ( f53a53...55639c )
by John
01:02
created

MessagesRetriever.retrieve_for_event()   B

Complexity

Conditions 3

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 3
dl 0
loc 32
rs 8.8571
1
require 'date'
2
require 'jsondoc'
3
4
module RingCentralSdk::REST
5
  class MessagesRetriever
6
    attr_accessor :range
7
    def initialize(client)
8
      @client = client
9
      @range = 1.0 # minutes
10
    end
11
12
    def retrieve_for_event(event, params={})
13
      unless event.is_a? RingCentralSdk::REST::Event
14
        fail 'retrieve_for_event requires RingCentralSdk::REST::Event argument'
15
      end
16
      url = event.doc.getAttr :event
17
      last_updated_s = event.doc.getAttr('body.lastUpdated')
18
      last_updated_dt = DateTime.iso8601(last_updated_s)
19
20
      params.merge!({
21
        :dateFrom => (last_updated_dt - (@range/1440.0)).to_s,
22
        :dateTo => (last_updated_dt + (@range/1440.0)).to_s
23
      })
24
      if event.new_sms_count > 0
25
        params[:messageType] = 'SMS'
26
      end
27
28
      res = @client.http.get do |req|
29
        req.url url
30
        req.params = params
31
      end
32
33
      messages = []
34
35
      res.body['records'].each do |rec|
36
        rec_last_modified_time = rec['lastModifiedTime']
37
        rec_last_modified_time_dt = DateTime.iso8601(rec_last_modified_time)
38
        if rec_last_modified_time_dt == last_updated_dt
39
          messages.push rec
40
        end
41
      end
42
      return messages
43
   end
44
  end
45
end
46