zemanta_client 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Zemanta
2
2
 
3
- This is a ruby client to awesome Zemanta app.
3
+ This is a ruby client to awesome Zemanta app.
4
4
 
5
5
  ## Installation
6
6
 
@@ -27,33 +27,34 @@ You need to set api_key to use the client. If you won't do that, gem will raise
27
27
  - You can set your api_key in configuration block like this:
28
28
 
29
29
  ```ruby
30
- Zemanta.configure { |c| c.api_key = 'your_api_key' }
30
+ Zemanta.configure { |c| c.api_key = 'your_api_key' }
31
31
  ```
32
32
 
33
33
 
34
34
  ## Usage
35
35
 
36
- Two use cases are implemented:
37
-
36
+ Two use cases are implemented:
37
+
38
38
  1. Retrieving suggest_markup data and enhancing the text.
39
39
 
40
40
  ```ruby
41
41
  Zemanta::Markup.fetch(text, opts = {})
42
42
  ```
43
-
43
+
44
44
  It will fetch suggest_markup data for passed text from Zemanta api and wrap it in custom classes.
45
45
 
46
-
46
+
47
47
  2. Enhancing the text with links:
48
-
48
+
49
49
  ```ruby
50
50
  Zemanta::Enhancer.new(text, opts = {}).enhance
51
51
  ```
52
52
 
53
53
  Options:
54
- no_duplicates (default: false) - ensures links are used once
55
-
56
-
54
+ * `no_duplicates` (default: false) - ensures links are used once
55
+ * `skip` (default: nil) - URL regexp of the links that should be skipped
56
+ * `strip_query_string` - removes the query string part of the suggested links urls
57
+
57
58
  It will fetch suggest_markup data for given text and for every returned link it will wrap the keyword with this link.
58
59
  Then it will return the updated text.
59
60
 
@@ -95,6 +96,7 @@ end
95
96
 
96
97
  ## Changelog
97
98
 
99
+ 0.0.6 - Added #skip and #strip_query_string options to enhancer
98
100
  0.0.5 - Added #no_duplicates option to Enhancer
99
101
  0.0.4 - Fixed yajl require in gemspec
100
102
  0.0.3 - Added Enhancer class, used yajl for json parsing, changed external gem api.
@@ -3,6 +3,8 @@ module Zemanta
3
3
  # Options:
4
4
  #
5
5
  # no_duplicates (default: false) - ensures links are used once
6
+ # skip - regexp for URLs not to be hot-linked
7
+ # strip_query_string - remove the query string from urls
6
8
  #
7
9
  def initialize(text, opts = {})
8
10
  @text = text
@@ -18,7 +20,15 @@ module Zemanta
18
20
 
19
21
  def enhance!
20
22
  words_to_anchor(@opts).each do |dictionary|
21
- link = "<a href=#{dictionary[:link]}>#{dictionary[:word]}</a>"
23
+ url = dictionary[:link]
24
+
25
+ if @opts[:skip]
26
+ next if url =~ @opts[:skip]
27
+ end
28
+
29
+ url = strip_query_string(url) if @opts[:strip_query_string]
30
+
31
+ link = "<a href=#{url}>#{dictionary[:word]}</a>"
22
32
  if @opts[:no_duplicates]
23
33
  @text.sub!(dictionary[:word], link)
24
34
  else
@@ -36,5 +46,10 @@ module Zemanta
36
46
  def suggest_markup(opts = {})
37
47
  Markup.fetch(@text, opts)
38
48
  end
49
+
50
+ def strip_query_string(url)
51
+ return nil unless url
52
+ url.gsub(/\?.*$/, '')
53
+ end
39
54
  end
40
55
  end
@@ -1,3 +1,3 @@
1
1
  module Zemanta
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -22,5 +22,29 @@ describe Zemanta::Enhancer do
22
22
  output.scan('<a').should have(1).elements
23
23
  end
24
24
  end
25
+
26
+ describe "skip option" do
27
+ it "skips links whose URL matches a user-provided regexp" do
28
+ stub_zemanta_enhancer!
29
+ output = Zemanta::Enhancer.new(input, skip: /machu_picchu/).enhance
30
+ output.scan('<a').should have(1).elements
31
+ end
32
+
33
+ it "doesn't affect the links that don't match thr user-provided regexp" do
34
+ stub_zemanta_enhancer!
35
+ output = Zemanta::Enhancer.new(input, skip: /macchu_picchu/).enhance
36
+ output.scan('<a').should have(2).elements
37
+ end
38
+ end
39
+
40
+ describe "strip_query_string" do
41
+ it "strips the query string from the suggested urls" do
42
+ text = "Hello foo bar"
43
+ suggestions = [{ word: 'foo', link: "http://bar.com/foo?bar=123" }]
44
+ Zemanta::Enhancer.any_instance.stub(words_to_anchor: suggestions)
45
+ output = Zemanta::Enhancer.new(text, strip_query_string: true).enhance
46
+ output.should == "Hello <a href=http://bar.com/foo>foo</a> bar"
47
+ end
48
+ end
25
49
  end
26
50
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zemanta_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-24 00:00:00.000000000 Z
12
+ date: 2013-07-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: typhoeus