Class: YammerCli

Inherits:
Object
  • Object
show all
Defined in:
lib/yammer-cli.rb

Constant Summary collapse

CONSUMER_TOKEN =

attr_accessor :yammer

'mWn3PY7sc2znsuZxEMvNUQ'
CONSUMER_SECRET =
'AGP2akBsMwybb1AoGBp7RdLc4vfb2l3NY4P6VM'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeYammerCli

Returns a new instance of YammerCli.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/yammer-cli.rb', line 19

def initialize

  settings_path = File.expand_path('./lib/config.yml')

  if File.exists?(settings_path)
    settings = YAML::load(File.open(settings_path))
  else
    puts "Settings not found. Run yammer with --setup (-s) option."
    exit
  end

  #configure the yammer authentication parameters
  Yammer.configure do |config|
    config.consumer_key = CONSUMER_TOKEN
    config.consumer_secret = CONSUMER_SECRET
    config.oauth_token = settings[:oauth_token]
    config.oauth_token_secret = settings[:oauth_secret]
  end

  #create new yammer object
  @yammer = Yammer.new

end

Class Method Details

.newObject



15
16
17
# File 'lib/yammer-cli.rb', line 15

def self.new
  super
end

.setupObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/yammer-cli.rb', line 63

def self.setup
  consumer=OAuth::Consumer.new CONSUMER_TOKEN,
    CONSUMER_SECRET, 
    {:site=>"https://www.yammer.com"}

  request_token=consumer.get_request_token

  Launchy.open(request_token.authorize_url)

  print "Enter the code from the Yammer website: "
  pin = STDIN.readline.chomp

  access_token = request_token.get_access_token(:oauth_verifier => pin)

  tokens = {:oauth_token => access_token.token, :oauth_secret => access_token.secret}

  settings_path = File.expand_path('./lib/config.yml')

  File.open(settings_path, "w") do |f|
    f.write tokens.to_yaml
  end

  puts "Settings saved."

end

Instance Method Details

#get_user(users, id) ⇒ Object

search users for user with specific id and return that user



91
92
93
94
# File 'lib/yammer-cli.rb', line 91

def get_user(users, id)
  user = users.select { |f| f[:id] == id }
  user.first
end

#listObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/yammer-cli.rb', line 48

def list
  messages = @yammer.messages

  #parse out user objects from references
  users = messages[:references].select {|f| f[:type] == 'user' }

  #parse each message and look up user names from users array
  messages[:messages].each do |message|
    body = message[:body][:plain]
    created_at = DateTime.parse(message[:created_at]).to_time.getlocal.strftime("%I:%M%p")
    user = get_user(users, message[:sender_id])[:full_name]
    puts user.foreground(:red) + " at " + created_at.foreground(:blue) + " " + body
  end
end

#send_update(update) ⇒ Object



43
44
45
46
# File 'lib/yammer-cli.rb', line 43

def send_update(update)
    puts "Sending update to Yammer: #{update}"
    @yammer.update(update)
end