Class: GlipSdk::REST::Posts

Inherits:
Object
  • Object
show all
Defined in:
lib/glip_sdk/rest/posts.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rc_sdk) ⇒ Posts

Returns a new instance of Posts.



8
9
10
11
# File 'lib/glip_sdk/rest/posts.rb', line 8

def initialize(rc_sdk)
  @api = rc_sdk
  @logger_prefix = " -- #{self.class.name}: "
end

Instance Attribute Details

#groups_cacheObject

Returns the value of attribute groups_cache.



6
7
8
# File 'lib/glip_sdk/rest/posts.rb', line 6

def groups_cache
  @groups_cache
end

Instance Method Details

#get(opts = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/glip_sdk/rest/posts.rb', line 46

def get(opts = {})
  if opts.key? :postId
    return @api.http.get "glip/posts/#{opts[:postId]}"
  end
  @api.http.get do |req|
    req.url "glip/posts"
    req.headers['Content-Type'] = 'application/json'
    req.body = opts
  end
end

#observe(observer) ⇒ Object



57
58
59
60
61
# File 'lib/glip_sdk/rest/posts.rb', line 57

def observe(observer)
  @subscription = @api.create_subscription()
  @subscription.subscribe(['/restapi/v1.0/account/~/extension/~/glip/posts'])
  @subscription.add_observer observer
end

#post(opts = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/glip_sdk/rest/posts.rb', line 13

def post(opts = {})
  unless opts.key? :text
    raise ArgumentError, "Text must be provided to post message"
  end

  unless opts.key?(:groupId) || opts.key?(:groupName)
    raise ArgumentError, "Group Id or Group Name must be provided"
  end

  group_id = opts[:groupId]

  if group_id.nil? && @groups_cache && opts.key?(:groupName)
    group_id = @groups_cache.id_by_name(opts[:groupName])
  end

  params = { groupId: group_id, text: opts[:text] }

  res = @api.http.post do |req|
    req.url 'glip/posts'
    req.headers['Content-Type'] = 'application/json'
    req.body = { groupId: group_id, text: opts[:text] }
  end

  if res.status >= 400
    @api.config.logger.warn("#{@logger_prefix}Glip API Response Status #{res.status}")
    @api.config.logger.warn("#{@logger_prefix}Response Body: #{MultiJson.encode(res.body)}")
  else
    @api.config.logger.info("#{@logger_prefix}Glip API Response Status #{res.status}")
  end

  res
end