diffbot 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  This is a ruby client for the [Diffbot](http://diffbot.com) API.
4
4
 
5
+ ## Install
6
+
7
+ Get the latest version from RubyGems:
8
+
9
+ $ gem install diffbot
10
+
5
11
  ## Global Options
6
12
 
7
13
  You can pass some settings to Diffbot like this:
@@ -30,7 +36,7 @@ The list of supported settings is:
30
36
  In order to fetch an article, do this:
31
37
 
32
38
  ``` ruby
33
- require "diffbot/article"
39
+ require "diffbot"
34
40
 
35
41
  article = Diffbot::Article.fetch(article_url, diffbot_token)
36
42
 
@@ -74,7 +80,7 @@ end
74
80
  In order to fetch and analyze a front page, do this:
75
81
 
76
82
  ``` ruby
77
- require "diffbot/frontpage"
83
+ require "diffbot"
78
84
 
79
85
  frontpage = Diffbot::Frontpage.fetch(url, diffbot_token)
80
86
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "diffbot"
3
- s.version = "0.1.0"
3
+ s.version = "0.1.1"
4
4
  s.description = "Diffbot provides a concise API for analyzing and extracting semantic information from web pages using Diffbot (http://www.diffbot.com)."
5
5
  s.summary = "Ruby interface to the Diffbot API "
6
6
  s.authors = ["Nicolas Sanguinetti"]
@@ -34,6 +34,15 @@ module Diffbot
34
34
  @article_defaults
35
35
  end
36
36
 
37
+ # Public: Reset the configuration to the defaults. Useful for testing.
38
+ #
39
+ # Returns nil.
40
+ def self.reset!
41
+ @article_defaults = nil
42
+ @token = nil
43
+ @instrumentor = nil
44
+ end
45
+
37
46
  class << self
38
47
  # Public: Your Diffbot API token.
39
48
  attr_accessor :token
@@ -1,6 +1,4 @@
1
1
  require "yajl"
2
- require "diffbot"
3
- require "diffbot/coercible_hash"
4
2
 
5
3
  module Diffbot
6
4
  # Representation of an article (ie a blog post or similar). This class offers
@@ -1,5 +1,4 @@
1
1
  require "nokogiri"
2
- require "diffbot"
3
2
  require "diffbot/item"
4
3
 
5
4
  module Diffbot
@@ -31,10 +31,10 @@ class Diffbot::Frontpage::DmlParser
31
31
  attrs = {}
32
32
 
33
33
  info = dml % "info"
34
- attrs["title"] = (info % "title").text
35
- attrs["icon"] = (info % "icon").text
36
- attrs["sourceType"] = (info % "sourceType").text
37
- attrs["sourceURL"] = (info % "sourceURL").text
34
+ attrs["title"] = extract_text(info, "title")
35
+ attrs["icon"] = extract_text(info, "icon")
36
+ attrs["sourceType"] = extract_text(info, "sourceType")
37
+ attrs["sourceURL"] = extract_text(info, "sourceURL")
38
38
 
39
39
  items = dml / "item"
40
40
  attrs["items"] = items.map do |item|
@@ -44,9 +44,18 @@ class Diffbot::Frontpage::DmlParser
44
44
  attrs
45
45
  end
46
46
 
47
+ def extract_text(node, selector)
48
+ tag = node % selector
49
+ tag && tag.text
50
+ end
51
+
47
52
  # Parser that takes the XML from a particular item from the XML returned from
48
53
  # the frontpage API.
49
54
  class ItemParser
55
+ def self.parse(node)
56
+ node = Nokogiri(node)
57
+ end
58
+
50
59
  # The root element of each item.
51
60
  attr_reader :item
52
61
 
@@ -2,14 +2,25 @@ require "excon"
2
2
 
3
3
  module Diffbot
4
4
  class Request
5
+ class << self
6
+ # Public: Whether we are in test mode or not (so we stub out HTTP calls).
7
+ # Defaults to false.
8
+ attr_accessor :testing
9
+ end
10
+
11
+ self.testing = false
12
+
5
13
  # The API token for Diffbot.
6
14
  attr_reader :token
7
15
 
8
16
  # Public: Initialize a new request to the API.
9
17
  #
10
- # token - The API token for Diffbot.
11
- def initialize(token)
18
+ # token - The API token for Diffbot.
19
+ # test_mode - Whether requests are in test mode or not. This is passed to
20
+ # Excon so we can mock connections.
21
+ def initialize(token, test_mode=self.class.testing)
12
22
  @token = token
23
+ @test_mode = test_mode
13
24
  end
14
25
 
15
26
  # Public: Perform an HTTP request against Diffbot's API.
@@ -26,8 +37,7 @@ module Diffbot
26
37
  request_options = build_request(method, query)
27
38
  yield request_options if block_given?
28
39
 
29
- request = Excon.new(endpoint)
30
-
40
+ request = Excon.new(endpoint)
31
41
  request.request(request_options)
32
42
  end
33
43
 
@@ -39,7 +49,7 @@ module Diffbot
39
49
  # Returns a Hash.
40
50
  def build_request(method, query_params={})
41
51
  query = { token: token }.merge(query_params)
42
- request = { query: query, method: method, headers: {} }
52
+ request = { query: query, method: method, headers: {}, mock: @test_mode }
43
53
 
44
54
  if Diffbot.instrumentor
45
55
  request.update(
@@ -0,0 +1,45 @@
1
+ require "test_helper"
2
+ require "uri"
3
+
4
+ describe Diffbot::Article do
5
+ describe ".fetch" do
6
+ before do
7
+ path = URI.parse(Diffbot::Article.endpoint).path
8
+
9
+ Excon.stub(method: :get, path: path) do |params|
10
+ body = { title: "The Title" }
11
+ body[:tags] = %w(tag1 tag2) if params[:query]["tags"]
12
+ body = Yajl::Encoder.encode(body)
13
+
14
+ { body: body, status: 200 }
15
+ end
16
+ end
17
+
18
+ it "returns a new Article" do
19
+ article = Diffbot::Article.fetch("http://example.org")
20
+ article.must_be_instance_of(Diffbot::Article)
21
+ end
22
+
23
+ it "sets the parameters as received from the API" do
24
+ article = Diffbot::Article.fetch("http://example.org")
25
+ article.title.must_equal("The Title")
26
+ end
27
+
28
+ it "can be configured by passing a block" do
29
+ article = Diffbot::Article.fetch("http://example.org") do |req|
30
+ req.tags = true
31
+ req.must_be_instance_of(Diffbot::Article::RequestParams)
32
+ end
33
+ article.tags.must_equal(["tag1", "tag2"])
34
+ end
35
+
36
+ it "will take the default configuration" do
37
+ Diffbot.article_defaults do |req|
38
+ req.tags = true
39
+ end
40
+
41
+ article = Diffbot::Article.fetch("http://example.org")
42
+ article.tags.wont_be_nil
43
+ end
44
+ end
45
+ end
@@ -1,5 +1,4 @@
1
1
  require "test_helper"
2
- require "diffbot/coercible_hash"
3
2
 
4
3
  describe Diffbot::CoercibleHash do
5
4
  module Foo
@@ -0,0 +1,69 @@
1
+ require "test_helper"
2
+
3
+ describe Diffbot::Request do
4
+ subject do
5
+ Diffbot::Request.new("DUMMY_TOKEN")
6
+ end
7
+
8
+ describe "#build_request" do
9
+ after do
10
+ Diffbot.instrumentor = nil
11
+ end
12
+
13
+ it "includes the method" do
14
+ req = subject.build_request(:get)
15
+ req[:method].must_equal(:get)
16
+ end
17
+
18
+ it "incldues the token in the query string" do
19
+ req = subject.build_request(:get)
20
+ req[:query][:token].must_equal("DUMMY_TOKEN")
21
+ end
22
+
23
+ it "includes passed-in query parameters in the query string" do
24
+ req = subject.build_request(:get, a: 1, b: 2)
25
+
26
+ req[:query].must_include(:a, :b)
27
+ req[:query][:a].must_equal(1)
28
+ req[:query][:b].must_equal(2)
29
+ end
30
+
31
+ it "sets an empty header hash so we can set headers afterwards" do
32
+ req = subject.build_request(:get)
33
+ req[:headers].must_equal({})
34
+ end
35
+
36
+ it "passes the instrumentor if present" do
37
+ Diffbot.instrumentor = instrumentor = Object.new
38
+ req = subject.build_request(:get)
39
+ req[:instrumentor].must_equal(instrumentor)
40
+ req[:instrumentor_name].must_equal("diffbot")
41
+ end
42
+ end
43
+
44
+ describe "#perform" do
45
+ before do
46
+ Excon.stub(method: :get) do |params|
47
+ body = params[:query].key?(:a) ? "Hello, A" : "Hello"
48
+ { body: body, status: 200 }
49
+ end
50
+ end
51
+
52
+ it "returns the raw response" do
53
+ response = subject.perform(:get, "http://example.org")
54
+ response.body.must_equal("Hello")
55
+ end
56
+
57
+ it "can pass query parameters" do
58
+ response = subject.perform(:get, "http://example.org", a: 1)
59
+ response.body.must_equal("Hello, A")
60
+ end
61
+
62
+ it "can modify the request" do
63
+ response = subject.perform(:get, "http://example.org") do |req|
64
+ req[:query][:a] = 1
65
+ end
66
+ response.body.must_equal("Hello, A")
67
+ end
68
+ end
69
+ end
@@ -1,2 +1,15 @@
1
1
  require "minitest/spec"
2
2
  require "minitest/autorun"
3
+ require "diffbot"
4
+
5
+ begin
6
+ require "purdytest"
7
+ rescue LoadError
8
+ # Oh well, no colorized tests for you. You can always use minitest/pride if
9
+ # you want :P
10
+ end
11
+
12
+ MiniTest::Unit::TestCase.add_setup_hook do
13
+ Diffbot::Request.testing = true
14
+ Diffbot.reset!
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diffbot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
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: 2012-02-06 00:00:00.000000000 Z
12
+ date: 2012-02-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: excon
16
- requirement: &70280593864880 !ruby/object:Gem::Requirement
16
+ requirement: &70220474094100 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70280593864880
24
+ version_requirements: *70220474094100
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: yajl-ruby
27
- requirement: &70280593864420 !ruby/object:Gem::Requirement
27
+ requirement: &70220474093520 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70280593864420
35
+ version_requirements: *70220474093520
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: nokogiri
38
- requirement: &70280593864000 !ruby/object:Gem::Requirement
38
+ requirement: &70220474092960 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70280593864000
46
+ version_requirements: *70220474092960
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: hashie
49
- requirement: &70280593863580 !ruby/object:Gem::Requirement
49
+ requirement: &70220474092340 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70280593863580
57
+ version_requirements: *70220474092340
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: minitest
60
- requirement: &70280593863160 !ruby/object:Gem::Requirement
60
+ requirement: &70220474091460 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,7 +65,7 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70280593863160
68
+ version_requirements: *70220474091460
69
69
  description: Diffbot provides a concise API for analyzing and extracting semantic
70
70
  information from web pages using Diffbot (http://www.diffbot.com).
71
71
  email: hi@nicolassanguinetti.info
@@ -85,7 +85,9 @@ files:
85
85
  - lib/diffbot/frontpage/dml_parser.rb
86
86
  - lib/diffbot/item.rb
87
87
  - lib/diffbot/request.rb
88
+ - test/article_test.rb
88
89
  - test/coercible_hash_test.rb
90
+ - test/request_test.rb
89
91
  - test/test_helper.rb
90
92
  homepage: http://github.com/tinder/diffbot
91
93
  licenses: []