class FlowdockNotifications::FlowdockClient

Public Class Methods

new() click to toggle source
# File lib/flowdock-notifications/flowdock_client.rb, line 6
def initialize
  @client = Flowdock::Client.new(api_token: token)
end

Public Instance Methods

avatar(user_id) click to toggle source
# File lib/flowdock-notifications/flowdock_client.rb, line 28
def avatar(user_id)
  u = user(user_id)
  return nil if u.nil?

  url = u['avatar']
  return nil if url.nil? || url.empty?

  avatar_id = url.match(/\/([^\/]+)\/\Z/)[1]
  return nil if avatar_id.nil?

  # cached local copy of the avatar
  local_filename = "#{Dir.tmpdir}/#{avatar_id}"
  unless File.exists? local_filename
    File.open(local_filename, "wb") do |saved_file|
      open(url, "rb") {|read_file| saved_file.write(read_file.read)}
    end
  end
  local_filename
end
flow_names() click to toggle source
# File lib/flowdock-notifications/flowdock_client.rb, line 10
def flow_names
  flows.map {|flow| flow['parameterized_name']}
end
latest_message_id(flow_name) click to toggle source
# File lib/flowdock-notifications/flowdock_client.rb, line 18
def latest_message_id(flow_name)
  latest_msg = @client.get("#{flow_url(flow_name)}/messages", limit: 1).first
  latest_msg.nil? ? 0 : latest_msg['id']
end
messages_since(flow_name, since_msg_id) click to toggle source
# File lib/flowdock-notifications/flowdock_client.rb, line 14
def messages_since(flow_name, since_msg_id)
  @client.get("#{flow_url(flow_name)}/messages", since_id: since_msg_id)
end
user(user_id) click to toggle source
# File lib/flowdock-notifications/flowdock_client.rb, line 23
def user(user_id)
  return nil if user_id.nil? || user_id == '0'
  @client.get "/users/#{user_id}"
end

Private Instance Methods

flow(flow_name) click to toggle source
# File lib/flowdock-notifications/flowdock_client.rb, line 57
def flow(flow_name)
  flows.find {|f| f['parameterized_name'] == flow_name}
end
flow_url(flow_name) click to toggle source
# File lib/flowdock-notifications/flowdock_client.rb, line 61
def flow_url(flow_name)
  f = flow(flow_name)
  "/flows/#{f['organization']['parameterized_name']}/#{f['parameterized_name']}"
end
flows() click to toggle source
# File lib/flowdock-notifications/flowdock_client.rb, line 53
def flows
  @flows ||= @client.get('/flows')
end
token() click to toggle source
# File lib/flowdock-notifications/flowdock_client.rb, line 49
def token
  ENV['FLOWDOCK_API_TOKEN'] or raise "Must configure FLOWDOCK_API_TOKEN in your environment variables"
end