zapt_in 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -3,3 +3,4 @@ pkg/*
3
3
  .bundle
4
4
  doc/*
5
5
  test.rb
6
+ .rvmrc
data/Gemfile.lock CHANGED
@@ -1,13 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- zapt_in (0.0.1)
4
+ zapt_in (0.0.2)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
8
8
  specs:
9
9
  ansi (1.2.5)
10
10
  fakeweb (1.3.0)
11
+ mocha (0.9.12)
11
12
  turn (0.8.2)
12
13
  ansi (>= 1.2.2)
13
14
 
@@ -15,6 +16,7 @@ PLATFORMS
15
16
  ruby
16
17
 
17
18
  DEPENDENCIES
18
- fakeweb
19
+ fakeweb (~> 1.3.0)
20
+ mocha
19
21
  turn (~> 0.8.2)
20
22
  zapt_in!
data/README.md CHANGED
@@ -7,9 +7,9 @@ More information for API here:
7
7
  http://zapt.in/pages/api
8
8
 
9
9
  Project goals:
10
- * Easy to use;
11
- * Flexible to configure;
12
- * No external dependencies;
10
+ *Easy to use
11
+ *Flexible to configure
12
+ *No external dependencies
13
13
 
14
14
  Installation
15
15
  ------------
@@ -19,13 +19,28 @@ Installation
19
19
  Usage
20
20
  -----
21
21
 
22
+ Case 1:
23
+
22
24
  require "rubygems"
23
25
  require "zapt_in"
24
26
 
25
- client = ZaptIn::Client.new(:login => "login", :key => "Z_123") #=> #<ZaptIn::Client:0xb7403140 @key="Z_123", @login="login", @uri="http://zapt.in/api/links">
26
- url = client.shorten("http://google.com") #=> #<ZaptIn::Url:0xb702daac @status_code="OK", @short_url="http://zapt.in/11jU", @error_code="0", @error_message=nil>
27
+ client = ZaptIn::Client.new(:login => "login", :key => "Z_123")
28
+ url = client.shorten("http://google.com")
27
29
  url.short_url #=> "http://zapt.in/11jU"
28
30
 
31
+ Case 2:
32
+
33
+ require "rubygems"
34
+ require "zapt_in"
35
+
36
+ #This is useful to setup config at Rails initializer for example.
37
+ ZaptIn.setup do |client|
38
+ client.uri = "http://abr.io/api/links"
39
+ client.login = "login"
40
+ client.key = "key"
41
+ end
42
+ url = ZaptIn.shorten("http://google.com")
43
+ url.short_url #=> "http://abr.io/18VN"
29
44
 
30
45
  Running the Tests
31
46
  -----------------
@@ -36,7 +51,7 @@ Install development dependencies with:
36
51
 
37
52
  To run the test suite:
38
53
 
39
- $ rake
54
+ $ rake test
40
55
 
41
56
 
42
57
  Contributing
@@ -2,7 +2,7 @@
2
2
  #Client proxy for Zapt.In or Abr.io API.
3
3
  class ZaptIn::Client
4
4
 
5
- attr_reader :uri, :login, :key
5
+ attr_accessor :uri, :login, :key
6
6
 
7
7
  DEFAULT_PARAMS = {:format => "xml", :version => "1.0"}
8
8
 
@@ -26,8 +26,8 @@ class ZaptIn::Client
26
26
  return nil if long_url.nil? || long_url == ""
27
27
 
28
28
  @uri = options.delete(:uri) || @uri
29
- params = {:login => @login || options.delete(:login),
30
- :key => @key || options.delete(:key)}.merge(DEFAULT_PARAMS)
29
+ params = {:login => options.delete(:login) || @login,
30
+ :key => options.delete(:key) || @key}.merge(DEFAULT_PARAMS)
31
31
  params[:longUrl] = long_url
32
32
 
33
33
  begin
@@ -10,10 +10,10 @@ class ZaptIn::Request
10
10
  #
11
11
  #Returns response body, a String.
12
12
  def self.get(uri, parameters)
13
- url = URI.parse(uri)
14
13
  params = build_parameters(parameters)
15
14
 
16
15
  begin
16
+ url = URI.parse(uri)
17
17
  req = Net::HTTP::Get.new("#{url}?#{params}")
18
18
  res = Net::HTTP.start(url.host, url.port) do |http|
19
19
  http.request(req)
@@ -24,7 +24,7 @@ class ZaptIn::Request
24
24
  end
25
25
  end
26
26
 
27
- protected
27
+ private
28
28
 
29
29
  #Receives a Hash.
30
30
  #
data/lib/zapt_in/url.rb CHANGED
@@ -17,16 +17,23 @@ class ZaptIn::Url
17
17
  #
18
18
  #See documentation at http://zapt.in/pages/api
19
19
  def self.parse(xml_body)
20
+ validates_xml(xml_body)
21
+
20
22
  doc = REXML::Document.new(xml_body)
21
23
 
22
- attributes = {:status_code => doc.elements["zaptin/statusCode"].text,
23
- :error_message => doc.elements["zaptin/errorMessage"].text,
24
- :error_code => doc.elements["zaptin/errorCode"].text}
24
+ attributes = {:status_code => doc.elements["*/statusCode"].text,
25
+ :error_message => doc.elements["*/errorMessage"].text,
26
+ :error_code => doc.elements["*/errorCode"].text}
25
27
 
