yamwow 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,20 +2,27 @@ require 'oauth'
2
2
  require 'json'
3
3
 
4
4
  module YamWow
5
- class RequestBase
5
+ class Client
6
6
 
7
7
  def initialize(oauth_credentials)
8
8
  @oauth_credentials = oauth_credentials
9
9
  end
10
10
 
11
- protected
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
18
+ end
19
+
20
+ private
12
21
 
13
22
  def access_token
14
23
  @access_token ||= get_access_token
15
24
  end
16
25
 
17
- private
18
-
19
26
  def get_access_token
20
27
  o = @oauth_credentials
21
28
  consumer = OAuth::Consumer.new o[:consumer_key], o[:consumer_secret], {:site => 'https://www.yammer.com', :scheme => :header}
@@ -0,0 +1,21 @@
1
+ module YamWow
2
+ class Facade
3
+
4
+ def initialize(oauth_credentials)
5
+ @client = Client.new oauth_credentials
6
+ end
7
+
8
+ def messages_for_topic(topic_name)
9
+ request = MessagesByTopicRequest.new @client
10
+ response = request.send topic_name
11
+ response.messages
12
+ end
13
+
14
+ def praises
15
+ request = PraiseRequest.new @client
16
+ response = request.send
17
+ response.praises
18
+ end
19
+
20
+ end
21
+ end
@@ -3,9 +3,13 @@ require 'time'
3
3
  module YamWow
4
4
  class Message
5
5
 
6
- def initialize(response_data, references)
7
- @response_data = response_data
8
- @references = references
6
+ def initialize(message_data, reference_data)
7
+ @topic_data = message_data
8
+ @reference_data = reference_data
9
+ end
10
+
11
+ def id
12
+ @topic_data['id']
9
13
  end
10
14
 
11
15
  def sender_tag
@@ -17,41 +21,53 @@ module YamWow
17
21
  end
18
22
 
19
23
  def plain_body
20
- @response_data['body']['plain']
24
+ body['plain']
25
+ end
26
+
27
+ def plain_body_single_line
28
+ single_line plain_body
21
29
  end
22
30
 
23
31
  def parsed_body
24
- @response_data['body']['parsed']
32
+ body['parsed']
25
33
  end
26
34
 
27
35
  def parsed_body_single_line
28
- parsed_body.gsub("\n", ' ').strip
36
+ single_line parsed_body
29
37
  end
30
38
 
31
39
  def date_created
32
- Time.parse @response_data['created_at']
40
+ Time.parse @topic_data['created_at']
33
41
  end
34
42
 
35
43
  def like_count
36
- @response_data['liked_by']['count'].to_i
44
+ @topic_data['liked_by']['count'].to_i
37
45
  end
38
46
 
39
47
  def url
40
- @response_data['web_url']
48
+ @topic_data['web_url']
41
49
  end
42
50
 
43
51
  def attachments
44
- @response_data['attachments']
52
+ @topic_data['attachments']
45
53
  end
46
54
 
47
55
  private
48
56
 
49
57
  def sender_id
50
- @response_data['sender_id']
58
+ @topic_data['sender_id']
59
+ end
60
+
61
+ def body
62
+ @topic_data['body']
51
63
  end
52
64
 
53
65
  def get_name(id)
54
- @references.select { |r| r['id'] == id }[0]['full_name']
66
+ @reference_data.select { |r| r['id'] == id }[0]['full_name']
67
+ end
68
+
69
+ def single_line(text)
70
+ text.gsub("\n", ' ').strip
55
71
  end
56
72
 
57
73
  end
@@ -1,18 +1,45 @@
1
- require_relative 'request_base'
2
1
  require_relative 'topic_request'
3
2
  require_relative 'messages_by_topic_response'
3
+ require_relative 'throttle'
4
4
 
5
5
  module YamWow
6
- class MessagesByTopicRequest < RequestBase
6
+ class MessagesByTopicRequest
7
+
8
+ def initialize(client)
9
+ @client = client
10
+ end
7
11
 
8
12
  def send(topic_name)
