class Flowdock::Client

Attributes

api_token[R]

Public Class Methods

new(options = {}) click to toggle source
# 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

Public Instance Methods

chat_message(params) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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

Private Instance Methods

api_url(path) click to toggle source
# File lib/flowdock.rb, line 202
def api_url(path)
  File.join(FLOWDOCK_API_URL, path)
end
headers() click to toggle source
# File lib/flowdock.rb, line 206
def headers
  {"Content-Type" => "application/json", "Accept" => "application/json"}
end