yamwow 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (27) hide show
  1. data/lib/yamwow/client.rb +30 -14
  2. data/lib/yamwow/facade.rb +41 -13
  3. data/lib/yamwow/messages_responder.rb +35 -0
  4. data/lib/yamwow/praise_messages_responder.rb +48 -0
  5. data/lib/yamwow/requests/groups/group_with_name_request.rb +27 -0
  6. data/lib/yamwow/requests/messages/limited_messages_with_topic_id_request.rb +15 -0
  7. data/lib/yamwow/requests/messages/messages_with_topic_id_request.rb +33 -0
  8. data/lib/yamwow/requests/messages/messages_with_topic_name_request.rb +22 -0
  9. data/lib/yamwow/requests/messages/praise_messages_request.rb +15 -0
  10. data/lib/yamwow/requests/topics/limited_topics_with_prefix_request.rb +24 -0
  11. data/lib/yamwow/requests/topics/topic_with_name_request.rb +25 -0
  12. data/lib/yamwow/requests/topics/topics_request.rb +20 -0
  13. data/lib/yamwow/requests/topics/topics_with_prefix_request.rb +40 -0
  14. data/lib/yamwow/requests/users/current_user_request.rb +16 -0
  15. data/lib/yamwow/requests.rb +1 -1
  16. data/lib/yamwow/throttle.rb +1 -1
  17. data/lib/yamwow/topics_responder.rb +22 -0
  18. data/lib/yamwow.rb +9 -1
  19. metadata +39 -12
  20. data/lib/yamwow/requests/current_user_request.rb +0 -18
  21. data/lib/yamwow/requests/group_with_name_request.rb +0 -29
  22. data/lib/yamwow/requests/messages_with_topic_request.rb +0 -78
  23. data/lib/yamwow/requests/praise_request.rb +0 -52
  24. data/lib/yamwow/requests/topic_with_id_request.rb +0 -17
  25. data/lib/yamwow/requests/topic_with_name_request.rb +0 -31
  26. data/lib/yamwow/requests/topics_request.rb +0 -37
  27. data/lib/yamwow/requests/topics_with_prefix_request.rb +0 -36
data/lib/yamwow/client.rb CHANGED
@@ -4,35 +4,51 @@ require_relative 'throttle'
4
4
  module YamWow
5
5
  class Client
6
6
 
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
11
- @other_throttle = Throttle.new 10, 10
7
+ def initialize(access_token)
8
+ @yam = Yam.new access_token, nil
9
+ @throttles = {
10
+ messages: Throttle.new(10, 30),
11
+ autocomplete: Throttle.new(10, 10),
12
+ other: Throttle.new(10, 10)
13
+ }
12
14
  end
13
15
 
14
- def get_messages(path=nil, params={})
15
- path = path ? File.join('messages', path) : 'messages.json'
16
- get path, params, @message_throttle
16
+ def get_autocomplete(params={})
17
+ get 'autocomplete.json', params
17
18
  end
18
19
 
19
- def get_autocomplete(params={})
20
- get 'autocomplete.json', params, @autocomplete_throttle
20
+ def get_current_user
21
+ get 'users/current.json'
22
+ end
23
+
24
+ def get_messages_about_topic(topic_id, params={})
25
+ get "messages/about_topic/#{topic_id}.json", params
21
26
  end
22
27
 
23
- def get_other(path, params={})
24
- get path, params, @other_throttle
28
+ def get_topic(topic_id)
29
+ get "topics/#{topic_id}.json"
25
30
  end
26
31
 
27
32
  private
28
33
 
29
- def get(path, params, throttle)
34
+ def get(path, params={})
30
35
  path = '/' + path unless path.start_with? '/'
31
- throttle.when_ready do
36
+ throttle(path).when_ready do
32
37
  puts "GET #{path} #{params.inspect}"
33
38
  @yam.get path, params
34
39
  end
35
40
  end
36
41
 
42
+ def throttle(path)
43
+ case
44
+ when path.start_with?('/messages')
45
+ @throttles[:messages]
46
+ when path.start_with?('/autocomplete')
47
+ @throttles[:autocomplete]
48
+ else
49
+ @throttles[:other]
50
+ end
51
+ end
52
+
37
53
  end
38
54
  end
