yamwow 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
data/lib/yamwow/client.rb CHANGED
@@ -8,6 +8,7 @@ module YamWow
8
8
  @yam = Yam.new oauth_token, nil
9
9
  @message_throttle = Throttle.new 10, 30
10
10
  @autocomplete_throttle = Throttle.new 10, 10
11
+ @other_throttle = Throttle.new 10, 10
11
12
  end
12
13
 
13
14
  def get_messages(path=nil, params={})
@@ -19,6 +20,10 @@ module YamWow
19
20
  get 'autocomplete.json', params, @autocomplete_throttle
20
21
  end
21
22
 
23
+ def get_other(path, params={})
24
+ get path, params, @other_throttle
25
+ end
26
+
22
27
  private
23
28
 
24
29
  def get(path, params, throttle)
@@ -0,0 +1,25 @@
1
+ module Enumerable
2
+
3
+ def flatten_with_path(parent_prefix = nil)
4
+ res = {}
5
+
6
+ self.each_with_index do |elem, i|
7
+ if elem.is_a?(Array)
8
+ k, v = elem
9
+ else
10
+ k, v = i, elem
11
+ end
12
+
13
+ key = parent_prefix ? "#{parent_prefix}.#{k}" : k # assign key name for result hash
14
+
15
+ if v.is_a? Enumerable
16
+ res.merge!(v.flatten_with_path(key)) # recursive call to flatten child elements
17
+ else
18
+ res[key] = v
19
+ end
20
+ end
21
+
22
+ res
23
+ end
24
+
25
+ end
data/lib/yamwow/facade.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require_relative 'client'
2
- Dir[File.expand_path(File.dirname(__FILE__) + '/*_request.rb')].each { |f| require f }
3
- require_relative 'message_builder'
2
+ require_relative 'requests'
4
3
 
5
4
  module YamWow
6
5
  class Facade
@@ -10,21 +9,19 @@ module YamWow
10
9
  end
11
10
 
12
11
  def messages_with_topic(topic_name, options={})
13
- r = MessagesWithTopicRequest.new(@client, topic_name, options).send
14
- b = MessageBuilder.new
15
- r['messages'].map { |m| b.build_message m, r['references'] }
12
+ MessagesWithTopicRequest.new(@client, topic_name, options).send
16
13
  end
17
14
 
18
15
  def praises(options={})
19
16
  PraiseRequest.new(@client, options).send
20
17
  end
21
18
 
22
- def topic_with_name(topic_name)
23
- TopicWithNameRequest.new(@client, topic_name).send
19
+ def topic_with_name(topic_name, options={})
20
+ TopicWithNameRequest.new(@client, topic_name, options).send
24
21
  end
25
22
 
26
- def topics
27
- TopicsRequest.new(@client).send
23
+ def topics(options={})
24
+ TopicsRequest.new(@client, options).send
28
25
  end
29
26
 
30
27
  def group_with_name(group_name)
@@ -0,0 +1,29 @@
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,6 +1,6 @@
1
+ require 'time'
2
+ require_relative '../response'
1
3
  require_relative 'topic_with_name_request'
2
- require_relative 'message'
3
- require_relative 'user'
4
4
 
5
5
  module YamWow
6
6
  class MessagesWithTopicRequest
@@ -27,10 +27,7 @@ module YamWow
27
27
 
28
28
  @messages.sort! { |x, y| Time.parse(y['created_at']) <=> Time.parse(x['created_at']) }
29
29
 
30
- {
31
- 'messages' => @messages,
32
- 'references' => @references
33
- }
30
+ Response.new @messages, @references
34
31
  end
35
32
 
36
33
  private
@@ -45,7 +42,8 @@ module YamWow
45
42
 
46
43
  def get_topic_id
47
44
  request = TopicWithNameRequest.new @client, @topic_name
48
- topic = request.send
45
+ response = request.send
46
+ topic = response.data
49
47
  raise "Topic '#{@topic_name}' was not found." unless topic
50
48
  topic.id
51
49
  end
@@ -1,5 +1,4 @@
1
1
  require_relative 'messages_with_topic_request'
