webtagger 1.0.1 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,6 +19,12 @@ And it's written to support any API in the future.
19
19
  tags = WebTagger.tag_with_tagthe(text)
20
20
  #some APIs might need an api key, pass that as the second parameter
21
21
  tags = WebTagger.tag_with_yahoo(text, "YOUR-API-KEY")
22
+ #you can also "OR" results together: the first service that responds will be the overall result
23
+ tags = WebTagger.tag_with_yahoo_or_tagthe_or_alchemy text, {:yahoo=>"YOUR_YAHOO_KEY", :alchemy=>"ALCHEMY_KEY"}
24
+ #and you can of course "AND" results together: call all of the services:
25
+ tags = WebTagger.tag_with_yahoo_and_tagthe_and_alchemy text, {:yahoo=>"YOUR_YAHOO_KEY", :alchemy=>"ALCHEMY_KEY"}
26
+ #however, you can't combine them: this is EXACTLY THE SAME as above :(
27
+ tags = WebTagger.tag_with_yahoo_and_tagthe_or_alchemy text, {:yahoo=>"YOUR_YAHOO_KEY", :alchemy=>"ALCHEMY_KEY"}
22
28
 
23
29
 
24
30
  WebTagger uses caching so rest assured you won't be throttled by the API providers.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.1.1
@@ -31,9 +31,9 @@ class WebTagger
31
31
 
32
32
  r = Net::HTTP.post_form URI.parse(opts[:uri]), query
33
33
 
34
- response = if opts[:json] then JSON.parse(r.body) else r.body end
35
34
  if (100..399) === r.code.to_i
36
- @@cache[text_digest] = response
35
+ response = if opts[:json] then JSON.parse(r.body) else r.body end
36
+ @@cache[text_digest] = response
37
37
  callback.call(response)
38
38
  else
39
39
  callback.call(nil)
@@ -79,4 +79,45 @@ class WebTagger
79
79
  resp['memes'][0]['dimensions']['topic']
80
80
  end
81
81
  end
82
+
83
+ #Following good practices as stated here:
84
+ #http://technicalpickles.com/posts/using-method_missing-and-respond_to-to-create-dynamic-methods/
85
+ #Always define respond_to and method_missing together, and define missing methods when they are
86
+ #first invoked
87
+ def self.respond_to?(m_sym, include_private=false)
88
+ !!(m_sym.to_s =~ /^tag_with.*/)
89
+ end
90
+
91
+ def self.method_missing(name, *args, &block)
92
+ if name.to_s =~ /^tag_with_[A-Za-z]+\w*/
93
+ operator = nil
94
+ methods = []
95
+ name.to_s.scan /(([A-Za-z]+)_?(and|or)?)+/ do |match|
96
+ operator ||= match[2]
97
+ methods << match[1]
98
+ end
99
+
100
+ #define the method, so as to NOT default to method_missing next time, 'cause
101
+ #that's slow: the class needs to dispatch twice!
102
+ class_eval <<-RUBY
103
+ def self.#{name.to_s}(text, tokens={})
104
+ results = []
105
+ #{
106
+ methods.collect do |m|
107
+ %Q{
108
+ #{"return results.flatten! unless results.empty?" if operator == "or"}
109
+ response = send("tag_with_#{m}".to_sym, text, tokens["#{m}".to_sym])
110
+ results << response if response and !response.empty?
111
+ }
112
+ end
113
+ }
114
+ results.flatten!
115
+ end
116
+ RUBY
117
+ send name, *args
118
+ else
119
+ super
120
+ end
121
+ end
122
+
82
123
  end #of webtagger module
@@ -0,0 +1 @@
1
+ {"ResultSet":{"Result":[]}}
@@ -8,6 +8,8 @@ file_opener = lambda {|service| File.open("#{File.dirname(__FILE__)}/fixtures/#{
8
8
  FakeWeb.register_uri(:post, "http://tagthe.net/api", :body=>file_opener.call("tagthe"))
9
9
  FakeWeb.register_uri(:post, "http://access.alchemyapi.com/calls/text/TextGetRankedKeywords",
10
10
  :body=>file_opener.call("alchemy"))
11
+ FakeWeb.register_uri(:post, "http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction",
12
+ :body=>file_opener.call("yahoo"))
11
13
 
12
14
  # Requires supporting files with custom matchers and macros, etc,
13
15
  # in ./support/ and its subdirectories.
@@ -1,18 +1,43 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
+ #ATTENTION, ACTHUNG, ATENCIÓN:
4
+ #The calls to *yahoo* and *alchemy* require
5
+ #an API token in the actual service
6
+ #but, because I'm using fakeweb here, I can bypass that.
7
+ #IF YOU USE THOSE; REMEMBER TO ADD YOUR API TOKEN AS A
8
+ #SECOND PARAMETER!
3
9
  describe "WebTagger" do
4
10
  before(:each) do
5
11
  @query = "I'm a very general surgeon, surgeon"
6
12
  end
7
13
 
8
- it "should tag with tagthe" do
14
+ it "should tag with an individual service" do
9
15
  r = WebTagger.tag_with_tagthe @query
10
16
  r.should == ["surgeon"]
17
+
18
+ r = WebTagger.tag_with_alchemy @query
19
+ r.should == ["general surgeon"]
20
+
21
+ r = WebTagger.tag_with_yahoo @query
22
+ r.should == nil
11
23
  end
24
+
25
+ it "should combine results" do
26
+ r = WebTagger.tag_with_alchemy_and_tagthe @query
27
+ r.sort.should == ["general surgeon", "surgeon"]
12
28
 
13
- it "should tag with alchemy" do
14
- r = WebTagger.tag_with_alchemy @query
15
- r.should == ["general surgeon"]
29
+ r = WebTagger.tag_with_yahoo_and_alchemy_and_tagthe @query
30
+ r.sort.should == ["general surgeon", "surgeon"]
16
31
  end
17
32
 
33
+ it "should present disjoint results" do
34
+ r = WebTagger.tag_with_alchemy_or_tagthe @query
35
+ r.should == ["general surgeon"]
36
+
37
+ s = WebTagger.tag_with_tagthe_or_alchemy @query
38
+ s.should == ["surgeon"]
39
+
40
+ s = WebTagger.tag_with_yahoo_or_tagthe_or_alchemy @query
41
+ s.should == ["surgeon"]
42
+ end
18
43
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webtagger
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
- - 0
9
8
  - 1
10
- version: 1.0.1
9
+ - 1
10
+ version: 1.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - lfborjas
@@ -144,6 +144,7 @@ files:
144
144
  - lib/webtagger.rb
145
145
  - spec/fixtures/alchemy.json
146
146
  - spec/fixtures/tagthe.json
147
+ - spec/fixtures/yahoo.json
147
148
  - spec/spec_helper.rb
148
149
  - spec/support_spec.rb
149
150
  - spec/webtagger_spec.rb