data/lib/yamwow/facade.rb CHANGED
@@ -1,35 +1,63 @@
1
1
  require_relative 'client'
2
2
  require_relative 'requests'
3
+ require_relative 'topics_responder'
4
+ require_relative 'messages_responder'
5
+ require_relative 'praise_messages_responder'
3
6
 
4
7
  module YamWow
5
8
  class Facade
6
9
 
7
- def initialize(oauth_token)
8
- @client = Client.new oauth_token
10
+ def initialize(access_token)
11
+ client = Client.new access_token
12
+ topics_responder = TopicsResponder.new
13
+ messages_responder = MessagesResponder.new
14
+ praise_messages_responder = PraiseMessagesResponder.new messages_responder
15
+
16
+ @group_with_name_request = GroupWithNameRequest.new client
17
+
18
+ limited_topics_with_prefix_request = LimitedTopicsWithPrefixRequest.new client, topics_responder
19
+ @topics_with_prefix_request = TopicsWithPrefixRequest.new topics_responder, limited_topics_with_prefix_request
20
+ @topic_with_name_request = TopicWithNameRequest.new @topics_with_prefix_request
21
+ @topics_request = TopicsRequest.new topics_responder, @topics_with_prefix_request
22
+
23
+ limited_messages_with_topic_id_request = LimitedMessagesWithTopicIdRequest.new client, messages_responder
24
+ @messages_with_topic_id_request = MessagesWithTopicIdRequest.new messages_responder, limited_messages_with_topic_id_request
25
+ @messages_with_topic_name_request = MessagesWithTopicNameRequest.new @topic_with_name_request, @messages_with_topic_id_request
26
+ @praise_messages_request = PraiseMessagesRequest.new praise_messages_responder, @messages_with_topic_name_request
27
+
28
+ @current_user_request = CurrentUserRequest.new client
29
+ end
30
+
31
+ def group_with_name(group_name)
32
+ @group_with_name_request.send group_name
9
33
  end
10
34
 
11
- def messages_with_topic(topic_name, options={})
12
- MessagesWithTopicRequest.new(@client, topic_name, options).send
35
+ def messages_with_topic_id(topic_id)
36
+ @messages_with_topic_id_request.send(topic_id)
13
37
  end
14
38
 
15
- def praises(options={})
16
- PraiseRequest.new(@client, options).send
39
+ def messages_with_topic_name(topic_name)
40
+ @messages_with_topic_name_request.send topic_name
17
41
  end
18
42
 
19
- def topic_with_name(topic_name, options={})
20
- TopicWithNameRequest.new(@client, topic_name, options).send
43
+ def praise_messages
44
+ @praise_messages_request.send
21
45
  end
22
46
 
23
- def topics(options={})
24
- TopicsRequest.new(@client, options).send
47
+ def topic_with_name(topic_name)
48
+ @topic_with_name_request.send topic_name
25
49
  end
26
50
 
27
- def group_with_name(group_name)
28
- GroupWithNameRequest.new(@client, group_name).send
51
+ def topics
52
+ @topics_request.send
53
+ end
54
+
55
+ def topics_with_prefix(topic_prefix)
56
+ @topics_with_prefix_request.send topic_prefix
29
57
  end
30
58
 
31
59
  def current_user
32
- CurrentUserRequest.new(@client).send
60
+ @current_user_request.send
33
61
  end
34
62
 
35
63
  end
