Module: Typetalk::Api::Message

Included in:
Typetalk::Api
Defined in:
lib/typetalk/api/message.rb

Instance Method Summary collapse

Instance Method Details

#get_message(topic_id, post_id, options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/typetalk/api/message.rb', line 39

def get_message(topic_id, post_id, options={})
  options = {token:nil}.merge(options)

  response = connection.get do |req|
    req.url "#{endpoint}/topics/#{topic_id}/posts/#{post_id}"
    req.params[:access_token] = options[:token] || access_token
  end
  parse_response(response)
end

#like_message(topic_id, post_id, options = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/typetalk/api/message.rb', line 62

def like_message(topic_id, post_id, options={})
  options = {token:nil}.merge(options)

  response = connection.post do |req|
    req.url "#{endpoint}/topics/#{topic_id}/posts/#{post_id}/like"
    req.params[:access_token] = options[:token] || access_token
  end
  parse_response(response)
end

#post_message(topic_id, message, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/typetalk/api/message.rb', line 8

def post_message(topic_id, message, options={})
  options = {token:nil, reply_to:nil, file_keys:nil, talk_ids:nil}.merge(options)

  response = connection.post do |req|
    req.url "#{endpoint}/topics/#{topic_id}"
    req.params[:access_token] = options[:token] || access_token
    body = {}
    body[:message] = message
    body[:replyTo] = options[:reply_to] unless options[:reply_to].nil?
    body.merge! to_request_params(:fileKeys, options[:file_keys])
    body.merge! to_request_params(:talkIds, options[:talk_ids])
    req.body = body
  end
  parse_response(response)
end

#read_message(topic_id, post_id = nil, options = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/typetalk/api/message.rb', line 84

def read_message(topic_id, post_id=nil, options={})
  options = {token:nil}.merge(options)

  response = connection.post do |req|
    req.url "#{endpoint}/bookmark/save"
    req.params[:access_token] = options[:token] || access_token
    body = {}
    body[:topicId] = topic_id
    body[:postId] = post_id unless post_id.nil?
    req.body = body
  end
  parse_response(response)
end

#remove_message(topic_id, post_id, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/typetalk/api/message.rb', line 50

def remove_message(topic_id, post_id, options={})
  options = {token:nil}.merge(options)

  response = connection.delete do |req|
    req.url "#{endpoint}/topics/#{topic_id}/posts/#{post_id}"
    req.params[:access_token] = options[:token] || access_token
  end
  parse_response(response)
  response.status == 200
end

#unlike_message(topic_id, post_id, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/typetalk/api/message.rb', line 73

def unlike_message(topic_id, post_id, options={})
  options = {token:nil}.merge(options)

  response = connection.delete do |req|
    req.url "#{endpoint}/topics/#{topic_id}/posts/#{post_id}/like"
    req.params[:access_token] = options[:token] || access_token
  end
  parse_response(response)
end

#upload_attachment(topic_id, file, options = {}) ⇒ Object

Raises:



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/typetalk/api/message.rb', line 25

def upload_attachment(topic_id, file, options={})
  options = {token:nil}.merge(options)

  raise InvalidFileSize if File.size(file) > 10485760 # > 10MB

  response = connection(multipart:true).post do |req|
    req.url "#{endpoint}/topics/#{topic_id}/attachments"
    req.params[:access_token] = options[:token] || access_token
    req.body = { file: Faraday::UploadIO.new(file, MIME::Types.type_for(file).first.to_s) }
  end
  parse_response(response)
end