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

Posts.get()   A

Complexity

Conditions 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
1
require 'multi_json'
2
3
module GlipSdk
4
  module REST
5
    class Posts
6
      attr_accessor :groups_cache
7
8
      def initialize(rc_sdk)
9
        @api = rc_sdk
10
      end
11
12
      def post(opts = {})
13
        unless opts.key? :text
14
          raise ArgumentError, "Text must be provided to post message"
15
        end
16
17
        unless opts.key?(:groupId) || opts.key?(:groupName)
18
          raise ArgumentError, "Group Id or Group Name must be provided"
19
        end
20
21
        group_id = opts[:groupId]
22
23
        if group_id.nil? && @groups_cache && opts.key?(:groupName)
24
          group_id = @groups_cache.id_by_name(opts[:groupName])
25
        end
26
27
        params = { groupId: group_id, text: opts[:text] }
28
        puts MultiJson.encode params
29
30
        @api.http.post do |req|
31
          req.url 'glip/posts'
32
          req.headers['Content-Type'] = 'application/json'
33
          req.body = { groupId: group_id, text: opts[:text] }
34
        end
35
      end
36
37
      def get(opts = {})
38
        if opts.key? :postId
39
          return @api.http.get "glip/posts/#{opts[:postId]}"
40
        end
41
        @api.http.get do |req|
42
          req.url "glip/posts"
43
          req.headers['Content-Type'] = 'application/json'
44
          req.body = opts
45
        end
46
      end
47
48
      def observe(observer)
49
        @subscription = @api.create_subscription()
50
        @subscription.subscribe(['/restapi/v1.0/account/~/extension/~/glip/posts'])
51
        @subscription.add_observer observer
52
      end
53
    end
54
  end
55
end
56