yamwow 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/yamwow/client.rb CHANGED
@@ -1,32 +1,32 @@
1
- require 'oauth'
2
- require 'json'
1
+ require 'yam'
2
+ require_relative 'throttle'
3
3
 
4
4
  module YamWow
5
5
  class Client
6
6
 
7
- def initialize(oauth_credentials)
8
- @oauth_credentials = oauth_credentials
7
+ def initialize(oauth_token)
8
+ @yam = Yam.new oauth_token, nil
9
+ @message_throttle = Throttle.new 10, 30
10
+ @autocomplete_throttle = Throttle.new 10, 10
9
11
  end
10
12
 
11
- def get(url, throttle)
12
- throttle.when_ready do
13
- url = url.start_with?('http') ? url : File.join('https://www.yammer.com/api/v1', url)
14
- puts "GET #{url}"
15
- response = access_token.get url
16
- JSON.parse response.body
17
- end
13
+ def get_messages(path=nil, params={})
14
+ path = path ? File.join('messages', path) : 'messages.json'
15
+ get path, params, @message_throttle
18
16
  end
19
17
 
20
- private
21
-
22
- def access_token
23
- @access_token ||= get_access_token
18
+ def get_autocomplete(params={})
19
+ get 'autocomplete.json', params, @autocomplete_throttle
24
20
  end
25
21
 
26
- def get_access_token
27
- o = @oauth_credentials
28
- consumer = OAuth::Consumer.new o[:consumer_key], o[:consumer_secret], {:site => 'https://www.yammer.com', :scheme => :header}
29
- OAuth::AccessToken.from_hash consumer, :oauth_token => o[:oauth_key], :oauth_token_secret => o[:oauth_secret]
22
+ private
23
+
24
+ def get(path, params, throttle)
25
+ path = '/' + path unless path.start_with? '/'
26
+ throttle.when_ready do
27
+ puts "GET #{path} #{params.inspect}"
28
+ @yam.get path, params
29
+ end
30
30
  end
31
31
 
32
32
  end
data/lib/yamwow/facade.rb CHANGED
@@ -1,21 +1,33 @@
1
1
  module YamWow
2
2
  class Facade
3
3
 
4
- def initialize(oauth_credentials)
5
- @client = Client.new oauth_credentials
4
+ def initialize(oauth_token)
5
+ @client = Client.new oauth_token
6
6
  end
7
7
 
8
8
  def messages_for_topic(topic_name, options={})
9
- request = MessagesByTopicRequest.new @client
10
- response = request.send topic_name, options
9
+ request = MessagesForTopicRequest.new @client, topic_name, options
10
+ response = request.send
11
11
  response.messages
12
12
  end
13
13
 
14
14
  def praises(options={})
15
- request = PraiseRequest.new @client
16
- response = request.send options
15
+ request = PraiseRequest.new @client, options
16
+ response = request.send
17
17
  response.praises
18
18
  end
19
19
 
20
+ def topic(topic_name)
21
+ request = TopicRequest.new @client, topic_name
22
+ response = request.send
23
+ response.topic
24
+ end
25
+
26
+ def group(group_name)
27
+ request = GroupRequest.new @client, group_name
28
+ response = request.send
29
+ response.group
30
+ end
31
+
20
32
  end
21
33
  end
@@ -0,0 +1,5 @@
1
+ module YamWow
2
+ class Group
3
+ attr_accessor :id, :name
4
+ end
5
+ end
@@ -0,0 +1,47 @@
1
+ require_relative 'group'
2
+ require_relative 'group_response'
3
+
4
+ module YamWow
5
+ class GroupRequest
6
+
7
+ def initialize(client, group_name)
8
+ @client = client
9
+ @group_name = group_name.gsub /[^0-9a-z]/i, ''
10
+ end
11
+
12
+ def send
13
+ group_data = get_group_data
14
+ group_data ? build_response(group_data) : build_empty_response
15
+ end
16
+
17
+ private
18
+
19
+ def get_group_data
20
+ data = send_autocomplete_request
21
+ topics = data['groups'] || []
22
+ topics.select { |t| t['name'].downcase == @group_name.downcase }[0]
23
+ end
24
+
25
+ def send_autocomplete_request
26
+ prefix = 'to:' + @group_name.downcase
27
+ @client.get_autocomplete prefix: prefix
28
+ end
29
+
30
+ def build_empty_response
31
+ GroupResponse.new nil
32
+ end
33
+
34
+ def build_response(group_data)
35
+ group = build_group group_data
36
+ GroupResponse.new group
37
+ end
38
+
39
+ def build_group(group_data)
40
+ g = YamWow::Group.new
41
+ g.id = group_data['id']
42
+ g.name = group_data['full_name']
43
+ g
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,11 @@
1
+ module YamWow
2
+ class GroupResponse
3
+
4
+ attr_reader :group
5
+
6
+ def initialize(group)
7
+ @group = group
8
+ end
9
+
10
+ end
11
+ end
@@ -3,73 +3,30 @@ require 'time'
3
3
  module YamWow
