Skip to content

Commit

Permalink
Some ideas for accessing messages and users
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Patterson committed Dec 20, 2008
1 parent 91f5fa6 commit f423171
Show file tree
Hide file tree
Showing 11 changed files with 215 additions and 38 deletions.
1 change: 1 addition & 0 deletions README
Expand Up @@ -10,6 +10,7 @@ Yammer4R provides an object based API to query or update your Yammer account via
* Ruby 1.8 (tested with 1.8.7)
* JSON gem (tested with versions: 1.1.3)
* OAuth gem (tested with versions: 0.2.7)
* RSpec gem (tested with versions: 1.1.11)

== Usage Examples
Coming soon...
15 changes: 14 additions & 1 deletion example.rb
@@ -1,4 +1,17 @@
require 'yammer'

# Create a new Yammer Client
yammer = Yammer::Client.new
puts yammer.messages.inspect

# Get all messages
messages = yammer.messages
puts messages.size
puts messages.last.inspect

# Print out all the users
yammer.users.each do |u|
puts "#{u.name} - #{u.me?}"
end



12 changes: 12 additions & 0 deletions ext/stdlib.rb
@@ -0,0 +1,12 @@
class String
def to_boolean
case self
when 'true'
true
when 'false'
false
else
nil
end
end
end
6 changes: 6 additions & 0 deletions spec/spec.opts
@@ -0,0 +1,6 @@
--colour
--format
progress
--loadby
mtime
--reverse
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,5 @@
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")

require 'spec'

# Add helper methods here if relevant to multiple _spec.rb files
22 changes: 22 additions & 0 deletions spec/yammer/message_spec.rb
@@ -0,0 +1,22 @@
require File.join(File.dirname(__FILE__), '..', 'spec_helper')

describe Yammer::Client, "#message" do
before(:each) do
@twitter = client_context
@message = 'This is my unique message'
@uris = Twitter::Client.class_eval("@@STATUS_URIS")
@options = {:id => 666666}
@request = mas_net_http_get(:basic_auth => nil)
@response = mas_net_http_response(:success, '{}')
@connection = mas_net_http(@response)
@float = 43.3434
@status = Twitter::Status.new(:id => 2349343)
@source = Twitter::Client.class_eval("@@defaults[:source]")
end

it "should return nil if nil is passed as value argument for :get case" do
status = @twitter.status(:get, nil)
status.should be_nil
end

end
6 changes: 5 additions & 1 deletion yammer.rb
Expand Up @@ -5,10 +5,14 @@ def require_local(suffix)
end

require('rubygems')
require('date')
require('yaml')
require('open-uri')
require('json')
require('oauth/consumer')

require_local('ext/stdlib')
require_local('yammer/client')
require_local('yammer/message')
require_local('yammer/message')
require_local('yammer/message_list')
require_local('yammer/user')
58 changes: 39 additions & 19 deletions yammer/client.rb
@@ -1,6 +1,8 @@
class Yammer::Client

URL = 'https://www.yammer.com'

attr_reader :access_token

def initialize
config = YAML.load(open('oauth.yml'))
Expand All @@ -10,27 +12,45 @@ def initialize
@access_token = OAuth::AccessToken.new(@consumer,@token,@secret)
end

# { "message_type"=>"update",
# "created_at"=>"2008/12/19 20:43:48 +0000",
# "body"=>{"plain"=>"@sgoldberg Steve returned his laptop so I have another Macbook Pro in the office.",
# "parsed"=>"[[user:131808]] Steve returned his laptop so I have another Macbook Pro in the office."},
# "client_type"=>"Web",
# "system_message"=>false,
# "url"=>"https://www.yammer.com/api/v1/messages/1672420",
# "id"=>1672420,
# "thread_id"=>1672420,
# "sender_type"=>"user",
# "sender_id"=>131802,
# "replied_to_id"=>nil,
# "web_url"=>"https://www.yammer.com/messages/1672420",
# "attachments"=>[],
# "client_url"=>nil }