@@ -0,0 +1,35 @@
1
+ require 'time'
2
+ require_relative 'response'
3
+
4
+ module YamWow
5
+ class MessagesResponder
6
+
7
+ def create_response(messages, references=[])
8
+ @messages = messages
9
+ @references = references
10
+ remove_comments
11
+ sort_by_most_recent_first
12
+ enhance_messages
13
+ YamWow::Response.new @messages, @references
14
+ end
15
+
16
+ private
17
+
18
+ def remove_comments
19
+ @messages.select! { |m| m['replied_to_id'].nil? }
20
+ end
21
+
22
+ def sort_by_most_recent_first
23
+ @messages.sort! { |x, y| Time.parse(y['created_at']) <=> Time.parse(x['created_at']) }
24
+ end
25
+
26
+ def enhance_messages
27
+ @messages.each { |m| enhance_message m }
28
+ end
29
+
30
+ def enhance_message(message)
31
+ message['sender_tag'] = "[[user:#{message['sender_id']}]]" if message['sender_id']
32
+ end
33
+
34
+ end
35
+ end
@@ -0,0 +1,48 @@
1
+ module YamWow
2
+ class PraiseMessagesResponder
3
+
4
+ def initialize(messages_responder)
5
+ @messages_responder = messages_responder
6
+ end
7
+
8
+ def create_response(messages, references=[])
9
+ @messages = messages
10
+ @references = references
11
+ enhance_messages
12
+ @messages_responder.create_response @messages, @references
13
+ end
14
+
15
+ private
16
+
17
+ def enhance_messages
18
+ @messages.each { |m| enhance_message m }
19
+ end
20
+
21
+ def enhance_message(message)
22
+ praise_attachment = praise_attachment message
23
+ praise_attachment ? enhance_message_with_praise_attachment(message, praise_attachment) : enhance_normal_message(message)
24
+ end
25
+
26
+ def enhance_message_with_praise_attachment(message, praise_attachment)
27
+ message['praise_message'] = praise_attachment['name'].match(/Praise: (.+)/m)[1]
28
+ message['praise_recipient_names'] = split_recipients(message['body']['plain']).map { |r| r.gsub '@', '' }.join ', '
29
+ message['praise_recipient_tags'] = split_recipients(message['body']['parsed']).join ', '
30
+ end
31
+
32
+ def enhance_normal_message(message)
33
+ message['praise_message'] = message['body']['parsed']
34
+ end
35
+
36
+ def praise_attachment(message)
37
+ attachments = message['attachments'] || []
38
+ attachments.select { |a| a['type'] == 'ymodule' }.select { |a| a['ymodule']['app_id'] == 'praise' }.first
39
+ end
40
+
41
+ def split_recipients(recipients)
42
+ recipients = recipients.to_s.include?('praised') ? recipients.match(/praised (.+)/)[1] : ''
43
+ recipients.gsub!(' and ', ', ')
44
+ recipients.split(', ')
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,27 @@
1
+ require_relative '../../response'
2
+
3
+ module YamWow
4
+ class GroupWithNameRequest
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def send(group_name)
11
+ @group_name = group_name
12
+ Response.new group_with_name
13
+ end
14
+
15
+ private
16
+
17
+ def group_with_name
18
+ groups_with_prefix.select { |g| g['full_name'].casecmp(@group_name) == 0 }.first
19
+ end
20
+
21
+ def groups_with_prefix
22
+ data = @client.get_autocomplete prefix: 'to:' + @group_name
23
+ data['groups'] || []
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ module YamWow
2
+ class LimitedMessagesWithTopicIdRequest
3
+
4
+ def initialize(client, responder)
5
+ @client = client
6
+ @responder = responder
7
+ end
8
+
9
+ def send(topic_id, options={})
10
+ data = @client.get_messages_about_topic topic_id, threaded: true, older_than: options[:older_than_id]
11
+ @responder.create_response data['messages'], data['references']
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,33 @@
1
+ module YamWow
2
+ class MessagesWithTopicIdRequest
3
+
4
+ def initialize(responder, limited_messages_with_topic_id_request)
5
+ @responder = responder
6
+ @limited_messages_with_topic_id_request = limited_messages_with_topic_id_request
7
+ end
8
+
9
+ def send(topic_id)
10
+ @topic_id = topic_id
11
+ @messages = []
12
+ @references = []
13
+ add_messages
14
+ @responder.create_response @messages, @references
15
+ end
16
+
17
+ private
18
+
19
+ def add_messages
20
+ response = @limited_messages_with_topic_id_request.send(@topic_id, older_than_id: oldest_message_id)
21
+ @messages += response.data
22
+ @references += response.reference_data
23
+ add_messages unless response.data.empty?
24
+ end
25
+
26
+ private
27
+
28
+ def oldest_message_id
29
+ @messages.empty? ? nil : @messages.last['id']
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ module YamWow
2
+ class MessagesWithTopicNameRequest
3
+
4
+ def initialize(topic_with_name_request, messages_with_topic_id_request)
5
+ @messages_with_topic_id_request = messages_with_topic_id_request
6
+ @topic_with_name_request = topic_with_name_request
7
+ end
8
+
9
+ def send(topic_name)
10
+ @topic_name = topic_name
11
+ @messages_with_topic_id_request.send topic_id
12
+ end
13
+
14
+ private
15
+
16
+ def topic_id
17
+ response = @topic_with_name_request.send @topic_name
18
+ response.data['id'] if response.data
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,15 @@
1
+ module YamWow
2
+ class PraiseMessagesRequest
3
+
4
+ def initialize(responder, messages_with_topic_name_request)
5
+ @responder = responder
6
+ @messages_with_topic_name_request = messages_with_topic_name_request
7
+ end
8
+
9
+ def send
10
+ r = @messages_with_topic_name_request.send 'praise'
11
+ @responder.create_response r.data, r.reference_data
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ module YamWow
2
+ class LimitedTopicsWithPrefixRequest
3
+
4
+ LIMIT = 50
5
+
6
+ def initialize(client, responder)
7
+ @client = client
8
+ @responder = responder
9
+ end
10
+
11
+ def send(topic_prefix)
12
+ @topic_prefix = topic_prefix
13
+ @responder.create_response topics
14
+ end
15
+
16
+ private
17
+
18
+ def topics
19
+ data = @client.get_autocomplete prefix: @topic_prefix
20
+ data['topics'] || []
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ module YamWow
2
+ class TopicWithNameRequest
3
+
4
+ def initialize(topics_with_prefix_request)
5
+ @topics_with_prefix_request = topics_with_prefix_request
6
+ end
7
+
8
+ def send(topic_name)
9
+ @topic_name = topic_name
10
+ Response.new topic_with_name
11
+ end
12
+
13
+ private
14
+
15
+ def topic_with_name
16
+ topics_with_prefix.select { |t| t['name'].casecmp(@topic_name) == 0 }.first
17
+ end
18
+
19
+ def topics_with_prefix
20
+ response = @topics_with_prefix_request.send @topic_name
21
+ response.data || []
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ module YamWow
2
+ class TopicsRequest
3
+
4
+ def initialize(responder, topics_with_prefix_request)
5
+ @responder = responder
6
+ @topics_with_prefix_request = topics_with_prefix_request
7
+ @prefixes = ('0'..'9').to_a + ('a'..'z').to_a
8
+ end
9
+
10
+ def send
11
+ topics = []
12
+ @prefixes.each do |prefix|
13
+ response = @topics_with_prefix_request.send prefix
14
+ topics += response.data
15
+ end
16
+ @responder.create_response topics
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,40 @@
1
+ module YamWow
2
+ class TopicsWithPrefixRequest
3
+
4
+ def initialize(responder, limited_topics_with_prefix_request)
5
+ @responder = responder
6
+ @limited_topics_with_prefix_request = limited_topics_with_prefix_request
7
+ end
8
+
9
+ def send(topic_prefix)
10
+ @topic_prefix = topic_prefix
11
+ @topics = []
12
+ add_topics(@topic_prefix) { find_topics @topic_prefix }
13
+ @responder.create_response @topics
14
+ end
15
+
16
+ private
17
+
18
+ def find_topics(prefix)
19
+ range.each do |suffix|
20
+ next_prefix = prefix + suffix
21
+ add_topics(next_prefix) { find_topics next_prefix }
22
+ end
23
+ end
24
+
25
+ def range
26
+ first_char = @topic_prefix[0]
27
+ first_char.to_i.to_s == first_char ? ('0'..'9') : ('a'..'z')
28
+ end
29
+
30
+ def add_topics(prefix)
31
+ response = @limited_topics_with_prefix_request.send prefix
32
+ topics = response.data || []
33
+ yield if topics.length == LimitedTopicsWithPrefixRequest::LIMIT
34
+ @topics += [topics].flatten
35
+ end
36
+
37
+ end
38
+ end
39
+
40
+
@@ -0,0 +1,16 @@
1
+ require_relative '../../response'
2
+
3
+ module YamWow
4
+ class CurrentUserRequest
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ def send
11
+ data = @client.get_current_user
12
+ Response.new data
13
+ end
14
+
15
+ end
16
+ end
@@ -1 +1 @@
1
- Dir[File.expand_path(File.dirname(__FILE__) + '/requests/*.rb')].each { |f| require f }
1
+ Dir[File.expand_path(File.dirname(__FILE__) + '/requests/**/*_request.rb')].each { |f| require f }
@@ -30,7 +30,7 @@ module YamWow
30
30
  return 0 unless oldest_applicable