4
4
  class Message
5
5
 
6
- def initialize(message_data, reference_data)
7
- @message_data = message_data
8
- @reference_data = reference_data
9
- end
10
-
11
- def id
12
- @message_data['id']
13
- end
6
+ attr_accessor :id, :plain_body, :parsed_body, :date_created, :like_count, :url, :sender, :reply_to_id, :attachments
14
7
 
15
8
  def sender_tag
16
- "[[user:#{sender_id}]]"
9
+ "[[user:#{@sender.id}]]"
17
10
  end
18
11
 
19
12
  def sender_name
20
- get_name sender_id
21
- end
22
-
23
- def plain_body
24
- body['plain']
13
+ @sender.name
25
14
  end
26
15
 
27
16
  def plain_body_single_line
28
- single_line plain_body
29
- end
30
-
31
- def parsed_body
32
- body['parsed']
17
+ single_line @plain_body
33
18
  end
34
19
 
35
20
  def parsed_body_single_line
36
- single_line parsed_body
37
- end
38
-
39
- def date_created
40
- Time.parse @message_data['created_at']
41
- end
42
-
43
- def like_count
44
- @message_data['liked_by']['count'].to_i
45
- end
46
-
47
- def url
48
- @message_data['web_url']
49
- end
50
-
51
- def attachments
52
- @message_data['attachments']
21
+ single_line @parsed_body
53
22
  end
54
23
 
55
24
  def thread_root?
56
- @message_data.select { |m| m['replied_to_id'].nil? }
25
+ @reply_to_id.nil?
57
26
  end
58
27
 
59
28
  private
60
29
 
61
- def sender_id
62
- @message_data['sender_id']
63
- end
64
-
65
- def body
66
- @message_data['body']
67
- end
68
-
69
- def get_name(id)
70
- @reference_data.select { |r| r['id'] == id }[0]['full_name']
71
- end
72
-
73
30
  def single_line(text)
74
31
  text.gsub("\n", ' ').strip
75
32
  end
@@ -0,0 +1,99 @@
1
+ require_relative 'topic_request'
2
+ require_relative 'messages_for_topic_response'
3
+ require_relative 'message'
4
+ require_relative 'user'
5
+
6
+ module YamWow
7
+ class MessagesForTopicRequest
8
+
9
+ def initialize(client, topic_name, options={})
10
+ @client = client
11
+ @topic_name = topic_name
12
+ @options = options
13
+ end
14
+
15
+
16
+ def send
17
+ @messages = []
18
+ loop_finished = false
19
+
20
+ until loop_finished
21
+ response_data = send_messages_request
22
+ before_message_count = @messages.length
23
+ add_messages response_data['messages'], response_data['references']
24
+ after_message_count = @messages.length
25
+ no_more_messages = before_message_count == after_message_count
26
+ loop_finished = no_more_messages || limit_reached
27
+ end
28
+
29
+ build_response
30
+ end
31
+
32
+ private
33
+
34
+ def send_messages_request
35
+ @client.get_messages "about_topic/#{topic_id}.json", threaded: true, older_than: oldest_message_id
36
+ end
37
+
38
+ def topic_id
39
+ @topic_id ||= get_topic_id
40
+ end
41
+
42
+ def get_topic_id
43
+ request = TopicRequest.new @client, @topic_name
44
+ response = request.send
45
+ raise "Topic '#{@topic_name}' was not found." unless response.topic
46
+ response.topic.id
47
+ end
48
+
49
+ def oldest_message_id
50
+ last_message = @messages.sort { |x, y| y.date_created <=> x.date_created }.last
51
+ last_message.id if last_message
52
+ end
53
+
54
+ def add_messages(messages_data, reference_data)
55
+ @messages += build_messages messages_data, reference_data
56
+ @messages = @messages[0..limit_count-1] if limit_count > 0
57
+ end
58
+
59
+ def build_messages(messages_data, reference_data)
60
+ messages_data.map { |m| build_message m, reference_data }.select { |m| m.thread_root? }
61
+ end
62
+
63
+ def build_message(message_data, reference_data)
64
+ d = message_data
65
+ m = Message.new
66
+ m.id = d['id']
67
+ m.plain_body = d['body']['plain']
68
+ m.parsed_body = d['body']['parsed']
69
+ m.date_created = Time.parse d['created_at']
70
+ m.like_count = d['liked_by']['count'].to_i
71
+ m.url = d['web_url']
72
+ m.sender = build_user d['sender_id'], reference_data
73
+ m.reply_to_id = d['replied_to_id']
74
+ m.attachments = d['attachments']
75
+ m
76
+ end
77
+
78
+ def build_user(user_id, reference_data)
79
+ d = reference_data.select { |r| r['id'] == user_id }[0]
80
+ u = User.new
81
+ u.id = d['id']
82
+ u.name = d['full_name']
83
+ u
84
+ end
85
+
86
+ def limit_reached
87
+ limit_count > 0 ? @messages.length == limit_count : false
88
+ end
89
+
90
+ def limit_count
91
+ @options[:limit_count] ? @options[:limit_count].to_i : 0
92
+ end
93
+
94
+ def build_response
95
+ MessagesForTopicResponse.new @messages
96
+ end
97
+
98
+ end
99
+ end
@@ -0,0 +1,11 @@
1
+ module YamWow
2
+ class MessagesForTopicResponse
3
+
4
+ attr_reader :messages
5
+
6
+ def initialize(messages)
7
+ @messages = messages
8
+ end
9
+
10
+ end
11
+ end
@@ -1,15 +1,16 @@
1
- require_relative 'messages_by_topic_request'
1
+ require_relative 'messages_for_topic_request'
2
2
 
