Total Complexity | 11 |
Total Lines | 49 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | require 'multi_json' |
||
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 |
||
56 |