yammer_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yammer_api.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Magdalena Sikorska
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # YammerApi
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'yammer_api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install yammer_api
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,33 @@
1
+ require 'omniauth-oauth2'
2
+
3
+ module YammerApi
4
+ autoload :Api, "yammer_api/api"
5
+ autoload :Base, "yammer_api/base"
6
+ autoload :Client, "yammer_api/client"
7
+ autoload :Errors, "yammer_api/errors"
8
+ autoload :Helper, "yammer_api/helper"
9
+ autoload :Mash, "yammer_api/mash"
10
+ autoload :Request, "yammer_api/request/request"
11
+ autoload :Post, "yammer_api/post"
12
+ autoload :User, "yammer_api/user"
13
+
14
+ class << self
15
+ #Alias for YammerApi::Client.new
16
+ #@return [YammerApi::Client]
17
+ def new(options={})
18
+ YammerApi::Client.new(options)
19
+ end
20
+
21
+ # Delegate to Yammer::Client
22
+ def method_missing(method, *args, &block)
23
+ return super unless new.respond_to?(method)
24
+ new.send(method, *args, &block)
25
+ end
26
+
27
+ def respond_to?(method, include_private = false)
28
+ new.respond_to?(method, include_private) || super(method, include_private)
29
+ end
30
+ end
31
+
32
+
33
+ end
@@ -0,0 +1,8 @@
1
+ module YammerApi
2
+ module Api
3
+ autoload :Feed, 'yammer_api/api/feed'
4
+ autoload :Messages, 'yammer_api/api/messages'
5
+ autoload :Likes, 'yammer_api/api/likes'
6
+ autoload :Users, 'yammer_api/api/users'
7
+ end
8
+ end
@@ -0,0 +1,195 @@
1
+ module YammerApi
2
+ module Api
3
+ module Feed
4
+
5
+ include Helper
6
+
7
+ # Returns the 20 most recent messages in this network. Corresponds to the "Company Feed" tab on the website.
8
+ #
9
+ # @note Developers should note that a strict rate limit is applied to all API requests, so clients should never poll for new messages more frequently than every 30 seconds to ensure that the user is still able to use the API for posting messages, etc.
10
+ # @format `:json`, `:xml`
11
+ # @authenticated true
12
+ # @rate_limited true
13
+ # @param options [Hash] A customizable set of options.
14
+ # @option options [Integer] :older_than Returns only messages older than the message ID specified. This is useful for paginating messages.
15
+ # @option options [Integer] :newer_than Return only messages newer than the message ID specified. This should always be used when polling for new messages.
16
+ # @option options [String] :threaded Accepts true or extended. When true, will only return the first message in each thread. This parameter is intended for applications which display message threads collapsed. threaded=extended will return the thread starter messages in order of most recently active as well as the two most recent messages, as they are viewed in the default view on our website.
17
+ # @option options [Integer] :limit Return only the specified number of messages. Works for `threaded=true` and `threaded=extended`.
18
+ # @return [Yammer::Post]
19
+ # @see http://developer.yammer.com/api/#message-viewing
20
+ # @example Return the 20 most recent messages in this network.
21
+ # Yammer.messages
22
+ def messages(options={})
23
+ path = "/messages.json" + params(options).to_s
24
+ get_and_parse_posts path, options
25
+ end
26
+
27
+ # Returns the 20 sent messages by the current logged in user.
28
+ # Alias for `/api/v1/messages/from_user/logged-in_user_id.format`. Corresponds to the "Sent" tab on the website.
29
+ #
30
+ # @note Important to note that the XML format returns a different structure than the JSON one. So we only support the JSON format for this method.
31
+ # @format `:json`
32
+ # @authenticated true
33
+ # @rate_limited true
34
+ # @param options [Hash] A customizable set of options.
35
+ # @option options [Integer] :older_than Returns only messages older than the message ID specified. This is useful for paginating messages.
36
+ # @option options [Integer] :newer_than Return only messages newer than the message ID specified. This should always be used when polling for new messages.
37
+ # @option options [String] :threaded Accepts true or extended. When true, will only return the first message in each thread. This parameter is intended for applications which display message threads collapsed. threaded=extended will return the thread starter messages in order of most recently active as well as the two most recent messages, as they are viewed in the default view on our website.
38
+ # @option options [Integer] :limit Return only the specified number of messages. Works for `threaded=true` and `threaded=extended`.
39
+ # @return [Yammer::Post]
40
+ # @see http://developer.yammer.com/api/#message-viewing
41
+ # @example Return the 20 most recent sent messages
42
+ # Yammer.messages_sent
43
+ def messages_sent(options={})
44
+ path = "/messages/sent.json" + params(options).to_s
45
+ get_and_parse_posts path, options
46
+ end
47
+
48
+ # Messages received by the logged-in user. Corresponds to the "Received" tab on the website.
49
+ #
50
+ # @note Important to note that the XML format returns a different structure than the JSON one. So we only support the JSON format for this method.
51
+ # @format `:json`
52
+ # @authenticated true
53
+ # @rate_limited true
54
+ # @param options [Hash] A customizable set of options.
55
+ # @option options [Integer] :older_than Returns only messages older than the message ID specified. This is useful for paginating messages.
56
+ # @option options [Integer] :newer_than Return only messages newer than the message ID specified. This should always be used when polling for new messages.
57
+ # @option options [String] :threaded Accepts true or extended. When true, will only return the first message in each thread. This parameter is intended for applications which display message threads collapsed. threaded=extended will return the thread starter messages in order of most recently active as well as the two most recent messages, as they are viewed in the default view on our website.
58
+ # @option options [Integer] :limit Return only the specified number of messages. Works for `threaded=true` and `threaded=extended`.
59
+ # @return [Yammer::Post]
60
+ # @see http://developer.yammer.com/api/#message-viewing
61
+ # @example Return the 20 most recent received messages
62
+ # Yammer.messages_received
63
+ def messages_received(options={})
64
+ path = "/messages/received.json" + params(options).to_s
65
+ get_and_parse_posts path, options
66
+ end
67
+
68
+ # Private Messages (aka Direct Messages) for the logged-in user. Corresponds to the "Direct Messages" section on the website.
69
+ #
70
+ # @note Important to note that the XML format returns a different structure than the JSON one. So we only support the JSON format for this method.
71
+ # @format `:json`
72
+ # @authenticated true
73
+ # @rate_limited true
74
+ # @param options [Hash] A customizable set of options.
75
+ # @option options [Integer] :older_than Returns only messages older than the message ID specified. This is useful for paginating messages.
76
+ # @option options [Integer] :newer_than Return only messages newer than the message ID specified. This should always be used when polling for new messages.
77
+ # @option options [String] :threaded Accepts true or extended. When true, will only return the first message in each thread. This parameter is intended for applications which display message threads collapsed. threaded=extended will return the thread starter messages in order of most recently active as well as the two most recent messages, as they are viewed in the default view on our website.
78
+ # @option options [Integer] :limit Return only the specified number of messages. Works for `threaded=true` and `threaded=extended`.
79
+ # @return [Yammer::Post]
80
+ # @see http://developer.yammer.com/api/#message-viewing
81
+ # @example Return the 20 most recent private messages
82
+ # Yammer.direct_messages
83
+ def direct_messages(options={})
84
+ path = "/messages/private.json" + params(options).to_s
85
+ get_and_parse_dms path, options
86
+ end
87
+
88
+ # Messages followed by the logged-in user. Corresponds to the "My Feed" tab on the website.
89
+ #
90
+ # @note Important to note that the XML format returns a different structure than the JSON one. So we only support the JSON format for this method.
91
+ # @format `:json`
92
+ # @authenticated true
93
+ # @rate_limited true
94
+ # @param options [Hash] A customizable set of options.
95
+ # @option options [Integer] :older_than Returns only messages older than the message ID specified. This is useful for paginating messages.
96
+ # @option options [Integer] :newer_than Return only messages newer than the message ID specified. This should always be used when polling for new messages.
97
+ # @option options [String] :threaded Accepts true or extended. When true, will only return the first message in each thread. This parameter is intended for applications which display message threads collapsed. threaded=extended will return the thread starter messages in order of most recently active as well as the two most recent messages, as they are viewed in the default view on our website.
98
+ # @option options [Integer] :limit Return only the specified number of messages. Works for `threaded=true` and `threaded=extended`.
99
+ # @return [Yammer::Post]
100
+ # @see http://developer.yammer.com/api/#message-viewing
101
+ # @example Return the 20 most recent received messages in my feed
102
+ # Yammer.my_feed
103
+ def my_feed(options={})
104
+ #response = get('messages/following', options, :json)
105
+ path = "/messages/following.json" + params(options).to_s
106
+ get_and_parse_posts path, options
107
+ end
108
+
109
+ # Messages sent by the user with the given ID. Corresponds to the messages on a user profile page on the website.
110
+ #
111
+ # @note Important to note that the XML format returns a different structure than the JSON one. So we only support the JSON format for this method.
112
+ # @format `:json`
113
+ # @authenticated true
114
+ # @rate_limited true
115
+ # @param id [Integer, String] A user ID or screen name.
116
+ # @param options [Hash] A customizable set of options.
117
+ # @option options [Integer] :older_than Returns only messages older than the message ID specified. This is useful for paginating messages.
118
+ # @option options [Integer] :newer_than Return only messages newer than the message ID specified. This should always be used when polling for new messages.
119
+ # @option options [String] :threaded Accepts true or extended. When true, will only return the first message in each thread. This parameter is intended for applications which display message threads collapsed. threaded=extended will return the thread starter messages in order of most recently active as well as the two most recent messages, as they are viewed in the default view on our website.
120
+ # @option options [Integer] :limit Return only the specified number of messages. Works for `threaded=true` and `threaded=extended`.
121
+ # @return [Yammer::Post]
122
+ # @see http://developer.yammer.com/api/#message-viewing
123
+ # @example Return the 20 most recent messages from the user
124
+ # Yammer.messages_from("bruno")
125
+ def messages_from(id, options={})
126
+ path = "/messages/from_user/#{id}.json" + params(options).to_s
127
+ get_and_parse_posts path, options
128
+ end
129
+
130
+ # Messages that have the topic with given ID. Corresponds to the messages on a topic's page on the website.
131
+ #
132
+ # @note Important to note that the XML format returns a different structure than the JSON one. So we only support the JSON format for this method.
133
+ # @format `:json`
134
+ # @authenticated true
135
+ # @rate_limited true
136
+ # @param id [Integer] A topic ID
137
+ # @param options [Hash] A customizable set of options.
138
+ # @option options [Integer] :older_than Returns only messages older than the message ID specified. This is useful for paginating messages.
139
+ # @option options [Integer] :newer_than Return only messages newer than the message ID specified. This should always be used when polling for new messages.
140
+ # @option options [String] :threaded Accepts true or extended. When true, will only return the first message in each thread. This parameter is intended for applications which display message threads collapsed. threaded=extended will return the thread starter messages in order of most recently active as well as the two most recent messages, as they are viewed in the default view on our website.
141
+ # @option options [Integer] :limit Return only the specified number of messages. Works for `threaded=true` and `threaded=extended`.
142
+ # @return [Yammer::Post]
143
+ # @see http://developer.yammer.com/api/#message-viewing
144
+ # @example Return the messages in topic with ID 1234567
145
+ # Yammer.about_topic(1234567)
146
+ def about_topic(id, options={})
147
+ path = "/messages/about_topic/#{id}.json" + params(options).to_s
148
+ get_and_parse_posts path, options
149
+ end
150
+
151
+ # Messages in the thread with the given ID. Corresponds to the page you'd see when clicking on "in reply to" on the website.
152
+ #
153
+ # @note Does not accept the threaded parameter.
154
+ # @format `:json`
155
+ # @authenticated true
156
+ # @rate_limited true
157
+ # @param id [Integer] A thread ID
158
+ # @param options [Hash] A customizable set of options.
159
+ # @option options [Integer] :older_than Returns only messages older than the message ID specified. This is useful for paginating messages.
160
+ # @option options [Integer] :newer_than Return only messages newer than the message ID specified. This should always be used when polling for new messages.
161
+ # @option options [Integer] :limit Return only the specified number of messages. Works for `threaded=true` and `threaded=extended`.
162
+ # @return [Yammer::Post]
163
+ # @see http://developer.yammer.com/api/#message-viewing
164
+ # @example Return the messages in the thread with ID 1234567
165
+ # Yammer.thread(1234567)
166
+ def thread(id, options={})
167
+ path = "/messages/in_thread/#{id}.json" + params(options).to_s
168
+ get_and_parse_posts path, options
169
+ end
170
+
171
+ private
172
+ def get_and_parse_posts path, options
173
+ response = get(path, options)
174
+ raw_messages = response.fetch("messages", [])
175
+ raw_ref = response.fetch("references", [])
176
+ raw_messages.map{|post|
177
+ sender = raw_ref.find{|u| u.id == post.sender_id && u.type == "user"}
178
+ YammerApi::Post.new(post.merge(:sender => sender))
179
+ }
180
+ end
181
+
182
+ def get_and_parse_dms path, options
183
+ response = get(path, options)
184
+ raw_messages = response.fetch("messages", [])
185
+ raw_ref = response.fetch("references", [])
186
+ raw_messages.map{|post|
187
+ sender = raw_ref.find{|u| u.id == post.sender_id && u.type == "user"} || raw_ref.find{|u| u.id == post.sender_id && u.type == "guide"}
188
+ recipient = raw_ref.find{|u| u.id == post.direct_to_id && u.type == "user"}
189
+ YammerApi::Post.new(post.merge(:sender => sender, :recipient => recipient))
190
+ }
191
+ end
192
+
193
+ end
194
+ end
195
+ end
@@ -0,0 +1,33 @@
1
+ module YammerApi
2
+ module Api
3
+ module Likes
4
+
5
+ include Helper
6
+
7
+ # Likes a specified message
8
+ #
9
+ # @format `:json`
10
+ # @authenticated true
11
+ # @rate_limited true
12
+ # @param id [Integer] The ID of message.
13
+ # @see http://developer.yammer.com/api/#likes
14
+ def add_like(id, options={})
15
+ path = "/messages/liked_by/current.json"
16
+ post(path, {:message_id => id}.to_json)
17
+ end
18
+
19
+ # Unlikes a specified message
20
+ #
21
+ # @format `:json`
22
+ # @authenticated true
23
+ # @rate_limited true
24
+ # @param id [Integer] The ID of message.
25
+ # @see http://developer.yammer.com/api/#likes
26
+ def remove_like(id, options={})
27
+ path = "/messages/liked_by/current.json?message_id=#{id}"
28
+ delete(path, options)
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,65 @@
1
+ module YammerApi
2
+ module Api
3
+ module Messages
4
+
5
+ include Helper
6
+ # Creates a new message from the authenticating user
7
+ #
8
+ # @note A status update with text identical to the authenticating user's current status will be ignored to prevent duplicates.
9
+ # @format `:json`
10
+ # @authenticated true
11
+ # @rate_limited false
12
+ # @param body [String] The text of your message.
13
+ # @param options [Hash] A customizable set of options.
14
+ # @option options [Integer] :group_id The ID of the group in which this message belongs.
15
+ # @option options [Integer] :replied_to_id The message ID this message is in reply to, if applicable.
16
+ # @option options [Integer] :direct_to_id the ID of the user to which this message will be sent as a private message.
17
+ # @option options [String] :broadcast If `true` this message should be broadcasted to all users. *Must be an admin*
18
+ # @option options [Integer] :topic*n* Topics to apply to the message. Can use topic1 through topic20.
19
+ # @return [YammerApi::Post] The created message.
20
+ # @see http://developer.yammer.com/api/#messages-manipulating
21
+ def update(message, options={})
22
+ path = "/messages.json"
23
+ response = post(path, {:body => message}.merge(options).to_json)
24
+ parse_post(response)
25
+ end
26
+
27
+
28
+ # Creates a new direct message from the authenticating user to selected user
29
+ #
30
+ # @format `:json`
31
+ # @authenticated true
32
+ # @rate_limited false
33
+ # @param body [String] The text of your message.
34
+ # @param user_id [String] The id of message recipient.
35
+ # @return [YammerApi::Post] The created message.
36
+ # @see http://developer.yammer.com/api/#messages-manipulating
37
+ def send_direct_message(message, user_id, options={})
38
+ path = "/messages.json"
39
+ response = post(path, {:body => message, :direct_to_id => user_id}.merge(options).to_json)
40
+ parse_post(response)
41
+ end
42
+
43
+ # Deletes a specified message from the authenticating user
44
+ #
45
+ # @format `:json`
46
+ # @authenticated true
47
+ # @rate_limited false
48
+ # @param id [Integer] The ID of your message.
49
+ # @see http://developer.yammer.com/api/#messages-manipulating
50
+ def delete_message(id, options={})
51
+ path = "/messages/#{id}"
52
+ delete(path, options)
53
+ end
54
+
55
+ private
56
+ def parse_post response
57
+ raw_messages = response.fetch("messages", [])
58
+ raw_ref = response.fetch("references", [])
59
+ post = raw_messages.first
60
+ sender = raw_ref.find{|u| u.id == post.sender_id && u.type == "user" || u.type == "guide"}
61
+ YammerApi::Post.new(post.merge(:sender => sender))
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,22 @@
1
+ module YammerApi
2
+ module Api
3
+ module Users
4
+
5
+ include Helper
6
+
7
+ # Get information about specified user
8
+ #
9
+ # @format `:json`
10
+ # @authenticated true
11
+ # @rate_limited true
12
+ # @param id [Integer] The ID of user.
13
+ # @see http://developer.yammer.com/api/#users
14
+ def user(id, options={})
15
+ path = "/users/#{id}.json"
16
+ response = get(path, options)
17
+ YammerApi::User.new(response)
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,41 @@
1
+ module YammerApi
2
+ class Base
3
+
4
+ attr_accessor :attrs
5
+ alias :to_hash :attrs
6
+
7
+ # Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key
8
+ #
9
+ # @overload self.lazy_attr_reader(attr)
10
+ # @param attr [Symbol]
11
+ # @overload self.lazy_attr_reader(attrs)
12
+ # @param attrs [Array<Symbol>]
13
+ def self.lazy_attr_reader(*attrs)
14
+ attrs.each do |attribute|
15
+ class_eval do
16
+ define_method attribute do
17
+ @attrs[attribute.to_s]
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ # Initializes a new Base object
24
+ #
25
+ # @param attrs [Hash]
26
+ # @return [YammerApi::Base]
27
+ def initialize(attrs={})
28
+ @attrs = attrs.dup
29
+ end
30
+
31
+ # Initializes a new Base object
32
+ #
33
+ # @param method [String, Symbol] Message to send to the object
34
+ def [](method)
35
+ self.__send__(method.to_sym)
36
+ rescue NoMethodError
37
+ nil
38
+ end
39
+
40
+ end
41
+ end