2
- require_relative 'praise'
3
2
 
4
3
  module YamWow
5
4
  class PraiseRequest
@@ -10,9 +9,11 @@ module YamWow
10
9
  end
11
10
 
12
11
  def send
13
- r = MessagesWithTopicRequest.new @client, 'praise', @options
14
- messages = r.send
15
- messages.map { |m| Praise.new m }
12
+ get_messages_with_praise_topic
13
+ end
14
+
15
+ def get_messages_with_praise_topic
16
+ MessagesWithTopicRequest.new(@client, 'praise', @options).send
16
17
  end
17
18
 
18
19
  end
@@ -0,0 +1,17 @@
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
@@ -0,0 +1,31 @@
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
@@ -0,0 +1,37 @@
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
@@ -0,0 +1,36 @@
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
+ detailed_topic = get_detailed_topic topic['id']
28
+ topic.merge! detailed_topic
29
+ end
30
+
31
+ def get_detailed_topic(topic_id)
32
+ TopicWithIdRequest.new(@client, topic_id).send
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1 @@
1
+ Dir[File.expand_path(File.dirname(__FILE__) + '/requests/*.rb')].each { |f| require f }
@@ -0,0 +1,35 @@
1
+ require 'csv'
2
+ require_relative 'enumerable_extensions'
3
+
4
+ module YamWow
5
+ class Response
6
+
7
+ attr_accessor :data, :reference_data
8
+
9
+ def initialize(data, reference_data=nil)
10
+ @data = data
11
+ @reference_data = reference_data
12
+ end
13
+
14
+ def to_csv(options={})
15
+ data = @data.kind_of?(Array) ? @data : [@data]
16
+ hashes = data.map { |h| h.flatten_with_path }
17
+ columns = options[:include]
18
+
19
+ unless columns
20
+ keys = Set.new
21
+ hashes.each { |h| keys += h.keys }
22
+ columns = keys.sort
23
+ end
24
+
25
+ exclude_columns = options[:exclude] || []
26
+ columns = columns.reject { |c1| exclude_columns.index { |c2| c1 =~ c2 } }
27
+
28
+ CSV.generate do |csv|
29
+ csv << columns
30
+ hashes.each { |h| csv << columns.map { |k| h[k] } }
31
+ end
32
+ end
33
+
34
+ end
35
+ end
data/lib/yamwow.rb CHANGED
@@ -1,3 +1,2 @@
1
1
  ENV['SSL_CERT_FILE'] ||= File.expand_path(File.dirname(__FILE__) + '/cacert.pem')
2
- require_relative 'yamwow/facade'
3
- require_relative 'yamwow/csv_generator'
2
+ require_relative 'yamwow/facade'
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.10
4
+ version: 0.0.11
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-06 00:00:00.000000000 Z
12
+ date: 2013-02-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: yam
16
- requirement: &70150410097100 !ruby/object:Gem::Requirement
16
+ requirement: &70345739143900 !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: *70150410097100
24
+ version_requirements: *70345739143900
25
25
  description:
26
26
  email: matthew-github@matthewriley.name
27
27
  executables: []
@@ -30,20 +30,18 @@ extra_rdoc_files: []
30
30
  files:
31
31
  - lib/cacert.pem
32
32
  - lib/yamwow/client.rb
33
- - lib/yamwow/csv_generator.rb
33
+ - lib/yamwow/enumerable_extensions.rb
34
34
  - lib/yamwow/facade.rb
35
- - lib/yamwow/group.rb
36
- - lib/yamwow/group_with_name_request.rb
37
- - lib/yamwow/message.rb
38
- - lib/yamwow/message_builder.rb
39
- - lib/yamwow/messages_with_topic_request.rb
40
- - lib/yamwow/praise.rb
41
- - lib/yamwow/praise_request.rb
35
+ - lib/yamwow/requests/group_with_name_request.rb
36
+ - lib/yamwow/requests/messages_with_topic_request.rb
37
+ - lib/yamwow/requests/praise_request.rb
38
+ - lib/yamwow/requests/topic_with_id_request.rb
39
+ - lib/yamwow/requests/topic_with_name_request.rb
40
+ - lib/yamwow/requests/topics_request.rb
41
+ - lib/yamwow/requests/topics_with_prefix_request.rb
42
+ - lib/yamwow/requests.rb
43
+ - lib/yamwow/response.rb
42
44
  - lib/yamwow/throttle.rb