9
- topic_request = TopicRequest.new @oauth_credentials
10
- topic_response = topic_request.send topic_name
11
- raise "Topic '#{topic_name}' was not found." unless topic_response
12
- response = access_token.get "https://www.yammer.com/api/v1/messages/about_topic/#{topic_response.topic_id}.json?threaded=true"
13
- response_data = JSON.parse response.body
14
- MessagesByTopicResponse.new response_data
13
+ topic_id = get_topic_id topic_name
14
+
15
+ oldest_message_id = nil
16
+ message_data = []
17
+ reference_data = []
18
+
19
+ loop do
20
+ response_data = get topic_id, oldest_message_id
21
+ message_data += response_data['messages']
22
+ reference_data += response_data['references']
23
+ response = MessagesByTopicResponse.new message_data, reference_data
24
+ return response if response.oldest_message_id == oldest_message_id
25
+ oldest_message_id = response.oldest_message_id
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def get_topic_id(topic_name)
32
+ request = TopicRequest.new @client
33
+ response = request.send topic_name
34
+ raise "Topic '#{topic_name}' was not found." unless response
35
+ response.topic_id
36
+ end
37
+
38
+ def get(topic_id, older_than=nil)
39
+ url = "messages/about_topic/#{topic_id}.json?threaded=true"
40
+ url += "&older_than=#{older_than}" if older_than
41
+ @client.get url, YamWow.message_throttle
15
42
  end
16
43
 
17
44
  end
18
- end
45
+ end
@@ -1,24 +1,26 @@
1
1
  module YamWow
2
2
  class MessagesByTopicResponse
3
3
 
4
- attr_reader :response_data
4
+ attr_reader :message_data
5
5
 
6
- def initialize(response_data)
7
- @response_data = response_data
6
+ def initialize(message_data, reference_data)
7
+ @topic_data = message_data
8
+ @reference_data = reference_data
8
9
  end
9
10
 
10
11
  def messages
11
- root_messages.map { |m| Message.new m, references }
12
+ @messages ||= root_messages.map { |m| Message.new m, @reference_data }
13
+ end
14
+
15
+ def oldest_message_id
16
+ last_message = messages.sort { |x, y| y.date_created <=> x.date_created }.last
17
+ last_message.id if last_message
12
18
  end
13
19
 
14
20
  private
15
21
 
16
22
  def root_messages
17
- @response_data['messages'].select { |m| m['replied_to_id'].nil? }
18
- end
19
-
20
- def references
21
- @response_data['references']
23
+ @topic_data.select { |m| m['replied_to_id'].nil? }
22
24
  end
23
25
 
24
26
  end
@@ -1,11 +1,14 @@
1
1
  require_relative 'messages_by_topic_request'
2
- require_relative 'praise_response'
3
2
 
4
3
  module YamWow
5
- class PraiseRequest < RequestBase
4
+ class PraiseRequest
5
+
6
+ def initialize(client)
7
+ @client = client
8
+ end
6
9
 
7
10
  def send
8
- messages_by_topic_request = MessagesByTopicRequest.new @oauth_credentials
11
+ messages_by_topic_request = MessagesByTopicRequest.new @client
9
12
  messages_by_topic_response = messages_by_topic_request.send 'praise'
10
13
  PraiseResponse.new messages_by_topic_response.messages
11
14
  end
@@ -8,36 +8,8 @@ module YamWow
8
8
  end
9
9
 
10
10
  def praises
11
- @messages.map { |m| Praise.new m }
11
+ @praises ||= @messages.map { |m| Praise.new m }
12
12
  end
13
13
 
14
14
  end
