zemanta_client 0.0.2 → 0.0.3

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. At this point it only supports suggest_markup method and is tested on ruby 1.9.3.
3
+ This is a ruby client to awesome Zemanta app.
4
4
 
5
5
  ## Installation
6
6
 
@@ -16,7 +16,7 @@ Or install it yourself as:
16
16
 
17
17
  $ gem install zemanta_client
18
18
 
19
- ## Usage
19
+ ## Api key
20
20
 
21
21
  You need to set api_key to use the client. If you won't do that, gem will raise an error. There are 2 ways:
22
22
  1. Environment variable
@@ -26,11 +26,19 @@ You need to set api_key to use the client. If you won't do that, gem will raise
26
26
  - You can set your api_key in configuration block like this:
27
27
  - Zemanta.configure { |c| c.api_key = 'your_api_key' }
28
28
 
29
- After your api_key has been set, all you need to do is:
30
29
 
31
- Zemanta.new("Text you want to send to Zemanta").suggest_markup
30
+ ## Usage
31
+
32
+ Two use cases are implemented: Retrieving suggest_markup data and enhancing the text.
33
+
34
+ 1. Zemanta::Markup.fetch(text, opts = {})
35
+ It will fetch suggest_markup data for passed text from Zemanta api and wrap it in custom classes.
36
+
37
+ 2. Zemanta::Enhancer.new(text, opts = {}).enhance
38
+ It will fetch suggest_markup data for given text and for every returned link it will wrap the keyword with this link.
39
+ Than it will return updated text.
32
40
 
33
- And you will be returned Markup object or error will be raised if Zemanta returns one.
41
+ In both relevance and confidence keys can be passes inside opts to filter any links that are below passed values.
34
42
 
35
43
  ## Configuration
36
44
 
@@ -65,5 +73,6 @@ And you will be returned Markup object or error will be raised if Zemanta return
65
73
 
66
74
  ## Changelog
67
75
 
76
+ 0.0.3 - Added Enhancer class, used yajl for json parsing, changed external gem api.
68
77
  0.0.2 - Got rid of autoloads.
69
78
  0.0.1 - Initial release. Support for suggest_markup method and caching.
@@ -1,4 +1,4 @@
1
- class Zemanta
1
+ module Zemanta
2
2
  module Cache
3
3
  class Disk
4
4
  attr_accessor :db
@@ -1,4 +1,4 @@
1
- class Zemanta
1
+ module Zemanta
2
2
  class Configuration
3
3
  class NullStorage
4
4
  def [](key)
@@ -1,4 +1,4 @@
1
- class Zemanta
1
+ module Zemanta
2
2
  class Configuration
3
3
  attr_accessor :custom_request_opts, :format, :api_key, :cache_storage
4
4
 
@@ -0,0 +1,32 @@
1
+ module Zemanta
2
+ class Enhancer
3
+ def initialize(text, opts = {})
4
+ @text = text
5
+ @opts = opts
6
+ end
7
+
8
+ def enhance
9
+ enhance!
10
+ @text
11
+ end
12
+
13
+ private
14
+
15
+ def enhance!
16
+ words_to_anchor(@opts).each do |dictionary|
17
+ link = "<a href=#{dictionary[:link]}>#{dictionary[:word]}</a>"
18
+ @text.gsub!(dictionary[:word], link)
19
+ end
20
+ end
21
+
22
+ def words_to_anchor(opts)
23
+ suggest_markup(opts).links.map do |link|
24
+ { word: link.anchor, link: link.target.first.url }
25
+ end
26
+ end
27
+
28
+ def suggest_markup(opts = {})
29
+ Markup.fetch(@text, opts)
30
+ end
31
+ end
32
+ end
@@ -1,4 +1,4 @@
1
- class Zemanta
1
+ module Zemanta
2
2
  class Fetcher
3
3
  class Cache
4
4
  class Key
@@ -1,4 +1,4 @@
1
- class Zemanta
1
+ module Zemanta
2
2
  class Fetcher
3
3
  class Cache
4
4
  class NullResponse
@@ -1,4 +1,4 @@
1
- class Zemanta
1
+ module Zemanta
2
2
  class Fetcher
3
3
  class Cache
4
4
  class Response
@@ -1,4 +1,4 @@
1
- class Zemanta
1
+ module Zemanta
2
2
  class Fetcher
3
3
  class Cache
4
4
  def initialize(opts = {})
@@ -0,0 +1,13 @@
1
+ module Zemanta
2
+ class Fetcher
3
+ class Parser
4
+ def initialize(json)
5
+ @json = json
6
+ end
7
+
8
+ def parse
9
+ Yajl::Parser.parse(@json)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,4 +1,4 @@
1
- class Zemanta
1
+ module Zemanta
2
2
  class Fetcher