3
3
  module YamWow
4
4
  class PraiseRequest
5
5
 
6
- def initialize(client)
6
+ def initialize(client, options={})
7
7
  @client = client
8
+ @options = options
8
9
  end
9
10
 
10
- def send(options={})
11
- messages_by_topic_request = MessagesByTopicRequest.new @client
12
- messages_by_topic_response = messages_by_topic_request.send 'praise', options
11
+ def send
12
+ messages_by_topic_request = MessagesForTopicRequest.new @client, 'praise', @options
13
+ messages_by_topic_response = messages_by_topic_request.send
13
14
  PraiseResponse.new messages_by_topic_response.messages
14
15
  end
15
16
 
@@ -1,13 +1,4 @@
1
1
  module YamWow
2
-
3
- def self.message_throttle
4
- @message_throttle ||= Throttle.new 10, 30
5
- end
6
-
7
- def self.autocomplete_throttle
8
- @autocomplete_throttle ||= Throttle.new 10, 10
9
- end
10
-
11
2
  class Throttle
12
3
 
13
4
  def initialize(max_requests, duration_in_seconds)
@@ -0,0 +1,5 @@
1
+ module YamWow
2
+ class Topic
3
+ attr_accessor :id, :name
4
+ end
5
+ end
@@ -1,18 +1,45 @@
1
+ require_relative 'topic'
1
2
  require_relative 'topic_response'
2
- require_relative 'throttle'
3
3
 
4
4
  module YamWow
5
5
  class TopicRequest
6
6
 
7
- def initialize(client)
7
+ def initialize(client, topic_name)
8
8
  @client = client
9
+ @topic_name = topic_name
9
10
  end
10
11
 
11
- def send(topic_name)
12
- response_data = @client.get "autocomplete.json?prefix=#{CGI::escape topic_name}", YamWow.autocomplete_throttle
13
- topics = response_data['topics'] || []
14
- matching_topic = topics.select { |t| t['name'].downcase == topic_name.downcase }[0]
15
- matching_topic ? TopicResponse.new(matching_topic) : nil
12
+ def send
13
+ topic_data = get_topic_data
14
+ topic_data ? build_response(topic_data) : build_empty_response
15
+ end
16
+
17
+ private
18
+
19
+ def get_topic_data
20
+ data = send_autocomplete_request
21
+ topics = data['topics'] || []
22
+ topics.select { |t| t['name'].downcase == @topic_name.downcase }[0]
23
+ end
24
+
25
+ def send_autocomplete_request
26
+ @client.get_autocomplete prefix: @topic_name.downcase
27
+ end
28
+
29
+ def build_empty_response
30
+ TopicResponse.new nil
31
+ end
32
+
33
+ def build_response(topic_data)
34
+ topic = build_topic topic_data
35
+ TopicResponse.new topic
36
+ end
37
+
38
+ def build_topic(topic_data)
39
+ t = Topic.new
40
+ t.id = topic_data['id']
41
+ t.name = topic_data['name']
42
+ t
16
43
  end
17
44
 
18
45
  end
@@ -1,14 +1,10 @@
1
1
  module YamWow
2
2
  class TopicResponse
3
3
 
4
- attr_reader :topic_data
4
+ attr_reader :topic
5
5
 
6
- def initialize(topic_data)
7
- @topic_data = topic_data
8
- end
9
-
10
- def topic_id
11
- @topic_data['id']
6
+ def initialize(topic)
7
+ @topic = topic
12
8
  end
13
9
 
14
10
  end
