yammer4r 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,16 @@
1
+ = Yammer4R
2
+
3
+ == Developers
4
+ * {Jim Patterson} <jimp79 at gmail dot com>
5
+
6
+ == Description
7
+ Yammer4R provides an object based API to query or update your Yammer account via pure Ruby. It hides the ugly HTTP/REST code from your code.
8
+
9
+ == External Dependencies
10
+ * Ruby 1.8 (tested with 1.8.7)
11
+ * JSON gem (tested with versions: 1.1.3)
12
+ * OAuth gem (tested with versions: 0.2.7)
13
+ * RSpec gem (tested with versions: 1.1.11)
14
+
15
+ == Usage Examples
16
+ Coming soon...
@@ -0,0 +1,15 @@
1
+ require 'yammer4r'
2
+
3
+ config_path = File.dirname(__FILE__) + 'oauth.yml'
4
+ yammer = Yammer::Client.new(:config => config_path)
5
+
6
+ # Get all messages
7
+ messages = yammer.messages
8
+ puts messages.size
9
+ puts messages.last.body.plain
10
+ puts messages.last.body.parsed
11
+
12
+ # Print out all the users
13
+ yammer.users.each do |u|
14
+ puts "#{u.name} - #{u.me?}"
15
+ end
@@ -0,0 +1,30 @@
1
+ class String
2
+ def to_boolean
3
+ case self
4
+ when 'true'
5
+ true
6
+ when 'false'
7
+ false
8
+ else
9
+ nil
10
+ end
11
+ end
12
+ end
13
+
14
+ class Hash
15
+ def symbolize_keys
16
+ inject({}) do |options, (key, value)|
17
+ options[(key.to_sym rescue key) || key] = value
18
+ options
19
+ end
20
+ end
21
+
22
+ def symbolize_keys!
23
+ self.replace(self.symbolize_keys)
24
+ end
25
+
26
+ def assert_has_keys(*valid_keys)
27
+ missing_keys = [valid_keys].flatten - keys
28
+ raise(ArgumentError, "Missing Option(s): #{missing_keys.join(", ")}") unless missing_keys.empty?
29
+ end
30
+ end
@@ -0,0 +1,100 @@
1
+ module Yammer
2
+ class Client
3
+ def initialize(options={})
4
+ options.assert_has_keys(:consumer, :access) unless options.has_key?(:config)
5
+
6
+ yammer_url = options.delete(:yammer_host) || "https://www.yammer.com"
7
+ @api_path = "/api/v1/"
8
+
9
+ if options[:config]
10
+ config = YAML.load(open(options[:config]))
11
+ options[:consumer] = config['consumer'].symbolize_keys
12
+ options[:access] = config['access'].symbolize_keys
13
+ end
14
+
15
+ consumer = OAuth::Consumer.new(options[:consumer][:key], options[:consumer][:secret], :site => yammer_url)
16
+ @access_token = OAuth::AccessToken.new(consumer, options[:access][:token], options[:access][:secret])
17
+ end
18
+
19
+
20
+ # TODO: modularize message and user handling
21
+ def messages(action = :all, params = {})
22
+ params.merge!(:resource => :messages)
23
+ params.merge!(:action => action) unless action == :all
24
+
25
+ parsed_response = JSON.parse(yammer_request(:get, params).body)
26
+ older_available = parsed_response['meta']['older_available']
27
+
28
+ ml = parsed_response['messages'].map do |m|
29
+ mash(m)
30
+ end
31
+ Yammer::MessageList.new(ml, older_available, self)
32
+ end
33
+
34
+ # POST or DELETE a message
35
+ def message(action, params)
36
+ params.merge!(:resource => :messages)
37
+ yammer_request(action, params)
38
+ end
39
+
40
+ def users
41
+ JSON.parse(yammer_request(:get, {:resource => :users}).body).map do |u|
42
+ Yammer::User.new(mash(u), self)
43
+ end
44
+ end
45
+
46
+ def user(id)
47
+ u = JSON.parse(yammer_request(:get, {:resource => :users, :id => id}).body)
48
+ Yammer::User.new(mash(u), self)
49
+ end
50
+
51
+ def current_user
52
+ u = JSON.parse(yammer_request(:get, {:resource => :users, :action => :current}).body)
53
+ Yammer::User.new(mash(u), self)
54
+ end
55
+ alias_method :me, :current_user
56
+
57
+ private
58
+
59
+ def yammer_request(http_method, options)
60
+ request_uri = @api_path + options.delete(:resource).to_s
61
+ [:action, :id].each {|k| request_uri += "/#{options.delete(k)}" if options.has_key?(k) }
62
+ request_uri += ".json"
63
+
64
+ if options.any?
65
+ request_uri += "?#{create_query_string(options)}" unless http_method == :post
66
+ end
67
+
68
+ if http_method == :post
69
+ handle_response(@access_token.send(http_method, request_uri, options))
70
+ else
71
+ handle_response(@access_token.send(http_method, request_uri))
72
+ end
73
+ end
74
+
75
+ def create_query_string(options)
76
+ options.map {|k, v| "#{OAuth::Helper.escape(k)}=#{OAuth::Helper.escape(v)}"}.join('&')
77
+ end
78
+
79
+ def mash(json)
80
+ Mash.new(json)
81
+ end
82
+
83
+ def handle_response(response)
84
+ # TODO: Write classes for exceptions
85
+ case response.code.to_i
86
+ when 200..201
87
+ response
88
+ when 400
89
+ raise "400 Bad request"
90
+ when 401
91
+ raise "Authentication failed. Check your username and password"
92
+ when 503
93
+ raise "503: Service Unavailable"
94
+ else
95
+ raise "Error. HTTP Response #{response.code}"
96
+ end
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,26 @@
1
+ class Yammer::Message
2
+
3
+ attr_reader :id, :url, :web_url, :replied_to_id, :thread_id,
4
+ :body_plain, :body_parsed, :message_type, :client_type,
5
+ :sender_id, :sender_type
6
+
7
+ def initialize(m)
8
+ @id = m['id']
9
+ @url = m['url']
10
+ @web_url = m['web_url']
11
+ @replied_to_id = m['replied_to_id']
12
+ @thread_id = m['thread_id']
13
+ @body_plain = m['body']['plain']
14
+ @body_parsed = m['body']['parsed']
15
+ @message_type = m['message_type']
16
+ @client_type = m['client_type']
17
+ @sender_id = m['sender_id']
18
+ @sender_type = m['sender_type']
19
+ begin
20
+ @created_at = m['created_at']
21
+ rescue ArgumentError => e
22
+ @created_at = nil
23
+ end
24
+ end
25
+
26
+ end
@@ -0,0 +1,20 @@
1
+ class Yammer::MessageList < Array
2
+
3
+ attr_reader :older_available, :ids
4
+
5
+ def initialize(a, oa, c)
6
+ super(a)
7
+ @older_available = oa
8
+ @client = c
9
+ @ids = a.map {|m| m.id}.sort
10
+ end
11
+
12
+ def first
13
+ self[0]
14
+ end
15
+
16
+ def last
17
+ self[self.size - 1]
18
+ end
19
+
20
+ end
@@ -0,0 +1,17 @@
1
+ class Yammer::User
2
+ extend Forwardable
3
+ def_delegator :@user, :id
4
+
5
+ def initialize(mash, client)
6
+ @user = mash
7
+ @client = client
8
+ end
9
+
10
+ def me?
11
+ @user.id == @client.me.id
12
+ end
13
+
14
+ def method_missing(call, *args)
15
+ @user.send(call, *args)
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ require 'forwardable'
2
+ require 'rubygems'
3
+ require 'date'
4
+ require 'yaml'
5
+ require 'open-uri'
6
+
7
+ gem 'json', '>= 1.1.7'
8
+ require 'json'
9
+
10
+ gem 'oauth', '>=0.3.5'
11
+ require 'oauth'
12
+
13
+ gem 'mash', '>=0.0.3'
14
+ require 'mash'
15
+
16
+ $:.unshift(File.dirname(__FILE__))
17
+ require 'ext/core_ext'
18
+ require 'yammer/client'
19
+ require 'yammer/message'
20
+ require 'yammer/message_list'
21
+ require 'yammer/user'
@@ -0,0 +1,7 @@
1
+ consumer:
2
+ key: CLIENT_KEY
3
+ secret: CLIENT_SECRET
4
+
5
+ access:
6
+ token: CONSUMER_TOKEN
7
+ secret: CONSUMER_SECRET
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yammer4r
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - Jim Patterson
8
+ - Jason Stewart
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-08-12 00:00:00 -04:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: mash
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 0.0.3
25
+ version:
26
+ - !ruby/object:Gem::Dependency
27
+ name: json
28
+ type: :runtime
29
+ version_requirement:
30
+ version_requirements: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 1.1.7
35
+ version:
36
+ - !ruby/object:Gem::Dependency
37
+ name: oauth
38
+ type: :runtime
39
+ version_requirement:
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 0.3.5
45
+ version:
46
+ description: Yammer4R provides an object based API to query or update your Yammer account via pure Ruby. It hides the ugly HTTP/REST code from your code.
47
+ email: jimp79@gmail.com
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files: []
53
+
54
+ files:
55
+ - README
56
+ - example.rb
57
+ - oauth.yml.template
58
+ - lib/yammer4r.rb
59
+ - lib/yammer/client.rb
60
+ - lib/yammer/message.rb
61
+ - lib/yammer/message_list.rb
62
+ - lib/yammer/user.rb
63
+ - lib/ext/core_ext.rb
64
+ has_rdoc: true
65
+ homepage: http://github.com/jpatterson/yammer4r
66
+ licenses: []
67
+
68
+ post_install_message:
69
+ rdoc_options: []
70
+
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ version:
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: "0"
84
+ version:
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.3.5
89
+ signing_key:
90
+ specification_version: 2
91
+ summary: Yammer access for ruby
92
+ test_files: []
93
+