Completed
Push — master ( 13432f...78215f )
by John
59s
created

Groups.all_groups()   B

Complexity

Conditions 5

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
rs 8.5454
cc 5
1
module GlipSdk
2
  module REST
3
    class Groups
4
      attr_accessor :cache
5
      attr_accessor :subscription
6
7
      def initialize(rc_sdk)
8
        @api = rc_sdk
9
      end
10
11
      def get(opts = {})
12
        if opts.key? :groupId
13
          return @api.http.get "glip/groups/#{opts[:groupId]}"
14
        end
15
        @api.http.get 'glip/groups', opts
16
      end
17
18
      def observe(observer)
19
        @subscription = @api.create_subscription
20
        @subscription.subscribe ['/restapi/v1.0/account/~/extension/~/glip/groups']
21
        @subscription.add_observer observer
22
      end
23
24
      def all_groups(params = {})
25
        groups = []
26
        get_next = true
27
        while get_next == true
28
          res = get params
29
          groups.concat(res.body['records']) if res.body['records'].length > 0
30
31
          if res.body.key?('navigation') && res.body['navigation'].key?('prevPageToken')
32
            params['pageToken'] = res.body['navigation']['prevPageToken']
33
            @api.config.logger.info "PrevPageToken [#{res.body['navigation']['prevPageToken']}]"
34
          else
35
            get_next = false
36
          end
37
        end
38
        return groups
39
      end
40
    end
41
  end
42
end
43