@@ -0,0 +1,5 @@
1
+ module YamWow
2
+ class User
3
+ attr_accessor :id, :name
4
+ end
5
+ end
data/lib/yamwow.rb CHANGED
@@ -1 +1,2 @@
1
- Dir[File.expand_path(File.dirname(__FILE__) + '/yamwow/*.rb')].each { |f| require f }
1
+ Dir[File.expand_path(File.dirname(__FILE__) + '/yamwow/*.rb')].each { |f| require f }
2
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yamwow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-27 00:00:00.000000000 Z
12
+ date: 2013-01-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: oauth
16
- requirement: &70128193076360 !ruby/object:Gem::Requirement
15
+ name: yam
16
+ requirement: &70137642787560 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70128193076360
24
+ version_requirements: *70137642787560
25
25
  description:
26
26
  email: matthew-github@matthewriley.name
27
27
  executables: []
@@ -31,15 +31,20 @@ files:
31
31
  - lib/yamwow/client.rb
32
32
  - lib/yamwow/csv_generator.rb
33
33
  - lib/yamwow/facade.rb
34
+ - lib/yamwow/group.rb
35
+ - lib/yamwow/group_request.rb
36
+ - lib/yamwow/group_response.rb
34
37
  - lib/yamwow/message.rb
35
- - lib/yamwow/messages_by_topic_request.rb
36
- - lib/yamwow/messages_by_topic_response.rb
38
+ - lib/yamwow/messages_for_topic_request.rb
39
+ - lib/yamwow/messages_for_topic_response.rb
37
40
  - lib/yamwow/praise.rb
38
41
  - lib/yamwow/praise_request.rb
39
42
  - lib/yamwow/praise_response.rb
40
43
  - lib/yamwow/throttle.rb
44
+ - lib/yamwow/topic.rb
41
45
  - lib/yamwow/topic_request.rb
42
46
  - lib/yamwow/topic_response.rb
47
+ - lib/yamwow/user.rb
43
48
  - lib/yamwow.rb
44
49
  homepage: http://rubygems.org/gems/yamwow
45
50
  licenses: []
@@ -64,5 +69,6 @@ rubyforge_project:
64
69
  rubygems_version: 1.8.15
65
70
  signing_key:
66
71
  specification_version: 3
67
- summary: Exploring the Yammer API.
72
+ summary: YamWow is a Ruby Gem built on top of the Yammer REST API to provide useful,
73
+ higher level operations.
68
74
  test_files: []
@@ -1,57 +0,0 @@
1
- require_relative 'topic_request'
2
- require_relative 'messages_by_topic_response'
3
- require_relative 'throttle'
4
-
5
- module YamWow
6
- class MessagesByTopicRequest
7
-
8
- def initialize(client)
9
- @client = client
10
- end
11
-
12
- def send(topic_name, options={})
13
- topic_id = get_topic_id topic_name
14
- limit_count = (options[:limit_count] || 0).to_i
15
-
16
- oldest_message_id = nil
17
- reference_data = []
18
- messages = []
19
- limit_reached = false
20
-
21
- loop do
22
- response_data = get topic_id, oldest_message_id
23
- reference_data += response_data['references']
24
- message_data = response_data['messages']
25
- messages += message_data.map { |m| Message.new m, reference_data }.select { |m| m.thread_root? }
26
-
27
- if limit_count > 0
28
- messages = messages[0..limit_count-1]
29
- limit_reached = messages.length == limit_count
30
- end
31
-
32
- response = MessagesByTopicResponse.new messages
33
- oldest_message_reached = response.oldest_message_id == oldest_message_id
34
- loop_finished = oldest_message_reached || limit_reached
35
- return response if loop_finished
36
-
37
- oldest_message_id = response.oldest_message_id
38
- end
39
- end
40
-
41
- private
42
-
43
- def get_topic_id(topic_name)
44
- request = TopicRequest.new @client
45
- response = request.send topic_name
46
- raise "Topic '#{topic_name}' was not found." unless response
47
- response.topic_id
48
- end
49
-
50
- def get(topic_id, older_than=nil)
51
- url = "messages/about_topic/#{topic_id}.json?threaded=true"
52
- url += "&older_than=#{older_than}" if older_than
53
- @client.get url, YamWow.message_throttle
54
- end
55
-
56
- end
57
- end
@@ -1,16 +0,0 @@
1
- module YamWow
2
- class MessagesByTopicResponse
3
-
4
- attr_reader :messages
5
-
6
- def initialize(messages)
7
- @messages = messages
8
- end
9
-
10
- def oldest_message_id
11
- last_message = messages.sort { |x, y| y.date_created <=> x.date_created }.last
12
- last_message.id if last_message
13
- end
14
-
15
- end
16
- end