3
3
  class Web
4
4
  def initialize(opts = {})
@@ -1,4 +1,4 @@
1
- class Zemanta
1
+ module Zemanta
2
2
  class Fetcher
3
3
  def initialize(opts = {})
4
4
  @opts = opts
@@ -21,7 +21,7 @@ class Zemanta
21
21
  end
22
22
 
23
23
  def parse(data)
24
- JSON.parse(data)
24
+ Parser.new(data).parse
25
25
  end
26
26
 
27
27
  def request_opts
@@ -1,4 +1,4 @@
1
- class Zemanta
1
+ module Zemanta
2
2
  class Markup
3
3
  class Link
4
4
  class Target
@@ -1,4 +1,4 @@
1
- class Zemanta
1
+ module Zemanta
2
2
  class Markup
3
3
  class Link
4
4
  attr_reader :relevance, :confidence, :entity_type, :target, :anchor
@@ -1,4 +1,4 @@
1
- class Zemanta
1
+ module Zemanta
2
2
  class Markup
3
3
  attr_reader :text, :links
4
4
 
@@ -6,5 +6,31 @@ class Zemanta
6
6
  @text = opts["text"]
7
7
  @links = opts["links"].map{ |link| Link.new(link) }
8
8
  end
9
+
10
+ def self.fetch(text, opts = {})
11
+ relevance = opts.delete(:relevance) || 0
12
+ confidence = opts.delete(:confidence) || 0
13
+
14
+ new(suggest_markup_request(text, opts)).tap do |markup|
15
+ markup.drop_links_below(relevance, confidence)
16
+ end
17
+ end
18
+
19
+ def drop_links_below(relevance, confidence)
20
+ @links.keep_if do |link|
21
+ link.above?(relevance, confidence)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def self.suggest_markup_request(text, opts)
28
+ return {'links' => []} if text.to_s.empty?
29
+ request({ text: text, method: "zemanta.suggest_markup" }.merge(opts))["markup"]
30
+ end
31
+
32
+ def self.request(opts)
33
+ Fetcher.new(opts).post
34
+ end
9
35
  end
10
36
  end
@@ -1,3 +1,3 @@
1
- class Zemanta
2
- VERSION = "0.0.2"
1
+ module Zemanta
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/zemanta.rb CHANGED
@@ -1,30 +1,23 @@
1
1
  require 'typhoeus'
2
- require 'json'
2
+ require 'yajl'
3
3
  require 'digest/md5'
4
4
 
5
- require 'zemanta/cache'
6
5
  require 'zemanta/cache/disk'
7
6
  require 'zemanta/configuration'
8
7
  require 'zemanta/configuration/null_storage'
9
8
  require 'zemanta/fetcher'
9
+ require 'zemanta/fetcher/parser'
10
10
  require 'zemanta/fetcher/web'
11
11
  require 'zemanta/fetcher/cache'
12
12
  require 'zemanta/fetcher/cache/null_response'
13
13
  require 'zemanta/fetcher/cache/response'
14
14
  require 'zemanta/fetcher/cache/key'
15
15
  require 'zemanta/markup'
16
- require "zemanta/markup/link"
16
+ require 'zemanta/markup/link'
17
17
  require 'zemanta/markup/link/target'
18
+ require 'zemanta/enhancer'
18
19
 
19
- class Zemanta
20
- def initialize(text)
21
- @text = text
22
- end
23
-
24
- def suggest_markup(opts = {})
25
- Markup.new(suggest_markup_request(opts))
26
- end
27
-
20
+ module Zemanta
28
21
  def self.configure
29
22
  yield config
30
23
  end
@@ -32,14 +25,4 @@ class Zemanta
32
25
  def self.config
33
26
  @configuration ||= Configuration.new
34
27
  end
35
-
36
- private
37
-
38
- def suggest_markup_request(opts)
39
- @response ||= request({ text: @text, method: "zemanta.suggest_markup" }.merge(opts))['markup']
40
- end
41
-
42
- def request(opts)
43
- Fetcher.new(opts).post
44
- end
45
- end
28
+ end
@@ -0,0 +1,37 @@
1
+ {
2
+ "markup":{
3
+ "text":"",
4
+ "links":[
5
+ {
6
+ "relevance":0.91956,
7
+ "confidence":0.846373,
8
+ "entity_type":[
9
+
10
+ ],
11
+ "target":[
12
+ {
13
+ "url":"http://www.some.url/leading_to/inca_trail",
14
+ "type":"geolocation",
15
+ "title":"The Inca Trail"
16
+ }
17
+ ],
18
+ "anchor":"Inca trail to Machu Picchu"
19
+ },
20
+ {
21
+ "relevance":0.881943,
22
+ "confidence":0.878267,
23
+ "entity_type":[
24
+ "/location/location"
25
+ ],
26
+ "target":[
27
+ {
28
+ "url":"http://www.some.url/about/machu_picchu",
29
+ "type":"geolocation",
30
+ "title":"Machu Picchu"
31
+ }
32
+ ],
33
+ "anchor":"Machu Pichu"
34
+ }
35
+ ]
36
+ }
37
+ }
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'zemanta'
2
2
  require 'webmock/rspec'