26
- if (result = doc.elements["zaptin/results/nodeKeyVal"])
28
+ if (result = doc.elements["*/results/nodeKeyVal"])
27
29
  attributes.merge!({:short_url => result.elements["shortUrl"].text})
28
30
  end
29
31
  self.new(attributes)
30
32
  end
31
33
 
34
+ def self.validates_xml(xml)
35
+ raise ZaptIn::Error.new("XML body cannot be blank") if xml.nil? || xml.empty?
36
+ raise ZaptIn::Error.new("Does not appear to be a valid ZaptIn or Abrio XML.\n\n#{xml}") unless xml.start_with?("<zaptin>") || xml.start_with?("<abrio>")
37
+ end
38
+
32
39
  end
@@ -1,3 +1,3 @@
1
1
  module ZaptIn
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/zapt_in.rb CHANGED
@@ -3,4 +3,29 @@ module ZaptIn
3
3
  autoload :Request, "zapt_in/request"
4
4
  autoload :Url, "zapt_in/url"
5
5
  autoload :Error, "zapt_in/error"
6
+
7
+ # Useful to set ZaptIn client parameters.
8
+ #
9
+ # ZaptIn.setup do |client|
10
+ # client.uri = "http://abr.io/api/links"
11
+ # client.login = "login"
12
+ # client.key = "key"
13
+ # end
14
+ #
15
+ def self.setup
16
+ yield(client) if block_given?
17
+ end
18
+
19
+ # Shortcut to ZaptIn::Client.shorten
20
+ def self.shorten(long_url, options = {})
21
+ client.shorten(long_url, options)
22
+ end
23
+
24
+ protected
25
+
26
+ #Returns ZaptIn::Client instance
27
+ def self.client
28
+ @@client ||= Client.new
29
+ end
30
+
6
31
  end
