yamwow 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/yamwow/facade.rb CHANGED
@@ -5,15 +5,15 @@ module YamWow
5
5
  @client = Client.new oauth_credentials
6
6
  end
7
7
 
8
- def messages_for_topic(topic_name)
8
+ def messages_for_topic(topic_name, options={})
9
9
  request = MessagesByTopicRequest.new @client
10
- response = request.send topic_name
10
+ response = request.send topic_name, options
11
11
  response.messages
12
12
  end
13
13
 
14
- def praises
14
+ def praises(options={})
15
15
  request = PraiseRequest.new @client
16
- response = request.send
16
+ response = request.send options
17
17
  response.praises
18
18
  end
19
19
 
@@ -4,12 +4,12 @@ module YamWow
4
4
  class Message
5
5
 
6
6
  def initialize(message_data, reference_data)
7
- @topic_data = message_data
7
+ @message_data = message_data
8
8
  @reference_data = reference_data
9
9
  end
10
10
 
11
11
  def id
12
- @topic_data['id']
12
+ @message_data['id']
13
13
  end
14
14
 
15
15
  def sender_tag
@@ -37,29 +37,33 @@ module YamWow
37
37
  end
38
38
 
39
39
  def date_created
40
- Time.parse @topic_data['created_at']
40
+ Time.parse @message_data['created_at']
41
41
  end
42
42
 
43
43
  def like_count
44
- @topic_data['liked_by']['count'].to_i
44
+ @message_data['liked_by']['count'].to_i
45
45
  end
46
46
 
47
47
  def url
48
- @topic_data['web_url']
48
+ @message_data['web_url']
49
49
  end
50
50
 
51
51
  def attachments
52
- @topic_data['attachments']
52
+ @message_data['attachments']
53
+ end
54
+
55
+ def thread_root?
56
+ @message_data.select { |m| m['replied_to_id'].nil? }
53
57
  end
54
58
 
55
59
  private
56
60
 
57
61
  def sender_id
58
- @topic_data['sender_id']
62
+ @message_data['sender_id']
59
63
  end
60
64
 
61
65
  def body
62
- @topic_data['body']
66
+ @message_data['body']
63
67
  end
64
68
 
65
69
  def get_name(id)
@@ -9,19 +9,31 @@ module YamWow
9
9
  @client = client
10
10
  end
11
11
 
12
- def send(topic_name)
12
+ def send(topic_name, options={})
13
13
  topic_id = get_topic_id topic_name
14
+ limit_count = (options[:limit_count] || 0).to_i
14
15
 
15
16
  oldest_message_id = nil
16
- message_data = []
17
17
  reference_data = []
18
+ messages = []
19
+ limit_reached = false
18
20
 
19
21
  loop do
20
22
  response_data = get topic_id, oldest_message_id
21
- message_data += response_data['messages']
22
23
  reference_data += response_data['references']
23
- response = MessagesByTopicResponse.new message_data, reference_data
24
- return response if response.oldest_message_id == oldest_message_id
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
+
25
37
  oldest_message_id = response.oldest_message_id
26
38
  end
27
39
  end
@@ -1,15 +1,10 @@
1
1
  module YamWow
2
2
  class MessagesByTopicResponse
3
3
 
4
- attr_reader :message_data
4
+ attr_reader :messages
5
5
 
6
- def initialize(message_data, reference_data)
7
- @topic_data = message_data
8
- @reference_data = reference_data
9
- end
10
-
11
- def messages
12
- @messages ||= root_messages.map { |m| Message.new m, @reference_data }
6
+ def initialize(messages)
7
+ @messages = messages
13
8
  end
14
9
 
15
10
  def oldest_message_id
@@ -17,11 +12,5 @@ module YamWow
17
12
  last_message.id if last_message
18
13
  end
19
14
 
20
- private
21
-
22
- def root_messages
23
- @topic_data.select { |m| m['replied_to_id'].nil? }
24
- end
25
-
26
15
  end
27
16
  end
@@ -7,9 +7,9 @@ module YamWow
7
7
  @client = client
8
8
  end
9
9
 
10
- def send
10
+ def send(options={})
11
11
  messages_by_topic_request = MessagesByTopicRequest.new @client
12
- messages_by_topic_response = messages_by_topic_request.send 'praise'
12
+ messages_by_topic_response = messages_by_topic_request.send 'praise', options
13
13
  PraiseResponse.new messages_by_topic_response.messages
14
14
  end
15
15
 
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.3
4
+ version: 0.0.4
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-26 00:00:00.000000000 Z
12
+ date: 2013-01-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: oauth
16
- requirement: &70183684306860 !ruby/object:Gem::Requirement
16
+ requirement: &70128193076360 !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: *70183684306860
24
+ version_requirements: *70128193076360
25
25
  description:
26
26
  email: matthew-github@matthewriley.name
27
27
  executables: []