3
+ require 'json'
3
4
 
4
5
  Zemanta.configure do |c|
5
6
  c.api_key = 'fake_key'
@@ -41,3 +42,8 @@ def stub_zemanta_exception!
41
42
  stub_request(:post, /api.zemanta.com/).
42
43
  to_return(:status => 200, :body => '<h1>403 Developer Inactive</h1>')
43
44
  end
45
+
46
+ def stub_zemanta_enhancer!
47
+ stub_request(:post, /api.zemanta.com/).
48
+ to_return(:status => 200, :body => fixture("enhancer").to_json)
49
+ end
@@ -37,7 +37,7 @@ describe Zemanta::Cache::Disk do
37
37
  let(:value) { 'text-value' }
38
38
 
39
39
  it "creates new file for specified key" do
40
- Pathname.new(storage.db.join(key)).delete
40
+ Pathname.new(storage.db.join(key)).delete if storage.db.join(key).exist?
41
41
  Pathname.new(storage.db.join(key)).should_not exist
42
42
 
43
43
  storage[key] = value
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Zemanta::Enhancer do
4
+ context "enhance" do
5
+ let(:input) { "Here be dragons: Inca trail to Machu Picchu, Machu Pichu" }
6
+
7
+ it "returns string" do
8
+ stub_zemanta_enhancer!
9
+ Zemanta::Enhancer.new(input).enhance.should be_a String
10
+ end
11
+
12
+ it "converts keywords to links" do
13
+ stub_zemanta_enhancer!
14
+ Zemanta::Enhancer.new(input).enhance.scan('<a').should have(2).elements
15
+ end
16
+ end
17
+ end
@@ -5,7 +5,7 @@ describe "suggest_markup" do
5
5
  stub_zemanta_full!
6
6
  end
7
7
 
8
- let(:markup) { Zemanta.new("some text").suggest_markup }
8
+ let(:markup) { Zemanta::Markup.fetch("some text") }
9
9
 
10
10
  it "responds to #text" do
11
11
  markup.text.should == "text"
@@ -2,7 +2,7 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  describe "incorrect api key" do
5
- let(:markup) { Zemanta.new("some text").suggest_markup }
5
+ let(:markup) { Zemanta::Markup.fetch("some text") }
6
6
 
7
7
  it "raises exception when 403 error returned" do
8
8
  stub_zemanta_exception!
@@ -12,5 +12,61 @@ describe Zemanta::Markup do
12
12
  subject.links.should be_a Array
13
13
  subject.links.first.should be_a Zemanta::Markup::Link
14
14
  end
15
+
16
+ context ".fetch" do
17
+ before(:each) { stub_zemanta_enhancer! }
18
+
19
+ it "returns Markup object for nonempty string" do
20
+ Zemanta::Markup.fetch("This is a text").should be_a Zemanta::Markup
21
+ end
22
+
23
+ it "returns Markup object for nil" do
24
+ Zemanta::Markup.fetch(nil).should be_a Zemanta::Markup
25
+ end
26
+
27
+ it "does not hit fetcher if nil passed" do
28
+ Zemanta::Fetcher.any_instance.should_not_receive(:post)
29
+ Zemanta::Markup.fetch(nil)
30
+ end
31
+
32
+ it "returns Markup object for empty string" do
33
+ stub_zemanta_full!
34
+ Zemanta::Markup.fetch("").should be_a Zemanta::Markup
35
+ end
36
+
37
+ it "does not hit fetcher if empty string passed" do
38
+ Zemanta::Fetcher.any_instance.should_not_receive(:post)
39
+ Zemanta::Markup.fetch("")
40
+ end
41
+
42
+ it "calls drop_links_below on created markup" do
43
+ Zemanta::Markup.any_instance.should_receive(:drop_links_below)
44
+ Zemanta::Markup.fetch("This is a text")
45
+ end
46
+
47
+ it "returns every link if no relevance or confidence passed" do
48
+ Zemanta::Markup.fetch("This is a text").links.count.should == 2
49
+ end
50
+
51
+ end
52
+
53
+ context "drop_links_below" do
54
+ subject { Zemanta::Markup.new(fixture("enhancer")['markup'])}
55
+
56
+ it "drops links below given relevance" do
57
+ subject.drop_links_below(0.9, 0)
58
+ subject.links.map(&:relevance).should == [0.91956]
59
+ end
60
+
61
+ it "drops links below given confidence" do
62
+ subject.drop_links_below(0, 0.85)
63
+ subject.links.map(&:confidence).should == [0.878267]
64
+ end
65
+
66
+ it "drops links below both given condifence and relevance" do
67
+ subject.drop_links_below(0.9, 0.9)
68
+ subject.links.empty?.should == true
69
+ end
70
+ end
15
71
  end
