Class: YammerCli

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeYammerCli

Returns a new instance of YammerCli.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/yammer-cli.rb', line 14

def initialize

  @config_file_path = File.dirname(__FILE__) + '/config.yml'
  @required_settings = [:consumer_token, :consumer_secret, :oauth_token, :oauth_secret]

  if valid_config?
    settings = YAML::load(File.open(@config_file_path))
  else
    exit
  end

  #configure the yammer authentication parameters
  Yammer.configure do |config|
    config.consumer_key = settings[:consumer_token]
    config.consumer_secret = settings[: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



10
11
12
# File 'lib/yammer-cli.rb', line 10

def self.new
  super
end

.setup(consumer_token, consumer_secret) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/yammer-cli.rb', line 68

def self.setup(consumer_token, consumer_secret)
  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, :consumer_token => consumer_token, :consumer_secret => consumer_secret}

  config_file_path = File.dirname(__FILE__) + '/config.yml'
  File.open(config_file_path, "w") do |f|
    f.write tokens.to_yaml
  end

  puts "Settings saved."

end

Instance Method Details

#get_date(date) ⇒ Object

return either Today or formatted date



124
125
126
# File 'lib/yammer-cli.rb', line 124

def get_date(date)
  date == Date.today ? "Today" : date.strftime("%A, %B %e %Y")
end

#get_user(users, id) ⇒ Object

search users for user with specific id and return that user



118
119
120
121
# File 'lib/yammer-cli.rb', line 118

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

#listObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/yammer-cli.rb', line 43

def list
  messages = @yammer.messages

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

  current_date = ""

  #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])
    date = get_date(created_at.to_date)
    user = get_user(users, message[:sender_id])[:full_name]

    # write out current date
    if date != current_date
      puts " --- #{date} ---".foreground(:yellow)
      current_date = date
    end

    puts user.foreground(:red) + " at " + created_at.to_time.getlocal.strftime("%I:%M%p").foreground(:blue) + " " + body
  end
end

#send_update(update, params) ⇒ Object



38
39
40
41
# File 'lib/yammer-cli.rb', line 38

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

#valid_config?Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/yammer-cli.rb', line 93

def valid_config?

  valid_config = true
  if File.exists?(@config_file_path)
    settings = YAML::load(File.open(@config_file_path))
    @required_settings.each do |required_setting|
      if settings[required_setting] == nil
        valid_config = false
      end
    end
  else
    valid_config = false
  end
  
  if !valid_config
    puts "Settings not valid. Run yammer with --setup (-s) option with your yammer Consumer Token and Consumer Secret."
    puts "If you have not registered the app you can do so here https://developer.yammer.com/introduction/"
    puts "to receive your Consumer Token and Consumer Secret."
  end

  valid_config

end