43
- - lib/yamwow/topic.rb
44
- - lib/yamwow/topic_with_name_request.rb
45
- - lib/yamwow/topics_request.rb
46
- - lib/yamwow/user.rb
47
45
  - lib/yamwow.rb
48
46
  homepage: http://rubygems.org/gems/yamwow
49
47
  licenses: []
@@ -1,27 +0,0 @@
1
- require 'csv'
2
-
3
- module YamWow
4
- class CsvGenerator
5
-
6
- attr_accessor :headers
7
-
8
- def initialize
9
- @headers = nil
10
- end
11
-
12
- def generate(items)
13
- CSV.generate do |csv|
14
- csv << headers_as_array if @headers
15
- items.each { |i| csv << yield(i) }
16
- end
17
- end
18
-
19
- private
20
-
21
- def headers_as_array
22
- return unless @headers
23
- @headers.kind_of?(Array) ? @headers : @headers.split(',').map { |h| h.strip }
24
- end
25
-
26
- end
27
- end
data/lib/yamwow/group.rb DELETED
@@ -1,5 +0,0 @@
1
- module YamWow
2
- class Group
3
- attr_accessor :id, :name
4
- end
5
- end
@@ -1,40 +0,0 @@
1
- require_relative 'group'
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
- build_group group_data
13
- end
14
-
15
- private
16
-
17
- def group_data
18
- data = send_autocomplete_request
19
- groups = data['groups'] || []
20
- groups.select { |g| g['name'] == @group_name }.first
21
- end
22
-
23
- def send_autocomplete_request
24
- @client.get_autocomplete prefix: autocomplete_prefix
25
- end
26
-
27
- def autocomplete_prefix
28
- 'to:' + @group_name
29
- end
30
-
31
- def build_group(group_data)
32
- return unless group_data
33
- g = YamWow::Group.new
34
- g.id = group_data['id']
35
- g.name = group_data['full_name']
36
- g
37
- end
38
-
39
- end
40
- end
@@ -1,35 +0,0 @@
1
- require 'time'
2
-
3
- module YamWow
4
- class Message
5
-
6
- attr_accessor :id, :plain_body, :parsed_body, :date_created, :like_count, :url, :sender, :reply_to_id, :attachments
7
-
8
- def sender_tag
9
- "[[user:#{@sender.id}]]"
10
- end
11
-
12
- def sender_name
13
- @sender.name
14
- end
15
-
16
- def plain_body_single_line
17
- single_line @plain_body
18
- end
19
-
20
- def parsed_body_single_line
21
- single_line @parsed_body
22
- end
23
-
24
- def thread_root?
25
- @reply_to_id.nil?
26
- end
27
-
28
- private
29
-
30
- def single_line(text)
31
- text.gsub("\n", ' ').strip
32
- end
33
-
34
- end
35
- end
@@ -1,30 +0,0 @@
1
- module YamWow
2
- class MessageBuilder
3
-
4
- def build_message(message_data, reference_data)
5
- d = message_data
6
- m = Message.new
7
- m.id = d['id']
8
- m.plain_body = d['body']['plain']
9
- m.parsed_body = d['body']['parsed']
10
- m.date_created = Time.parse d['created_at']
11
- m.like_count = d['liked_by']['count'].to_i
12
- m.url = d['web_url']
13
- m.sender = build_user d['sender_id'], reference_data
14
- m.reply_to_id = d['replied_to_id']
15
- m.attachments = d['attachments']
16
- m
17
- end
18
-
19
- private
20
-
21
- def build_user(user_id, reference_data)
22
- d = reference_data.select { |r| r['id'] == user_id }[0]
23
- u = User.new
24
- u.id = d['id']
25
- u.name = d['full_name']
26
- u
27
- end
28
-
29
- end
30
- end
data/lib/yamwow/praise.rb DELETED
@@ -1,57 +0,0 @@
1
- require_relative 'message'
2
-
3
- module YamWow
4
- class Praise
5
-
6
- def initialize(message)
7
- @message = message
8
- end
9
-
10
- def sender_tag
11
- @message.sender_tag
12
- end
13
-
14
- def sender_name
15
- @message.sender_name
16
- end
17
-
18
- def date_created
19
- @message.date_created
20
- end
21
-
22
- def like_count
23
- @message.like_count
24
- end
25
-
26
- def url
27
- @message.url
28
- end
29
-
30
- def recipient_tags
31
- split_recipients @message.parsed_body
32
- end
33
-
34
- def recipient_names
35
- split_recipients(@message.plain_body).map { |r| r.delete('@') }
36
- end
37
-
38
- def reason
39
- attachments = @message.attachments || []
40
- reason = attachments.map { |a| m = a['name'].match(/Praise: (.+)/m); m ? m[1] : nil }[0]
41
- reason || @message.parsed_body
42
- end
43
-
44
- def reason_single_line
45
- reason.gsub("\n", ' ').strip
46
- end
47
-
48
- private
49
-
50
- def split_recipients(recipients)
51
- recipients = recipients.include?('praised') ? recipients.match(/praised (.+)/)[1] : ''
52
- recipients.gsub!(' and ', ', ')
53
- recipients.split(', ')
54
- end
55
-
56
- end
57
- end
data/lib/yamwow/topic.rb DELETED
@@ -1,5 +0,0 @@
1
- module YamWow
2
- class Topic
3
- attr_accessor :id, :name, :usages
4
- end
5
- end
@@ -1,36 +0,0 @@
1
- require_relative 'topic'
2
-
3
- module YamWow
4
- class TopicWithNameRequest
5
-
6
- def initialize(client, topic_name)
7
- @client = client
8
- @topic_name = topic_name
9
- end
10
-
11
- def send
12
- build_topic topic_data
13
- end
14
-
15
- private
16
-
17
- def topic_data
18
- data = send_autocomplete_request
19
- topics = data['topics'] || []
20
- topics.select { |t| t['name'].casecmp(@topic_name) == 0 }.first
21
- end
22
-
23
- def send_autocomplete_request
24
- @client.get_autocomplete prefix: @topic_name
25
- end
26
-
27
- def build_topic(topic_data)
28
- return unless topic_data
29
- t = Topic.new
30
- t.id = topic_data['id']
31
- t.name = topic_data['name']
32
- t
33
- end
34
-
35
- end
36
- end
@@ -1,44 +0,0 @@
1
- require_relative 'topic'
2
-
3
- module YamWow
4
- class TopicsRequest
5
-
6
- def initialize(client)
7
- @client = client
8
- end
9
-
10
- def send
11
- @topics = []
12
- process_range '0', '9'
13
- process_range 'a', 'z'
14
- @topics.uniq! { |t| t.id }
15
- @topics.sort! { |x, y| x.name.downcase <=> y.name.downcase }
16
- @topics
17
- end
18
-
19
- private
20
-
21
- def process_range(first, last, current=nil)
22
- ((current.to_s + first)..(current.to_s + last)).each do |prefix|
23
- response_data = send_autocomplete_request prefix
24
- topics_data = response_data['topics'] || []
25
- topics = topics_data.map { |t| build_topic t }
26
- @topics += topics
27
- process_range first, last, prefix if topics.length == 50
28
- end
29
- end
30
-
31
- def send_autocomplete_request(prefix)
32
- @client.get_autocomplete prefix: prefix.downcase
33
- end
34
-
35
- def build_topic(topic_data)
36
- t = Topic.new
37
- t.id = topic_data['id']
38
- t.name = topic_data['name']
39
- t.usages = topic_data['applications_count']
40
- t
41
- end
42
-
43
- end
44
- end
data/lib/yamwow/user.rb DELETED
@@ -1,5 +0,0 @@
1
- module YamWow
2
- class User
3
- attr_accessor :id, :name
4
- end
5
- end