31
31
 
32
32
  age = Time.now - oldest_applicable
33
- (@duration_in_seconds - age).ceil
33
+ (@duration_in_seconds - age + 0.5).round 2
34
34
  end
35
35
 
36
36
  end
@@ -0,0 +1,22 @@
1
+ require_relative 'response'
2
+
3
+ module YamWow
4
+ class TopicsResponder
5
+
6
+ def create_response(topics)
7
+ @topics = topics
8
+ remove_duplicates
9
+ sort_by_name
10
+ Response.new @topics
11
+ end
12
+
13
+ def remove_duplicates
14
+ @topics.uniq! { |t| t['id'] }
15
+ end
16
+
17
+ def sort_by_name
18
+ @topics.sort! { |x, y| x['full_name'].to_s.downcase <=> y['full_name'].to_s.downcase }
19
+ end
20
+
21
+ end
22
+ end
data/lib/yamwow.rb CHANGED
@@ -1,2 +1,10 @@
1
1
  ENV['SSL_CERT_FILE'] ||= File.expand_path(File.dirname(__FILE__) + '/cacert.pem')
2
- require_relative 'yamwow/facade'
2
+ require_relative 'yamwow/facade'
3
+
4
+ module YamWow
5
+ class << self
6
+ def new(access_token)
7
+ Facade.new access_token
8
+ end
9
+ end
10
+ end
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.1.0
4
+ version: 0.1.1
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-02-16 00:00:00.000000000 Z
12
+ date: 2013-03-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: yam
16
- requirement: &70201996950260 !ruby/object:Gem::Requirement
16
+ requirement: &70253074602400 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,29 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70201996950260
24
+ version_requirements: *70253074602400
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70253074601420 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70253074601420
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec-mocks
38
+ requirement: &70253074600560 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70253074600560
25
47
  description:
26
48
  email: matthew-github@matthewriley.name
27
49
  executables: []
@@ -32,17 +54,22 @@ files:
32
54
  - lib/yamwow/client.rb
33
55
  - lib/yamwow/enumerable_extensions.rb
34
56
  - lib/yamwow/facade.rb
35
- - lib/yamwow/requests/current_user_request.rb
36
- - lib/yamwow/requests/group_with_name_request.rb
37
- - lib/yamwow/requests/messages_with_topic_request.rb
38
- - lib/yamwow/requests/praise_request.rb
39
- - lib/yamwow/requests/topic_with_id_request.rb
40
- - lib/yamwow/requests/topic_with_name_request.rb
41
- - lib/yamwow/requests/topics_request.rb
42
- - lib/yamwow/requests/topics_with_prefix_request.rb
57
+ - lib/yamwow/messages_responder.rb
58
+ - lib/yamwow/praise_messages_responder.rb
59
+ - lib/yamwow/requests/groups/group_with_name_request.rb
60
+ - lib/yamwow/requests/messages/limited_messages_with_topic_id_request.rb
61
+ - lib/yamwow/requests/messages/messages_with_topic_id_request.rb
62
+ - lib/yamwow/requests/messages/messages_with_topic_name_request.rb
63
+ - lib/yamwow/requests/messages/praise_messages_request.rb
64
+ - lib/yamwow/requests/topics/limited_topics_with_prefix_request.rb
65
+ - lib/yamwow/requests/topics/topic_with_name_request.rb
66
+ - lib/yamwow/requests/topics/topics_request.rb
67
+ - lib/yamwow/requests/topics/topics_with_prefix_request.rb
68
+ - lib/yamwow/requests/users/current_user_request.rb
43
69
  - lib/yamwow/requests.rb
44
70
  - lib/yamwow/response.rb
45
71
  - lib/yamwow/throttle.rb
72
+ - lib/yamwow/topics_responder.rb
46
73
  - lib/yamwow.rb
47
74
  homepage: http://rubygems.org/gems/yamwow
48
75
  licenses: []