def messages
response = @access_token.get '/api/v1/messages.json'
JSON.parse(response.body)['messages'].map do |m|
def messages(action = :all)
response = case action
when :all:
@access_token.get "/api/v1/messages.json"
when :sent, :received, :following:
@access_token.get "/api/v1/messages/#{action}.json"
else
raise ArgumentError, "Invalid messaging action: #{action}"
end
parsed_response = JSON.parse(response.body)
older_available = parsed_response['meta']['older_available']
ml = parsed_response['messages'].map do |m|
Yammer::Message.new(m)
end
Yammer::MessageList.new(ml, older_available, self)
end

def users
response = @access_token.get "/api/v1/users.json"
JSON.parse(response.body).map do |u|
Yammer::User.new(u, self)
end
end

def user(id)
response = @access_token.get "/api/v1/users/#{id}.json"
u = JSON.parse(response.body)
Yammer::User.new(u, self)
end

def me
@me ||= current_user
end

private
def current_user
response = @access_token.get "/api/v1/users/current.json"
u = JSON.parse(response.body)
Yammer::User.new(u, self)
end

end
36 changes: 19 additions & 17 deletions yammer/message.rb
@@ -1,24 +1,26 @@
# <message>
# <id>1102</id>
# <url>https://www.yammer.com/api/v1/messages/1102</url>
# <web-url>https://www.yammer.com/messages/1102</web-url>
# <replied-to-id>1101</replied-to-id>
# <thread-id>1101</thread-id>
# <body>
# <plain>I love #yammer.</plain>
# <parsed>I love [[tag:1000]].</parsed>
# </body>
# <message-type>update</message-type>
# <client-type>web</client-type>
# <sender-id>1002</sender-id>
# <sender-type>user</sender-type>
# <created-at>2008-09-12T17:35:43Z</created-at>
# </message>

class Yammer::Message

attr_reader :id, :url, :web_url, :replied_to_id, :thread_id,
:body_plain, :body_parsed, :message_type, :client_type,
:sender_id, :sender_type

def initialize(m)
@id = m['id']
@url = m['url']
@web_url = m['web_url']
@replied_to_id = m['replied_to_id']
@thread_id = m['thread_id']
@body_plain = m['body']['plain']
@body_parsed = m['body']['parsed']
@message_type = m['message_type']
@client_type = m['client_type']
@sender_id = m['sender_id']
@sender_type = m['sender_type']
begin
@created_at = m['created_at']
rescue ArgumentError => e
@created_at = nil
end
end

end
20 changes: 20 additions & 0 deletions yammer/message_list.rb
@@ -0,0 +1,20 @@
class Yammer::MessageList < Array

attr_reader :older_available, :ids

def initialize(a, oa, c)
super(a)
@older_available = oa
@client = c
@ids = a.map {|m| m.id}.sort
end

def first
self[0]
end

def last
self[self.size - 1]
end

end
72 changes: 72 additions & 0 deletions yammer/user.rb
@@ -0,0 +1,72 @@
class Yammer::User

attr_reader :id, :url, :web_url, :name, :full_name, :mugshot_url,
:job_title, :location, :stats, :contact,
:hire_date, :birth_date, :network_name, :full_user

def initialize(u, c)
@client = c
@id = u['id']
@url = u['url']
@web_url = u['web_url']
@name = u['name']
@full_name = u['full_name']
@mugshot_url = u['mugshot_url']
@job_title = u['job_title']
# These attributes will be nil when User is a reference
@network_id = u['network_id']
@location = u['location']
@stats = u['stats']
@contact = u['contact']
@hire_date = u['hire_date']
@birth_date = u['birth_date']
@network_name = u['network_name']
end

def network_id
get_full_user unless @network_id
@network_id
end

def location
get_full_user unless @location
@location
end

def stats
get_full_user unless @stats
@stats
end

def contact
get_full_user unless @contact
@contact
end

def hire_date
get_full_user unless @hire_date
@hire_date
end

def birth_date
get_full_user unless @birth_date
@birth_date
end

def network_name
get_full_user unless @network_name
@network_name
end

def me?
@id == @client.me.id
end

private
def get_full_user
response = @client.access_token.get "/api/v1/users/#{id}.json"
u = JSON.parse(response.body)
initialize(u, @client)
end

end

0 comments on commit f423171

Please sign in to comment.