pixy 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -16,3 +16,4 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  .rvmrc
19
+ ruby/
data/README.md CHANGED
@@ -14,19 +14,22 @@ gem 'pixy'
14
14
  ## Usage
15
15
 
16
16
  ```ruby
17
- url = Pixy.shorten!(API_KEY, 'https://github.com/narkoz/pixy')
17
+ Pixy.shorten!(API_KEY, 'https://github.com/narkoz/pixy')
18
+ # => "http://p.tl/Us9R"
19
+
20
+ pixy = Pixy.shorten(API_KEY, 'https://github.com/narkoz/pixy')
18
21
  # => #<Pixy::Shorten:0x0000010201bde8 @status="ok", @long_url="https://github.com/narkoz/pixy", @short_url="http://p.tl/Us9R", @counter=20>
19
22
 
20
- url.status
23
+ pixy.status
21
24
  # => "ok"
22
25
 
23
- url.long_url
26
+ pixy.long_url
24
27
  # => "https://github.com/narkoz/pixy"
25
28
 
26
- url.short_url
29
+ pixy.short_url
27
30
  # => "http://p.tl/Us9R"
28
31
 
29
- url.counter
32
+ pixy.counter
30
33
  # => 20
31
34
  ```
32
35
 
@@ -1,14 +1,18 @@
1
1
  require 'pixy/version'
2
2
  require 'pixy/shorten'
3
+ require 'pixy/errors'
3
4
  require 'net/http'
4
5
  require 'json'
5
6
 
6
7
  module Pixy
7
8
  extend self
8
9
 
9
- def shorten!(key=nil, url='')
10
- raise ArgumentError, "API key is required" if key.nil?
10
+ def shorten(key=nil, url='')
11
+ raise MissingApiKey, "API key is required" if key.nil?
11
12
  Pixy::Shorten.new(key, url)
12
13
  end
13
- alias_method :shorten, :shorten!
14
+
15
+ def shorten!(key=nil, url='')
16
+ Pixy.shorten(key, url).short_url
17
+ end
14
18
  end
@@ -0,0 +1,9 @@
1
+ module Pixy
2
+ class EmptyLongUrl < StandardError; end
3
+ class EmptyApiKey < StandardError; end
4
+ class ApiLimit < StandardError; end
5
+ class InvalidApiKey < StandardError; end
6
+ class InvalidLongUrl < StandardError; end
7
+ class UnknownError < StandardError; end
8
+ class MissingApiKey < ArgumentError; end
9
+ end
@@ -1,10 +1,10 @@
1
1
  module Pixy
2
2
  class Shorten
3
+ API_URL = 'http://p.tl/api/api_simple.php'
3
4
  attr_accessor :status, :long_url, :short_url, :counter
4
5
 
5
6
  def initialize(key, url)
6
- uri = URI('http://p.tl/api/api_simple.php')
7
- uri.query = URI.encode_www_form({:key => key, :url => url})
7
+ uri = URI "#{API_URL}?key=#{key}&url=#{escape_url(url)}"
8
8
  response = Net::HTTP.get_response(uri)
9
9
  result = JSON.parse(response.body)
10
10
 
@@ -13,9 +13,33 @@ module Pixy
13
13
  self.long_url = result['long_url']
14
14
  self.short_url = result['short_url']
15
15
  self.counter = result['counter']
16
+ raise_exception self.status unless self.status == 'ok'
16
17
  else
17
- raise "Server responded with code #{response.code}"
18
+ raise UnknownError, "Server responded with code #{response.code}"
18
19
  end
19
20
  end