@@ -1,18 +0,0 @@
1
- #GET https://www.yammer.com/api/v1/users/current.json
2
-
3
- require_relative '../response'
4
-
5
- module YamWow
6
- class CurrentUserRequest
7
-
8
- def initialize(client)
9
- @client = client
10
- end
11
-
12
- def send
13
- user = @client.get_other 'users/current.json'
14
- Response.new user
15
- end
16
-
17
- end
18
- end
@@ -1,29 +0,0 @@
1
- require_relative '../response'
2
-
3
- module YamWow
4
- class GroupWithNameRequest
5
-
6
- def initialize(client, group_name)
7
- @client = client
8
- @group_name = group_name.downcase.gsub(/[^0-9a-z]/i, '')
9
- end
10
-
11
- def send
12
- groups = get_groups
13
- group = first_group_with_matching_name groups
14
- Response.new group
15
- end
16
-
17
- private
18
-
19
- def get_groups
20
- r = @client.get_autocomplete prefix: 'to:' + @group_name
21
- r['groups'] || []
22
- end
23
-
24
- def first_group_with_matching_name(groups)
25
- groups.select { |g| g['name'] == @group_name }.first
26
- end
27
-
28
- end
29
- end
@@ -1,78 +0,0 @@
1
- require 'time'
2
- require_relative '../response'
3
- require_relative 'topic_with_name_request'
4
-
5
- module YamWow
6
- class MessagesWithTopicRequest
7
-
8
- def initialize(client, topic_name, options={})
9
- @client = client
10
- @topic_name = topic_name
11
- @options = options
12
- end
13
-
14
- def send
15
- @messages = []
16
- @references = []
17
- loop_finished = topic_id.nil?
18
-
19
- until loop_finished
20
- response_data = send_messages_request
21
- before_message_count = @messages.length
22
- add_messages response_data['messages'], response_data['references']
23
- after_message_count = @messages.length
24
- no_more_messages = before_message_count == after_message_count
25
- loop_finished = no_more_messages || limit_reached
26
- end
27
-
28
- @messages.sort! { |x, y| Time.parse(y['created_at']) <=> Time.parse(x['created_at']) }
29
-
30
- Response.new @messages, @references
31
- end
32
-
33
- private
34
-
35
- def send_messages_request
36
- @client.get_messages "about_topic/#{topic_id}.json", threaded: true, older_than: oldest_message_id
37
- end
38
-
39
- def topic_id
40
- @topic_id ||= get_topic_id
41
- end
42
-
43
- def get_topic_id
44
- request = TopicWithNameRequest.new @client, @topic_name
45
- response = request.send
46
- topic = response.data
47
- topic.id if topic
48
- end
49
-
50
- def oldest_message_id
51
- sorted_messages = @messages.sort { |x, y| Time.parse(y['created_at']) <=> Time.parse(x['created_at']) }
52
- last_message = sorted_messages.last
53
- last_message.id if last_message
54
- end
55
-
56
- def add_messages(messages_data, reference_data)
57
- @messages += build_messages messages_data, reference_data
58
- @messages = @messages[0..limit_count-1] if limit_count > 0
59
- @references += reference_data
60
- end
61
-
62
- def build_messages(messages_data, reference_data)
63
- messages_data.select { |m| m['replied_to_id'].nil? }.each do |m|
64
- m['like_count'] = m['liked_by']['count'].to_i
65
- m['sender_tag'] = "[[user:#{m['sender_id']}]]"
66
- end
67
- end
68
-
69
- def limit_reached
70
- limit_count > 0 ? @messages.length == limit_count : false
71
- end
72
-
73
- def limit_count
74
- @options[:limit_count] ? @options[:limit_count].to_i : 0
75
- end
76
-
77
- end
78
- end
@@ -1,52 +0,0 @@
1
- require_relative 'messages_with_topic_request'
2
-
3
- module YamWow
4
- class PraiseRequest
5
-
6
- def initialize(client, options={})
7
- @client = client
8
- @options = options
9
- end
10
-
11
- def send
12
- get_messages_with_praise_topic
13
- end
14
-
15
- def get_messages_with_praise_topic
16
- r = MessagesWithTopicRequest.new(@client, 'praise', @options).send
17
- r.data.each do |m|
18
- m['praise_message'] = reason m
19
- m['praise_recipient_names'] = recipient_names m
20
- m['praise_recipient_tags'] = recipient_tags m
21
- end
22
- r
23
- end
24
-
25
- private
26
-
27
- def reason(message)
28
- begin
29
- attachments = message['attachments'] || []
30
- praise_attachment = attachments.select { |a| a['type'] == 'ymodule' }.select { |a| a['ymodule']['app_id'] == 'praise' }.first
31
- praise_attachment ? praise_attachment['name'].match(/Praise: (.+)/m)[1] : message['body']['parsed']
32
- rescue
33
- '(failed to extract reason)'
34
- end
35
- end
36
-
37
- def recipient_names(message)
38
- split_recipients(message['body']['plain']).map { |r| r.gsub '@', '' }.join ', '
39
- end
40
-
41
- def recipient_tags(message)
42
- split_recipients(message['body']['parsed']).join ', '
43
- end
44
-
45
- def split_recipients(recipients)
46
- recipients = recipients.include?('praised') ? recipients.match(/praised (.+)/)[1] : ''
47
- recipients.gsub!(' and ', ', ')
48
- recipients.split(', ')
49
- end
50
-
51
- end
52
- end
@@ -1,17 +0,0 @@
1
- require_relative '../response'
2
-
3
- module YamWow
4
- class TopicWithIdRequest
5
-
6
- def initialize(client, topic_id)
7
- @client = client
8
- @topic_id = topic_id
9
- end
10
-
11
- def send
12
- topic = @client.get_other "topics/#{@topic_id}.json"
13
- Response.new topic
14
- end
15
-
16
- end
17
- end
@@ -1,31 +0,0 @@
1
- require_relative '../response'
2
- require_relative 'topics_with_prefix_request'
3
-
4
- module YamWow
5
- class TopicWithNameRequest
6
-
7
- def initialize(client, topic_name, options={})
8
- @client = client
9
- @topic_name = topic_name
10
- @detailed = options[:detailed]
11
- end
12
-
13
- def send
14
- topics = get_topics
15
- topic = first_topic_with_matching_name topics
16
- Response.new topic
17
- end
18
-
19
- private
20
-
21
- def get_topics
22
- response = TopicsWithPrefixRequest.new(@client, @topic_name, detailed: @detailed).send
23
- response.data || []
24
- end
25
-
26
- def first_topic_with_matching_name(topics)
27
- topics.select { |t| t['name'].casecmp(@topic_name) == 0 }.first
28
- end
29
-
30
- end
31
- end
@@ -1,37 +0,0 @@
1
- require_relative '../response'
2
- require_relative 'topics_with_prefix_request'
3
-
4
- module YamWow
5
- class TopicsRequest
6
-
7
- def initialize(client, options={})
8
- @client = client
9
- @detailed = options[:detailed]
10
- end
11
-
12
- def send
13
- @topics = []
14
- process_range '0', '9'
15
- process_range 'a', 'z'
16
- @topics.uniq! { |t| t['id'] }
17
- @topics.sort! { |x, y| x['name'].downcase <=> y['name'].downcase }
18
- Response.new @topics
19
- end
20
-
21
- private
22
-
23
- def process_range(first, last, current=nil)
24
- ((current.to_s + first)..(current.to_s + last)).each do |prefix|
25
- topics = send_topics_with_prefix_request prefix
26
- @topics += topics
27
- process_range first, last, prefix if topics.length == 50
28
- end
29
- end
30
-
31
- def send_topics_with_prefix_request(topic_prefix)
32
- response = TopicsWithPrefixRequest.new(@client, topic_prefix, detailed: @detailed).send
33
- response.data || []
34
- end
35
-
36
- end
37
- end
@@ -1,36 +0,0 @@
1
- require_relative '../response'
2
- require_relative 'topic_with_id_request'
3
-
4
- module YamWow
5
- class TopicsWithPrefixRequest
6
-
7
- def initialize(client, topic_prefix, options={})
8
- @client = client
9
- @topic_prefix = topic_prefix
10
- @detailed = options[:detailed]
11
- end
12
-
13
- def send
14
- topics = get_topics
15
- topics.each { |t| add_details t } if @detailed
16
- Response.new topics
17
- end
18
-
19
- private
20
-
21
- def get_topics
22
- response = @client.get_autocomplete(prefix: @topic_prefix)
23
- response['topics'] || []
24
- end
25
-
26
- def add_details(topic)
27
- r = get_detailed_topic topic['id']
28
- topic.merge! r.data
29
- end
30
-
31
- def get_detailed_topic(topic_id)
32
- TopicWithIdRequest.new(@client, topic_id).send
33
- end
34
-
35
- end
36
- end