zapt_in 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ doc/*
5
+ test.rb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in zapt_in.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ zapt_in (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ ansi (1.2.5)
10
+ fakeweb (1.3.0)
11
+ turn (0.8.2)
12
+ ansi (>= 1.2.2)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ fakeweb
19
+ turn (~> 0.8.2)
20
+ zapt_in!
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Roger Leite
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ 'Software'), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ ZaptIn
2
+ ======
3
+
4
+ A Ruby API for http://zapt.in and http://abr.io
5
+
6
+ More information for API here:
7
+ http://zapt.in/pages/api
8
+
9
+ Project goals:
10
+ * Easy to use;
11
+ * Flexible to configure;
12
+ * No external dependencies;
13
+
14
+ Installation
15
+ ------------
16
+
17
+ gem install zapt_in
18
+
19
+ Usage
20
+ -----
21
+
22
+ require "rubygems"
23
+ require "zapt_in"
24
+
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
+ url.short_url #=> "http://zapt.in/11jU"
28
+
29
+
30
+ Running the Tests
31
+ -----------------
32
+
33
+ Install development dependencies with:
34
+
35
+ $ bundle install
36
+
37
+ To run the test suite:
38
+
39
+ $ rake
40
+
41
+
42
+ Contributing
43
+ ------------
44
+
45
+ Once you've made your great commits:
46
+
47
+ 1. [Fork][0] zapt_in
48
+ 2. Create a topic branch - `git checkout -b my_branch`
49
+ 3. Push to your branch - `git push origin my_branch`
50
+ 4. Create an [Issue][1] with a link to your branch
51
+ 5. That's it!
52
+
53
+
54
+ [0]: http://help.github.com/forking/
55
+ [1]: https://github.com/rogerleite/zapt_in/issues
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'bundler'
5
+
6
+ Bundler::GemHelper.install_tasks
7
+
8
+ desc 'Generate API documentation to doc/rdocs/index.html'
9
+ Rake::RDocTask.new do |rd|
10
+ rd.rdoc_dir = 'doc'
11
+ rd.main = 'README.md'
12
+ rd.rdoc_files.include "README.md", "LICENSE", "lib/**/*\.rb"
13
+
14
+ rd.options << '--inline-source'
15
+ rd.options << '--line-numbers'
16
+ rd.options << '--all'
17
+ rd.options << '--fileboxes'
18
+ end
19
+
20
+ desc "Run tests"
21
+ Rake::TestTask.new do |t|
22
+ t.libs << "test"
23
+ t.test_files = FileList['test/**/*.rb']
24
+ t.verbose = true
25
+ end
26
+
27
+ task :default => :test
@@ -0,0 +1,41 @@
1
+
2
+ #Client proxy for Zapt.In or Abr.io API.
3
+ class ZaptIn::Client
4
+
5
+ attr_reader :uri, :login, :key
6
+
7
+ DEFAULT_PARAMS = {:format => "xml", :version => "1.0"}
8
+
9
+ #Use the following options to initialize your client:
10
+ # [uri] - Default: "zapt.in/api/links"
11
+ # [login] - Your login from Zapt.In. See: http://zapt.in/pages/api
12
+ # [key] - Your key from Zapt.In. See: http://zapt.in/pages/api
13
+ def initialize(options = {})
14
+ @uri = options[:uri] || "http://zapt.in/api/links"
15
+ @login = options[:login]
16
+ @key = options[:key]
17
+ end
18
+
19
+ #Shorten an url.
20
+ #[long_url] url you want to shorten.
21
+ #[options] Optional Hash. You can pass the following keys:
22
+ # [uri] - Default: "zapt.in/api/links"
23
+ # [login] - Your login from Zapt.In. See: http://zapt.in/pages/api
24
+ # [key] - Your key from Zapt.In. See: http://zapt.in/pages/api
25
+ def shorten(long_url, options = {})
26
+ return nil if long_url.nil? || long_url == ""
27
+
28
+ @uri = options.delete(:uri) || @uri
29
+ params = {:login => @login || options.delete(:login),
30
+ :key => @key || options.delete(:key)}.merge(DEFAULT_PARAMS)
31
+ params[:longUrl] = long_url
32
+
33
+ begin
34
+ response = ZaptIn::Request.get("#{self.uri}/shorten", params)
35
+ ZaptIn::Url.parse(response)
36
+ rescue ZaptIn::Error => e
37
+ ZaptIn::Url.new(:status_code => "ERROR", :errorMessage => e.message, :error_code => -1)
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,3 @@
1
+
2
+ class ZaptIn::Error < StandardError
3
+ end
@@ -0,0 +1,38 @@
1
+ require "net/http"
2
+ require "uri"
3
+
4
+ #Wrapper responsible for all requests.
5
+ class ZaptIn::Request
6
+
7
+ #Call get at *uri* with *parameters*.
8
+ #[uri] Complete url. Example: http://zapt.in/api/links/shorten
9
+ #[parameters] Hash of parameters. Example: {:format => "xml", :version => "1.0"}
10
+ #
11
+ #Returns response body, a String.
12
+ def self.get(uri, parameters)
13
+ url = URI.parse(uri)
14
+ params = build_parameters(parameters)
15
+
16
+ begin
17
+ req = Net::HTTP::Get.new("#{url}?#{params}")
18
+ res = Net::HTTP.start(url.host, url.port) do |http|
19
+ http.request(req)
20
+ end
21
+ res.body
22
+ rescue StandardError => e
23
+ raise ZaptIn::Error.new(e)
24
+ end
25
+ end
26
+
27
+ protected
28
+
29
+ #Receives a Hash.
30
+ #
31
+ #Returns a String in format: "key=value&key2=value2"
32
+ def self.build_parameters(params)
33
+ array_params = []
34
+ params.each_pair { |key, value| array_params << "#{key}=#{value}"}
35
+ array_params.join("&")
36
+ end
37
+
38
+ end
@@ -0,0 +1,32 @@
1
+ require 'rexml/document'
2
+
3
+ #Represents ZaptIn API response.
4
+ class ZaptIn::Url
5
+
6
+ attr_reader :status_code, :error_message, :error_code
7
+ attr_reader :short_url
8
+
9
+ #Initialize all read-only atributes with Hash.
10
+ def initialize(attrs = {})
11
+ attrs.each_pair do |attr, value|
12
+ self.instance_variable_set("@#{attr}", value)
13
+ end
14
+ end
15
+
16
+ #Parses *xml* from ZaptIn API.
17
+ #
18
+ #See documentation at http://zapt.in/pages/api
19
+ def self.parse(xml_body)
20
+ doc = REXML::Document.new(xml_body)
21
+
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}
25
+
26
+ if (result = doc.elements["zaptin/results/nodeKeyVal"])
27
+ attributes.merge!({:short_url => result.elements["shortUrl"].text})
28
+ end
29
+ self.new(attributes)
30
+ end
31
+
32
+ end
@@ -0,0 +1,3 @@
1
+ module ZaptIn
2
+ VERSION = "0.0.1"
3
+ end
data/lib/zapt_in.rb ADDED
@@ -0,0 +1,6 @@
1
+ module ZaptIn
2
+ autoload :Client, "zapt_in/client"
3
+ autoload :Request, "zapt_in/request"
4
+ autoload :Url, "zapt_in/url"
5
+ autoload :Error, "zapt_in/error"
6
+ end
@@ -0,0 +1,18 @@
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>
@@ -0,0 +1,29 @@
1
+ HTTP/1.1 200 OK
2
+ Server: nginx/0.7.67
3
+ Date: Fri, 03 Jun 2011 18:17:52 GMT
4
+ Content-Type: application/xml; charset=utf-8
5
+ Connection: keep-alive
6
+ X-Runtime: 33
7
+ ETag: "96e2aec9b5358bed89d8345f4d94b969"
8
+ Cache-Control: private, max-age=0, must-revalidate
9
+ Content-Length: 380
10
+ X-Varnish: 981428441
11
+ Age: 0
12
+ Via: 1.1 varnish
13
+
14
+ <zaptin>
15
+ <errorCode>0</errorCode>
16
+ <errorMessage></errorMessage>
17
+ <results>
18
+ <nodeKeyVal>
19
+ <userHash>11jU</userHash>
20
+ <shortKeywordUrl></shortKeywordUrl>
21
+ <hash>9</hash>
22
+ <nodeKey>
23
+ <![CDATA[http://google.com]]>
24
+ </nodeKey>
25
+ <shortUrl>http://zapt.in/11jU</shortUrl>
26
+ </nodeKeyVal>
27
+ </results>
28
+ <statusCode>OK</statusCode>
29
+ </zaptin>
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'ruby-debug'
4
+ require 'turn'
5
+ require 'fakeweb'
6
+
7
+ FakeWeb.allow_net_connect = false
8
+
9
+ class Test::Unit::TestCase
10
+
11
+ def register_uri(url, options = {})
12
+ fixture = options.delete(:fixture)
13
+ FakeWeb.register_uri(:get, url, :response => File.read("test/fixtures/#{fixture}"))
14
+ end
15
+
16
+ end
17
+
18
+ require 'zapt_in'
@@ -0,0 +1,65 @@
1
+ require 'test_helper'
2
+
3
+ class ZaptIn::ClientTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_default_configurations
9
+ assert_equal "http://zapt.in/api/links", ZaptIn::Client.new.uri
10
+ end
11
+
12
+ def test_shorten_with_blank_value
13
+ client = ZaptIn::Client.new
14
+ assert_equal nil, client.shorten(nil)
15
+ assert_equal nil, client.shorten("")
16
+ end
17
+
18
+ def test_shorten_with_valid_value
19
+ client = ZaptIn::Client.new(:login => "rogleite", :key => "Z_321")
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")
24
+
25
+ assert_not_nil zapt_url, "expects a ZaptIn::Url"
26
+ assert_equal "http://zapt.in/11jU", zapt_url.short_url
27
+ end
28
+
29
+ def test_shorten_options_parameter
30
+ client = ZaptIn::Client.new(:login => "rogleite", :key => "Z_321")
31
+
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")
34
+
35
+ zapt_url = client.shorten("http://google.com", :uri => "http://abr.io/api/links", :login => "login", :key => "Z_123")
36
+ assert_not_nil zapt_url, "expects a ZaptIn::Url"
37
+ assert_equal "OK", zapt_url.status_code
38
+ end
39
+
40
+ def test_shorten_failure
41
+ client = ZaptIn::Client.new(:login => "rogleite", :key => "Z_INVALID")
42
+
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")
45
+
46
+ zapt_url = client.shorten("http://google.com")
47
+ assert_not_nil zapt_url, "expects a ZaptIn::Url"
48
+ assert_equal "ERROR", zapt_url.status_code
49
+ assert_nil zapt_url.short_url
50
+ end
51
+
52
+ def test_shorten_net_http_error
53
+ FakeWeb.allow_net_connect = true
54
+
55
+ client = ZaptIn::Client.new(:login => "rogleite", :key => "Z_INVALID")
56
+ zapt_url = client.shorten("http://google.com", :uri => "http://nonexist")
57
+
58
+ FakeWeb.allow_net_connect = false
59
+
60
+ assert_not_nil zapt_url, "expects a ZaptIn::Url"
61
+ assert_equal "ERROR", zapt_url.status_code
62
+ assert_nil zapt_url.short_url
63
+ end
64
+
65
+ end
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+
3
+ class ZaptIn::RequestTest < Test::Unit::TestCase
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
+ def test_get_request
11
+ register_uri(/zapt\.in\/api\/links\/shorten/, :fixture => "shorten_success")
12
+
13
+ uri = "http://zapt.in/api/links/shorten"
14
+ params = {:format => "xml", :version => "1.0", :login => "login", :key => "key"}
15
+
16
+ response_body = ZaptIn::Request.get(uri, params)
17
+ assert_match /zaptin/, response_body
18
+ end
19
+
20
+ def test_get_invalid_request
21
+ FakeWeb.allow_net_connect = true
22
+
23
+ assert_raise ZaptIn::Error do
24
+ ZaptIn::Request.get("http://nonexist", {})
25
+ end
26
+
27
+ FakeWeb.allow_net_connect = false
28
+ end
29
+
30
+ end
data/zapt_in.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "zapt_in/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "zapt_in"
7
+ s.version = ZaptIn::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Roger Leite"]
10
+ s.email = ["roger.leite@abril.com.br"]
11
+ s.homepage = ""
12
+ s.summary = %q{Client ruby para os encurtadores http://zapt.in e http://abr.io}
13
+ s.description = %q{Client ruby para os encurtadores http://zapt.in e http://abr.io}
14
+ s.required_ruby_version = ">= 1.8.7"
15
+
16
+ #s.rubyforge_project = "zapt_in"
17
+ s.add_development_dependency "turn", "~> 0.8.2"
18
+ s.add_development_dependency "fakeweb", "~> 1.0.7"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zapt_in
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Roger Leite
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-06 00:00:00 -03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: turn
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 59
30
+ segments:
31
+ - 0
32
+ - 8
33
+ - 2
34
+ version: 0.8.2
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: fakeweb
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 25
46
+ segments:
47
+ - 1
48
+ - 0
49
+ - 7
50
+ version: 1.0.7
51
+ type: :development
52
+ version_requirements: *id002
53
+ description: Client ruby para os encurtadores http://zapt.in e http://abr.io
54
+ email:
55
+ - roger.leite@abril.com.br
56
+ executables: []
57
+
58
+ extensions: []
59
+
60
+ extra_rdoc_files: []
61
+
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE
67
+ - README.md
68
+ - Rakefile
69
+ - lib/zapt_in.rb
70
+ - lib/zapt_in/client.rb
71
+ - lib/zapt_in/error.rb
72
+ - lib/zapt_in/request.rb
73
+ - lib/zapt_in/url.rb
74
+ - lib/zapt_in/version.rb
75
+ - test/fixtures/shorten_failure
76
+ - test/fixtures/shorten_success
77
+ - test/test_helper.rb
78
+ - test/zapt_in/client_test.rb
79
+ - test/zapt_in/request_test.rb
80
+ - zapt_in.gemspec
81
+ has_rdoc: true
82
+ homepage: ""
83
+ licenses: []
84
+
85
+ post_install_message:
86
+ rdoc_options: []
87
+
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 57
96
+ segments:
97
+ - 1
98
+ - 8
99
+ - 7
100
+ version: 1.8.7
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ hash: 3
107
+ segments:
108
+ - 0
109
+ version: "0"
110
+ requirements: []
111
+
112
+ rubyforge_project:
113
+ rubygems_version: 1.4.1
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: Client ruby para os encurtadores http://zapt.in e http://abr.io
117
+ test_files: []
118
+