16
72
  end
data/spec/zemanta_spec.rb CHANGED
@@ -1,12 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Zemanta do
4
- it "makes request with text and method params on #suggest_markup" do
5
- context = Zemanta.new("This is a text")
6
- context.stub(:suggest_markup_request) { fixture("markup") }
7
- context.suggest_markup.should be_a Zemanta::Markup
8
- end
9
-
10
4
  it "returns Configuration instance on Zemanta.config" do
11
5
  Zemanta.config.should be_a Zemanta::Configuration
12
6
  end
@@ -16,4 +10,4 @@ describe Zemanta do
16
10
  config.should be_a Zemanta::Configuration
17
11
  end
18
12
  end
19
- end
13
+ end
data/zemanta.gemspec CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
  gem.add_dependency "typhoeus"
21
+ gem.add_dependency "yajl"
21
22
 
22
23
  gem.add_development_dependency "rspec"
23
24
  gem.add_development_dependency "webmock"
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.2
4
+ version: 0.0.3
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-04-09 00:00:00.000000000 Z
12
+ date: 2013-06-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: typhoeus
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: yajl
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: rspec
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -72,20 +88,22 @@ files:
72
88
  - README.md
73
89
  - Rakefile
74
90
  - lib/zemanta.rb
75
- - lib/zemanta/cache.rb
76
91
  - lib/zemanta/cache/disk.rb
77
92
  - lib/zemanta/configuration.rb
78
93
  - lib/zemanta/configuration/null_storage.rb
94
+ - lib/zemanta/enhancer.rb
79
95
  - lib/zemanta/fetcher.rb
80
96
  - lib/zemanta/fetcher/cache.rb
81
97
  - lib/zemanta/fetcher/cache/key.rb
82
98
  - lib/zemanta/fetcher/cache/null_response.rb
83
99
  - lib/zemanta/fetcher/cache/response.rb
100
+ - lib/zemanta/fetcher/parser.rb
84
101
  - lib/zemanta/fetcher/web.rb
85
102
  - lib/zemanta/markup.rb
86
103
  - lib/zemanta/markup/link.rb
87
104
  - lib/zemanta/markup/link/target.rb
88
105
  - lib/zemanta/version.rb
106
+ - spec/fixtures/zemanta/enhancer.json
89
107
  - spec/fixtures/zemanta/full_response.json
90
108
  - spec/fixtures/zemanta/link.json
91
109
  - spec/fixtures/zemanta/markup.json
@@ -94,6 +112,7 @@ files:
94
112
  - spec/zemanta/cache/disk_spec.rb
95
113
  - spec/zemanta/configuration/null_storage_spec.rb
96
114
  - spec/zemanta/configuration_spec.rb
115
+ - spec/zemanta/enhancer_spec.rb
97
116
  - spec/zemanta/fetcher/cache/key_spec.rb
98
117
  - spec/zemanta/fetcher/cache/null_response_spec.rb
99
118
  - spec/zemanta/fetcher/cache/response_spec.rb
@@ -132,6 +151,7 @@ signing_key:
132
151
  specification_version: 3
133
152
  summary: This is a ruby client for Zemanta api.
134
153
  test_files:
154
+ - spec/fixtures/zemanta/enhancer.json
135
155
  - spec/fixtures/zemanta/full_response.json
136
156
  - spec/fixtures/zemanta/link.json
137
157
  - spec/fixtures/zemanta/markup.json
@@ -140,6 +160,7 @@ test_files:
140
160
  - spec/zemanta/cache/disk_spec.rb
141
161
  - spec/zemanta/configuration/null_storage_spec.rb
142
162
  - spec/zemanta/configuration_spec.rb
163
+ - spec/zemanta/enhancer_spec.rb
143
164
  - spec/zemanta/fetcher/cache/key_spec.rb
144
165
  - spec/zemanta/fetcher/cache/null_response_spec.rb
145
166
  - spec/zemanta/fetcher/cache/response_spec.rb
data/lib/zemanta/cache.rb DELETED
@@ -1,4 +0,0 @@
1
- class Zemanta
2
- module Cache
3
- end
4
- end