Class: Flowdock::Client

Inherits:
Object
  • Object
show all
Includes:
Helpers, HTTParty
Defined in:
lib/flowdock.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#blank?, #handle_response

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



141
142
143
144
145
# File 'lib/flowdock.rb', line 141

def initialize(options = {})
  @api_token = options[:api_token]
  @flow_token = options[:flow_token]
  raise InvalidParameterError, "Client must have :api_token or an :flow_token" if blank?(@api_token) && blank?(@flow_token)
end

Instance Attribute Details

#api_tokenObject (readonly)

Returns the value of attribute api_token.



140
141
142
# File 'lib/flowdock.rb', line 140

def api_token
  @api_token
end

Instance Method Details

#chat_message(params) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/flowdock.rb', line 147

def chat_message(params)
  raise InvalidParameterError, "missing api_token" if blank?(@api_token)
  raise InvalidParameterError, "Message must have :content" if blank?(params[:content])
  raise InvalidParameterError, "Message must have :flow" if blank?(params[:flow])
  params = params.clone
  tags = (params[:tags].kind_of?(Array)) ? params[:tags] : []
  params[:message] = params.delete(:message_id) if params[:message_id]
  tags.reject! { |tag| !tag.kind_of?(String) || blank?(tag) }
  event = if params[:message] then 'comment' else 'message' end
  post(event + 's', params.merge(tags: tags, event: event))
end

#delete(path) ⇒ Object



195
196
197
198
# File 'lib/flowdock.rb', line 195

def delete(path)
  resp = self.class.delete(api_url(path), :basic_auth => {:username => @api_token, :password => ''}, :headers => headers)
  handle_response(resp)
end

#get(path, data = {}) ⇒ Object



185
186
187
188
# File 'lib/flowdock.rb', line 185

def get(path, data = {})
  resp = self.class.get(api_url(path), :query => data, :basic_auth => {:username => @api_token, :password => ''}, :headers => headers)
  handle_response(resp)
end

#post(path, data = {}) ⇒ Object



180
181
182
183
# File 'lib/flowdock.rb', line 180

def post(path, data = {})
  resp = self.class.post(api_url(path), :body => MultiJson.dump(data), :basic_auth => {:username => @api_token, :password => ''}, :headers => headers)
  handle_response(resp)
end

#post_to_thread(thread) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/flowdock.rb', line 172

def post_to_thread(thread)
  raise InvalidParameterError, "missing flow_token" if blank?(@flow_token)
  resp = self.class.post(api_url("/messages"),
                         body: MultiJson.dump(thread.merge(flow_token: @flow_token)),
                         headers: headers)
  handle_response resp
end

#private_message(params) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/flowdock.rb', line 159

def private_message(params)
  raise InvalidParameterError, "missing api_token" if blank?(@api_token)
  raise InvalidParameterError, "Message must have :content" if blank?(params[:content])
  raise InvalidParameterError, "Message must have :user_id" if blank?(params[:user_id])

  user_id = params.delete(:user_id)

  params = params.clone
  event = "message"

  post("private/#{user_id}/messages", params.merge(event: event))
end

#put(path, data = {}) ⇒ Object



190
191
192
193
# File 'lib/flowdock.rb', line 190

def put(path, data = {})
  resp = self.class.put(api_url(path), :body => MultiJson.dump(data), :basic_auth => {:username => @api_token, :password => ''}, :headers => headers)
  handle_response(resp)
end