15
- end
16
-
17
- #require_relative 'praise'
18
- #
19
- #module YamWow
20
- # class PraiseResponse
21
- #
22
- # attr_reader :response_data
23
- #
24
- # def initialize(response_data)
25
- # @response_data = response_data
26
- # end
27
- #
28
- # def praises
29
- # root_messages.map { |m| Praise.new m, references }
30
- # end
31
- #
32
- # private
33
- #
34
- # def root_messages
35
- # @response_data['messages'].select { |m| m['replied_to_id'].nil? }
36
- # end
37
- #
38
- # def references
39
- # @response_data['references']
40
- # end
41
- #
42
- # end
43
- #end
15
+ end
@@ -0,0 +1,46 @@
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
+ class Throttle
12
+
13
+ def initialize(max_requests, duration_in_seconds)
14
+ @request_log = []
15
+ @max_requests = max_requests
16
+ @duration_in_seconds = duration_in_seconds
17
+ end
18
+
19
+ def when_ready
20
+ sleep_if_limit_reached
21
+ @request_log << Time.now
22
+ yield
23
+ end
24
+
25
+ private
26
+
27
+ def sleep_if_limit_reached
28
+ seconds_to_sleep = calculate_seconds_to_sleep
29
+ return unless seconds_to_sleep > 0
30
+ puts "Rate limit reached. Sleeping for #{seconds_to_sleep} seconds..."
31
+ sleep seconds_to_sleep
32
+ end
33
+
34
+ def calculate_seconds_to_sleep
35
+ log_index = @request_log.length - @max_requests
36
+ return 0 if log_index < 0
37
+
38
+ oldest_applicable = @request_log[log_index]
39
+ return 0 unless oldest_applicable
40
+
41
+ age = Time.now - oldest_applicable
42
+ (@duration_in_seconds - age).ceil
43
+ end
44
+
45
+ end
46
+ end
@@ -1,12 +1,15 @@
1
- require_relative 'request_base'
2
1
  require_relative 'topic_response'
2
+ require_relative 'throttle'
3
3
 
4
4
  module YamWow
5
- class TopicRequest < RequestBase
5
+ class TopicRequest
6
+
7
+ def initialize(client)
8
+ @client = client
9
+ end
6
10
 
7
11
  def send(topic_name)
8
- response = access_token.get "https://www.yammer.com/api/v1/autocomplete.json?prefix=#{topic_name}"
9
- response_data = JSON.parse response.body
12
+ response_data = @client.get "autocomplete.json?prefix=#{CGI::escape topic_name}", YamWow.autocomplete_throttle
10
13
  topics = response_data['topics'] || []
11
14
  matching_topic = topics.select { |t| t['name'].downcase == topic_name.downcase }[0]
12
15
  matching_topic ? TopicResponse.new(matching_topic) : nil
@@ -1,14 +1,14 @@
1
1
  module YamWow
2
2
  class TopicResponse
3
3
 
4
- attr_reader :response_data
4
+ attr_reader :topic_data
5
5
 
6
- def initialize(response_data)
7
- @response_data = response_data
6
+ def initialize(topic_data)
7
+ @topic_data = topic_data
8
8
  end
9
9
 
10
10
  def topic_id
11
- @response_data['id']
11
+ @topic_data['id']
12
12
  end
13
13
 
14
14
  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.0.2
4
+ version: 0.0.3
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-24 00:00:00.000000000 Z
12
+ date: 2013-01-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: oauth
16
- requirement: &70299510551020 !ruby/object:Gem::Requirement
16
+ requirement: &70183684306860 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,21 +21,23 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70299510551020
24
+ version_requirements: *70183684306860
25
25
  description:
26
26
  email: matthew-github@matthewriley.name
27
27
  executables: []
28
28
  extensions: []
29
29
  extra_rdoc_files: []
30
30
  files:
31
+ - lib/yamwow/client.rb
31
32
  - lib/yamwow/csv_generator.rb
33
+ - lib/yamwow/facade.rb
32
34
  - lib/yamwow/message.rb
33
35
  - lib/yamwow/messages_by_topic_request.rb
34
36
  - lib/yamwow/messages_by_topic_response.rb
35
37
  - lib/yamwow/praise.rb
36
38
  - lib/yamwow/praise_request.rb
37
39
  - lib/yamwow/praise_response.rb
38
- - lib/yamwow/request_base.rb
40
+ - lib/yamwow/throttle.rb
39
41
  - lib/yamwow/topic_request.rb
40
42
  - lib/yamwow/topic_response.rb
41
43
  - lib/yamwow.rb