| Total Complexity | 12 |
| Total Lines | 58 |
| 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 | @logger_prefix = " -- #{self.class.name}: " |
||
| 11 | end |
||
| 12 | |||
| 13 | def post(opts = {}) |
||
| 14 | unless opts.key? :text |
||
| 15 | raise ArgumentError, "Text must be provided to post message" |
||
| 16 | end |
||
| 17 | |||
| 18 | unless opts.key?(:groupId) || opts.key?(:groupName) |
||
| 19 | raise ArgumentError, "Group Id or Group Name must be provided" |
||
| 20 | end |
||
| 21 | |||
| 22 | group_id = opts[:groupId] |
||
| 23 | |||
| 24 | if group_id.nil? && @groups_cache && opts.key?(:groupName) |
||
| 25 | group_id = @groups_cache.id_by_name(opts[:groupName]) |
||
| 26 | end |
||
| 27 | |||
| 28 | params = { groupId: group_id, text: opts[:text] } |
||
| 29 | |||
| 30 | res = @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 | |||
| 36 | if res.status >= 400 |
||
| 37 | @api.config.logger.warn("#{@logger_prefix}Glip API Response Status #{res.status}") |
||
| 38 | @api.config.logger.warn("#{@logger_prefix}Response Body: #{MultiJson.encode(res.body)}") |
||
| 39 | else |
||
| 40 | @api.config.logger.info("#{@logger_prefix}Glip API Response Status #{res.status}") |
||
| 41 | end |
||
| 42 | |||
| 43 | res |
||
| 44 | end |
||
| 45 | |||
| 46 | def get(opts = {}) |
||
| 47 | if opts.key? :postId |
||
| 48 | return @api.http.get "glip/posts/#{opts[:postId]}" |
||
| 49 | end |
||
| 50 | @api.http.get do |req| |
||
| 51 | req.url "glip/posts" |
||
| 52 | req.headers['Content-Type'] = 'application/json' |
||
| 53 | req.body = opts |
||
| 54 | end |
||
| 55 | end |
||
| 56 | |||
| 57 | def observe(observer) |
||
| 58 | @subscription = @api.create_subscription() |
||
| 59 | @subscription.subscribe(['/restapi/v1.0/account/~/extension/~/glip/posts']) |
||
| 60 | @subscription.add_observer observer |
||
| 61 | end |
||
| 62 | end |
||
| 63 | end |
||
| 65 |