21
+
22
+ private
23
+
24
+ def raise_exception(status)
25
+ case status
26
+ when 'empty long url'
27
+ raise EmptyLongUrl, "Missing long URL."
28
+ when 'empty API key'
29
+ raise EmptyApiKey, "Missing API key."
30
+ when 'API limit'
31
+ raise ApiLimit, "API limit exceeded."
32
+ when 'invalid API key'
33
+ raise InvalidApiKey, "API key is invalid."
34
+ when 'invalid long url'
35
+ raise InvalidLongUrl, "The URL can not be shortened."
36
+ else
37
+ raise UnknownError, "Unknown status error."
38
+ end
39
+ end
40
+
41
+ def escape_url(url)
42
+ URI.escape(url, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
43
+ end
20
44
  end
21
45
  end
@@ -1,3 +1,3 @@
1
1
  module Pixy
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,6 @@
1
+ {
2
+ "status": "empty API key",
3
+ "long_url": "https://github.com/narkoz/pixy",
4
+ "short_url": "",
5
+ "counter": 0
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "status": "empty long url",
3
+ "long_url": "",
4
+ "short_url": "",
5
+ "counter": 0
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "status": "invalid API key",
3
+ "long_url": "https://github.com/narkoz/pixy",
4
+ "short_url": "",
5
+ "counter": 0
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "status": "invalid long url",
3
+ "long_url": "^_^",
4
+ "short_url": "",
5
+ "counter": 20
6
+ }
@@ -0,0 +1,6 @@
1
+ {
2
+ "status": "ok",
3
+ "long_url": "https://github.com/narkoz/pixy",
4
+ "short_url": "http://p.tl/Us9R",
5
+ "counter": 12
6
+ }
@@ -1,18 +1,24 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Pixy::Shorten do
4
- context "when a new short url requested" do
5
- it { Pixy.should respond_to(:shorten!) }
6
- it { Pixy.should respond_to(:shorten) }
4
+ it { Pixy.should respond_to :shorten }
5
+ it { Pixy.should respond_to :shorten! }
7
6
 
7
+ describe "#shorten!" do
8
+ it "should return a short url" do
9
+ Pixy.shorten!('API_KEY', 'https://github.com/narkoz/pixy').should == 'http://p.tl/Us9R'
10
+ end
11
+ end
12
+
13
+ context "when a new short url requested" do
8
14
  context "without arguments" do
9
15
  it "should raise a required API key error" do
10
- lambda { Pixy.shorten! }.should raise_error(ArgumentError, "API key is required")
16
+ lambda { Pixy.shorten }.should raise_error(Pixy::MissingApiKey, "API key is required")
11
17
  end
12
18
  end
13
19
 
14
20
  context "with arguments" do
15
- subject { Pixy.shorten!('API_KEY', 'https://github.com/narkoz/pixy') }
21
+ subject { Pixy.shorten('API_KEY', 'https://github.com/narkoz/pixy') }
16
22
 
17
23
  describe "#status" do
18
24
  it "should return a response status" do
@@ -37,6 +43,38 @@ describe Pixy::Shorten do
37
43
  subject.counter.should == 12
38
44
  end
39
45
  end
46
+
47
+ context "with empty long url" do
48
+ it "should raise EmptyLongUrl" do
49
+ lambda {
50
+ Pixy.shorten('API_KEY', '')
51
+ }.should raise_error(Pixy::EmptyLongUrl, "Missing long URL.")
52
+ end
53
+ end
54
+
55
+ context "with empty API key" do
56
+ it "should raise EmptyApiKey" do
57
+ lambda {
58
+ Pixy.shorten('', 'https://github.com/narkoz/pixy')
59
+ }.should raise_error(Pixy::EmptyApiKey, "Missing API key.")
60
+ end
61
+ end
62
+
63
+ context "with invalid API key" do
64
+ it "should raise InvalidApiKey" do
65
+ lambda {
66
+ Pixy.shorten('invalid_API_KEY', 'https://github.com/narkoz/pixy')
67
+ }.should raise_error(Pixy::InvalidApiKey, "API key is invalid.")
68
+ end
69
+ end
70
+
71
+ context "with invalid long url" do
72
+ it "should raise InvalidLongUrl" do
73
+ lambda {
74
+ Pixy.shorten('API_KEY', '^_^')
75
+ }.should raise_error(Pixy::InvalidLongUrl, "The URL can not be shortened.")
76
+ end
77
+ end
40
78
  end
41
79
  end
42
80
  end
@@ -4,12 +4,24 @@ require 'pixy'
4
4
 
5
5
  FakeWeb.allow_net_connect = false
6
6
 
7
- url_to_shorten = "https%3A%2F%2Fgithub.com%2Fnarkoz%2Fpixy"
8
- FakeWeb.register_uri(:get,
9
- "http://p.tl/api/api_simple.php?key=API_KEY&url=#{url_to_shorten}",
10
- :body => {
11
- :status => 'ok',
12
- :long_url => 'https://github.com/narkoz/pixy',
13
- :short_url => 'http://p.tl/Us9R',
14
- :counter => 12
15
- }.to_json)
7
+ def escape_url(url)
8
+ URI.escape(url, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
9
+ end
10
+
11
+ API_URL = 'http://p.tl/api/api_simple.php'
12
+ url_to_shorten = escape_url("https://github.com/narkoz/pixy")
13
+ invalid_url_to_shorten = escape_url("^_^")
14
+
15
+ def load_fixture(name)
16
+ File.open(File.dirname(__FILE__) + "/fixtures/#{name}.json").read
17
+ end
18
+
19
+ def fake_url(url, fixture_name)
20
+ FakeWeb.register_uri(:get, url, :body => load_fixture(fixture_name))
21
+ end
22
+
23
+ fake_url "#{API_URL}?key=API_KEY&url=#{url_to_shorten}", 'ok'
24
+ fake_url "#{API_URL}?key=API_KEY&url=", 'empty_long_url'
25
+ fake_url "#{API_URL}?key=&url=#{url_to_shorten}", 'empty_api_key'
26
+ fake_url "#{API_URL}?key=invalid_API_KEY&url=#{url_to_shorten}", 'invalid_api_key'
27
+ fake_url "#{API_URL}?key=API_KEY&url=#{invalid_url_to_shorten}", 'invalid_long_url'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pixy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
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: 2011-12-15 00:00:00.000000000 Z
12
+ date: 2011-12-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &2151939620 !ruby/object:Gem::Requirement
16
+ requirement: &2164331840 !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: *2151939620
24
+ version_requirements: *2164331840
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &2151939040 !ruby/object:Gem::Requirement
27
+ requirement: &2164331040 !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: :development
34
34
  prerelease: false
35
- version_requirements: *2151939040
35
+ version_requirements: *2164331040
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &2151937580 !ruby/object:Gem::Requirement
38
+ requirement: &2164330600 !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: :development
45
45
  prerelease: false
46
- version_requirements: *2151937580
46
+ version_requirements: *2164330600
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: fakeweb
49
- requirement: &2151937040 !ruby/object:Gem::Requirement
49
+ requirement: &2164329980 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2151937040
57
+ version_requirements: *2164329980
58
58
  description: Pixy is an API wrapper for Pixiv url shortener - p.tl
59
59
  email:
60
60
  - mail@narkoz.me
@@ -69,9 +69,15 @@ files:
69
69
  - README.md
70
70
  - Rakefile
71
71
  - lib/pixy.rb
72
+ - lib/pixy/errors.rb
72
73
  - lib/pixy/shorten.rb
73
74
  - lib/pixy/version.rb
74
75
  - pixy.gemspec
76
+ - spec/fixtures/empty_api_key.json
77
+ - spec/fixtures/empty_long_url.json
78
+ - spec/fixtures/invalid_api_key.json
79
+ - spec/fixtures/invalid_long_url.json
80
+ - spec/fixtures/ok.json
75
81
  - spec/pixy/shorten_spec.rb
76
82
  - spec/spec_helper.rb
77
83
  homepage: https://github.com/narkoz/pixy
@@ -99,5 +105,10 @@ signing_key:
99
105
  specification_version: 3
100
106
  summary: API wrapper for Pixiv url shortener
101
107
  test_files:
108
+ - spec/fixtures/empty_api_key.json
109
+ - spec/fixtures/empty_long_url.json
110
+ - spec/fixtures/invalid_api_key.json
111
+ - spec/fixtures/invalid_long_url.json
112
+ - spec/fixtures/ok.json
102
113
  - spec/pixy/shorten_spec.rb
103
114
  - spec/spec_helper.rb