@@ -0,0 +1,68 @@
1
+ module ZaptIn::Fixtures
2
+
3
+ module XmlResponse
4
+
5
+ def zaptin_success_xml
6
+ <<XML
7
+ <zaptin>
8
+ <errorCode>0</errorCode>
9
+ <errorMessage></errorMessage>
10
+ <results>
11
+ <nodeKeyVal>
12
+ <userHash>11jU</userHash>
13
+ <shortKeywordUrl></shortKeywordUrl>
14
+ <hash>9</hash>
15
+ <nodeKey>
16
+ <![CDATA[http://google.com]]>
17
+ </nodeKey>
18
+ <shortUrl>http://zapt.in/11jU</shortUrl>
19
+ </nodeKeyVal>
20
+ </results>
21
+ <statusCode>OK</statusCode>
22
+ </zaptin>
23
+ XML
24
+ end
25
+
26
+ def zaptin_error_xml
27
+ <<XML
28
+ <zaptin>
29
+ <errorCode>203</errorCode>
30
+ <errorMessage>You must be authenticated to access shorten.</errorMessage>
31
+ <statusCode>ERROR</statusCode>
32
+ </zaptin>
33
+ XML
34
+ end
35
+
36
+ def abrio_success_xml
37
+ <<XML
38
+ <abrio>
39
+ <errorCode>0</errorCode>
40
+ <errorMessage></errorMessage>
41
+ <results>
42
+ <nodeKeyVal>
43
+ <userHash>18VN</userHash>
44
+ <shortKeywordUrl></shortKeywordUrl>
45
+ <hash>3u</hash>
46
+ <nodeKey>
47
+ <![CDATA[http://google.com]]>
48
+ </nodeKey>
49
+ <shortUrl>http://abr.io/18VN</shortUrl>
50
+ </nodeKeyVal>
51
+ </results>
52
+ <statusCode>OK</statusCode>
53
+ </abrio>
54
+ XML
55
+ end
56
+
57
+ def abrio_error_xml
58
+ <<XML
59
+ <abrio>
60
+ <errorCode>203</errorCode>
61
+ <errorMessage>You must be authenticated to access shorten.</errorMessage>
62
+ <statusCode>ERROR</statusCode>
63
+ </abrio>
64
+ XML
65
+ end
66
+
67
+ end
68
+ end
data/test/test_helper.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  require 'rubygems'
2
+
2
3
  require 'test/unit'
3
- require 'ruby-debug'
4
- require 'turn'
4
+ require 'mocha'
5
5
  require 'fakeweb'
6
6
 
7
+ require 'turn'
8
+
7
9
  FakeWeb.allow_net_connect = false
8
10
 
9
11
  class Test::Unit::TestCase
@@ -16,3 +18,8 @@ class Test::Unit::TestCase
16
18
  end
17
19
 
18
20
  require 'zapt_in'
21
+
22
+ module ZaptIn::Fixtures
23
+ autoload :XmlResponse, "test/fixtures/xml_response"
24
+ end
25
+
@@ -2,8 +2,8 @@ require 'test_helper'
2
2
 
3
3
  class ZaptIn::ClientTest < Test::Unit::TestCase
4
4
 
5
- def setup
6
- end
5
+ include ZaptIn::Fixtures::XmlResponse
6
+ DEFAULT_EXPECTED_PARAMS = {:longUrl => 'http://google.com', :version => '1.0', :format => 'xml', :key => 'Z_VALID', :login => 'rogleite'}
7
7
 
8
8
  def test_default_configurations
9
9
  assert_equal "http://zapt.in/api/links", ZaptIn::Client.new.uri
@@ -16,23 +16,22 @@ class ZaptIn::ClientTest < Test::Unit::TestCase
16
16
  end
17
17
 
18
18
  def test_shorten_with_valid_value
19
- client = ZaptIn::Client.new(:login => "rogleite", :key => "Z_321")
19
+ client = ZaptIn::Client.new(:login => "rogleite", :key => "Z_VALID")
20
20
 
21
- FakeWeb.clean_registry
22
- register_uri("http://zapt.in/api/links/shorten?version=1.0&login=rogleite&format=xml&key=Z_321&longUrl=http://google.com", :fixture => "shorten_success")
23
- zapt_url = client.shorten("http://google.com")
21
+ ZaptIn::Request.expects(:get).with("#{client.uri}/shorten", DEFAULT_EXPECTED_PARAMS).returns(zaptin_success_xml)
24
22
 
23
+ zapt_url = client.shorten("http://google.com")
25
24
  assert_not_nil zapt_url, "expects a ZaptIn::Url"
26
25
  assert_equal "http://zapt.in/11jU", zapt_url.short_url
27
26
  end
28
27
 
29
28
  def test_shorten_options_parameter
30
- client = ZaptIn::Client.new(:login => "rogleite", :key => "Z_321")
29
+ client = ZaptIn::Client.new(:login => "rogleite", :key => "Z_VALID")
31
30
 
32
- FakeWeb.clean_registry
33
- register_uri("http://abr.io/api/links/shorten?version=1.0&login=rogleite&format=xml&key=Z_321&longUrl=http://google.com", :fixture => "shorten_success")
31
+ expected_params = DEFAULT_EXPECTED_PARAMS.merge({:key => 'Z_OPTION', :login => 'login'})
32
+ ZaptIn::Request.expects(:get).with("http://abr.io/api/links/shorten", expected_params).returns(abrio_success_xml)
34
33
 
35
- zapt_url = client.shorten("http://google.com", :uri => "http://abr.io/api/links", :login => "login", :key => "Z_123")
34
+ zapt_url = client.shorten("http://google.com", :uri => "http://abr.io/api/links", :login => "login", :key => "Z_OPTION")
36
35
  assert_not_nil zapt_url, "expects a ZaptIn::Url"
37
36
  assert_equal "OK", zapt_url.status_code
38
37
  end
@@ -40,8 +39,8 @@ class ZaptIn::ClientTest < Test::Unit::TestCase
40
39
  def test_shorten_failure
41
40
  client = ZaptIn::Client.new(:login => "rogleite", :key => "Z_INVALID")
42
41
 
43
- FakeWeb.clean_registry
44
- register_uri("http://zapt.in/api/links/shorten?longUrl=http://google.com&version=1.0&login=rogleite&format=xml&key=Z_INVALID", :fixture => "shorten_failure")
42
+ expected_params = DEFAULT_EXPECTED_PARAMS.merge({:key => 'Z_INVALID'})
43
+ ZaptIn::Request.expects(:get).with("#{client.uri}/shorten", expected_params).returns(zaptin_error_xml)
45
44
 
46
45
  zapt_url = client.shorten("http://google.com")
47
46
  assert_not_nil zapt_url, "expects a ZaptIn::Url"
@@ -50,13 +49,12 @@ class ZaptIn::ClientTest < Test::Unit::TestCase
50
49
  end
51
50
 
52
51
  def test_shorten_net_http_error
53
- FakeWeb.allow_net_connect = true
54
-
52
+ expected_params = DEFAULT_EXPECTED_PARAMS.merge({:key => 'Z_INVALID'})
53
+ ZaptIn::Request.expects(:get).with("http://nonexist/shorten", expected_params).raises(ZaptIn::Error, "Net fake error")
54
+
55
55
  client = ZaptIn::Client.new(:login => "rogleite", :key => "Z_INVALID")
56
56
  zapt_url = client.shorten("http://google.com", :uri => "http://nonexist")
57
57
 
58
- FakeWeb.allow_net_connect = false
59
-
60
58
  assert_not_nil zapt_url, "expects a ZaptIn::Url"
61
59
  assert_equal "ERROR", zapt_url.status_code
62
60
  assert_nil zapt_url.short_url
@@ -2,29 +2,23 @@ require 'test_helper'
2
2
 
3
3
  class ZaptIn::RequestTest < Test::Unit::TestCase
4
4
 
5
- def test_build_parameters
6
- params = {:format => "xml", :version => "1.0", :login => "login", :key => "key"}
7
- assert_equal "version=1.0&login=login&format=xml&key=key", ZaptIn::Request.build_parameters(params)
8
- end
9
-
10
5
  def test_get_request
11
- register_uri(/zapt\.in\/api\/links\/shorten/, :fixture => "shorten_success")
6
+ register_uri("http://zapt.in/api/links/shorten?param=value", :fixture => "shorten_success")
12
7
 
13
8
  uri = "http://zapt.in/api/links/shorten"
14
- params = {:format => "xml", :version => "1.0", :login => "login", :key => "key"}
9
+ params = {:param => "value"}
15
10
 
16
11
  response_body = ZaptIn::Request.get(uri, params)
17
- assert_match /zaptin/, response_body
12
+ assert_match(/<zaptin>/, response_body, "Response should start with <zaptin>")
18
13
  end
19
14
 
20
15
  def test_get_invalid_request
21
- FakeWeb.allow_net_connect = true
22
16
 
17
+ Net::HTTP.expects(:start).with("invalid.com", 80).raises(ArgumentError)
23
18
  assert_raise ZaptIn::Error do
24
- ZaptIn::Request.get("http://nonexist", {})
19
+ ZaptIn::Request.get("http://invalid.com", {})
25
20
  end
26
21
 
27
- FakeWeb.allow_net_connect = false
28
22
  end
29
23
 
30
24
  end
@@ -0,0 +1,57 @@
1
+ require 'test_helper'
2
+
3
+ class ZaptIn::UrlTest < Test::Unit::TestCase
4
+
5
+ include ZaptIn::Fixtures::XmlResponse
6
+
7
+ def assert_success_url(url, attrs)
8
+ assert_not_nil url
9
+ assert_equal "0", url.error_code
10
+ assert_equal "OK", url.status_code
11
+ assert_equal attrs[:short_url], url.short_url
12
+ end
13
+
14
+ def assert_error_url(url, code, message)
15
+ assert_not_nil url
16
+ assert_equal code, url.error_code
17
+ assert_equal message, url.error_message
18
+ assert_equal "ERROR", url.status_code
19
+ assert_equal nil, url.short_url
20
+ end
21
+
22
+ def test_parse_zaptin_success_xml
23
+ url = ZaptIn::Url.parse(zaptin_success_xml)
24
+ assert_success_url url, :short_url => "http://zapt.in/11jU"
25
+ end
26
+
27
+ def test_parse_zaptin_error_xml
28
+ url = ZaptIn::Url.parse(zaptin_error_xml)
29
+ assert_error_url url, "203", "You must be authenticated to access shorten."
30
+ end
31
+
32
+ def test_parse_abrio_success_xml
33
+ url = ZaptIn::Url.parse(abrio_success_xml)
34
+ assert_success_url url, :short_url => "http://abr.io/18VN"
35
+ end
36
+
37
+ def test_parse_abrio_error_xml
38
+ url = ZaptIn::Url.parse(abrio_error_xml)
39
+ assert_error_url url, "203", "You must be authenticated to access shorten."
40
+ end
41
+
42
+ def test_parse_blank_xml
43
+ assert_raise ZaptIn::Error do
44
+ ZaptIn::Url.parse(nil)
45
+ end
46
+ assert_raise ZaptIn::Error do
47
+ ZaptIn::Url.parse("")
48
+ end
49
+ end
50
+
51
+ def test_parse_should_check_for_root_tags
52
+ assert_raise ZaptIn::Error do
53
+ ZaptIn::Url.parse("<h1>Some Error</h1>")
54
+ end
55
+ end
56
+
57
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+
3
+ class ZaptInTest < Test::Unit::TestCase
4
+
5
+ def test_setup
6
+ ZaptIn.setup do |client|
7
+ client.uri = "http://abr.io/api/links"
8
+ client.login = "login"
9
+ client.key = "key"
10
+ end
11
+
12
+ assert_equal "http://abr.io/api/links", ZaptIn.client.uri
13
+ assert_equal "login", ZaptIn.client.login
14
+ assert_equal "key", ZaptIn.client.key
15
+ end
16
+
17
+ def test_shorten
18
+ client = mock() do
19
+ expects(:shorten).with("google.com", {}).returns("OK")
20
+ end
21
+ ZaptIn.expects(:client).returns(client)
22
+
23
+ result = ZaptIn.shorten("google.com")
24
+ assert_equal "OK", result
25
+ end
26
+
27
+ end
data/zapt_in.gemspec CHANGED
@@ -15,7 +15,8 @@ Gem::Specification.new do |s|
15
15
 
16
16
  #s.rubyforge_project = "zapt_in"
17
17
  s.add_development_dependency "turn", "~> 0.8.2"
18
- s.add_development_dependency "fakeweb", "~> 1.0.7"
18
+ s.add_development_dependency "fakeweb", "~> 1.3.0"
19
+ s.add_development_dependency "mocha", "~> 0.9.12"
19
20
 
20
21
  s.files = `git ls-files`.split("\n")
21
22
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zapt_in
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Roger Leite
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-06 00:00:00 -03:00
18
+ date: 2011-06-07 00:00:00 -03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -42,14 +42,30 @@ dependencies:
42
42
  requirements:
43
43
  - - ~>
44
44
  - !ruby/object:Gem::Version
45
- hash: 25
45
+ hash: 27
46
46
  segments:
47
47
  - 1
48
+ - 3
48
49
  - 0
49
- - 7
50
- version: 1.0.7
50
+ version: 1.3.0
51
51
  type: :development
52
52
  version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: mocha
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ hash: 35
62
+ segments:
63
+ - 0
64
+ - 9
65
+ - 12
66
+ version: 0.9.12
67
+ type: :development
68
+ version_requirements: *id003
53
69
  description: Client ruby para os encurtadores http://zapt.in e http://abr.io
54
70
  email:
55
71
  - roger.leite@abril.com.br
@@ -72,11 +88,13 @@ files:
72
88
  - lib/zapt_in/request.rb
73
89
  - lib/zapt_in/url.rb
74
90
  - lib/zapt_in/version.rb
75
- - test/fixtures/shorten_failure
76
91
  - test/fixtures/shorten_success
92
+ - test/fixtures/xml_response.rb
77
93
  - test/test_helper.rb
78
94
  - test/zapt_in/client_test.rb
79
95
  - test/zapt_in/request_test.rb
96
+ - test/zapt_in/url_test.rb
97
+ - test/zapt_in_test.rb
80
98
  - zapt_in.gemspec
81
99
  has_rdoc: true
82
100
  homepage: ""
@@ -1,18 +0,0 @@
1
- HTTP/1.1 200 OK
2
- Server: nginx/0.7.67
3
- Date: Mon, 06 Jun 2011 17:42:58 GMT
4
- Content-Type: application/xml; charset=utf-8
5
- Connection: keep-alive
6
- X-Runtime: 12
7
- Etag: "9cd7a5d7537c402e82dfc1612e7d2dea"
8
- Cache-Control: private, max-age=0, must-revalidate
9
- Content-Length: 153
10
- X-Varnish: 247868969
11
- Age: 0
12
- Via: 1.1 varnish
13
-
14
- <zaptin>
15
- <errorCode>203</errorCode>
16
- <errorMessage>You must be authenticated to access shorten.</errorMessage>
17
- <statusCode>ERROR</statusCode>
18
- </zaptin>