clnk_api 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +36 -0
- data/Rakefile +2 -0
- data/clnk_api.gemspec +25 -0
- data/lib/clnk_api/clnk.rb +26 -0
- data/lib/clnk_api/errors.rb +17 -0
- data/lib/clnk_api/link.rb +76 -0
- data/lib/clnk_api/version.rb +3 -0
- data/lib/clnk_api.rb +13 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 319a1378a4827e9b36cbac7023f1fa37507da0c6
|
4
|
+
data.tar.gz: 1051887458b895c9678b78d718031b298076469b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 89ed56ae05a9642d33248971dd9088c5d6836020c898e4ac90cdf9c99c3298177079116387da0d6e92e310149842ff24c6e76827e41cba30b2a5f84779d0f11c
|
7
|
+
data.tar.gz: 2fbe5a440bff9c3702b41c8e38a7e06963b0d9062f33502f65f0898cbb4b62fc01f5591b6cb8e567482889c0c38f8bb94bd030996604442d241dc7c76352fb6e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 amardaxini
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# ClnkApi
|
2
|
+
|
3
|
+
Clnk Shortner API
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'clnk_api'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install clnk_api
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
clnk = ClnkApi::Clnk.new("AccessToken")
|
24
|
+
# Clnk::Link contains :short_url,:long_url,short_code
|
25
|
+
link = clnk.shorten(url) # return ClnkApi::Link
|
26
|
+
link = clnk.expand("shortcode") # return ClnkApi::Link
|
27
|
+
link = clnk.info("shorturl") # return ClnkApi::Link
|
28
|
+
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
1. Fork it ( https://github.com/[my-github-username]/clnk_api/fork )
|
33
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
34
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
35
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
36
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/clnk_api.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'clnk_api/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "clnk_api"
|
8
|
+
spec.version = ClnkApi::VERSION
|
9
|
+
spec.authors = ["amardaxini"]
|
10
|
+
spec.email = ["amardaxini@gmail.com"]
|
11
|
+
spec.summary = %q{Clnk Client API}
|
12
|
+
spec.description = %q{Clnk Client API}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "httparty"
|
24
|
+
spec.add_development_dependency "multi_json"
|
25
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module ClnkApi
|
2
|
+
class Clnk
|
3
|
+
|
4
|
+
attr_accessor :api_key
|
5
|
+
def initialize(api_key)
|
6
|
+
@api_key = api_key
|
7
|
+
end
|
8
|
+
|
9
|
+
def shorten(url)
|
10
|
+
link = ClnkApi::Link.new(@api_key)
|
11
|
+
link.shorten(url)
|
12
|
+
link
|
13
|
+
end
|
14
|
+
|
15
|
+
def info(url)
|
16
|
+
link = ClnkApi::Link.new(@api_key)
|
17
|
+
link.info(url)
|
18
|
+
link
|
19
|
+
end
|
20
|
+
def expand(url)
|
21
|
+
link = ClnkApi::Link.new(@api_key)
|
22
|
+
link.expand(url)
|
23
|
+
link
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ClnkApi
|
2
|
+
class ClnkError < StandardError
|
3
|
+
attr_reader :data
|
4
|
+
|
5
|
+
def initialize(data)
|
6
|
+
@data = data
|
7
|
+
super
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class RateLimitExceeded < StandardError; end
|
12
|
+
class Unauthorized < StandardError; end
|
13
|
+
class General < ClnkError; end
|
14
|
+
class InformClnk < StandardError; end
|
15
|
+
class Unavailable < StandardError; end
|
16
|
+
class NotFound < StandardError; end
|
17
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'pry'
|
2
|
+
module ClnkApi
|
3
|
+
class Link
|
4
|
+
include HTTParty
|
5
|
+
base_uri 'http://api.clnk.in'
|
6
|
+
attr_accessor :long_url, :short_url, :short_code,:api_key
|
7
|
+
def initialize(api_key)
|
8
|
+
@api_key = api_key
|
9
|
+
end
|
10
|
+
|
11
|
+
def validate_url(url)
|
12
|
+
raise ClnkApi::General.new("Url is invalid.") unless url =~ /\A#{URI::regexp(['http', 'https'])}\z/
|
13
|
+
end
|
14
|
+
|
15
|
+
def info(url)
|
16
|
+
validate_url(url)
|
17
|
+
self.short_url = url
|
18
|
+
options = {:query=>{ :short_url=> url,:access_token=> @api_key} }
|
19
|
+
response = self.class.get("/api/v1/links/info", options)
|
20
|
+
handle_response(response)
|
21
|
+
end
|
22
|
+
|
23
|
+
def expand(short_code)
|
24
|
+
self.short_code = short_code
|
25
|
+
options = {:query=>{ :short_code=> short_code,:access_token=> @api_key} }
|
26
|
+
response = self.class.get("/api/v1/links/expand", options)
|
27
|
+
handle_response(response)
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
def shorten(url)
|
33
|
+
validate_url(url)
|
34
|
+
long_url = url
|
35
|
+
options = {:body=>{ :long_url=> long_url,:access_token=> @api_key} }
|
36
|
+
response = self.class.post("/api/v1/links/shorten", options)
|
37
|
+
handle_response(response)
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
def handle_response(response)
|
45
|
+
raise_errors(response)
|
46
|
+
build_response(response)
|
47
|
+
end
|
48
|
+
|
49
|
+
def raise_errors(response)
|
50
|
+
code = response.code.to_i
|
51
|
+
case code
|
52
|
+
when 400
|
53
|
+
raise ClnkApi::General.new("Parameter check failed. This error indicates that a required parameter is missing or a parameter has a value that is out of bounds.")
|
54
|
+
when 401
|
55
|
+
raise ClnkApi::Unauthorized.new
|
56
|
+
when 404
|
57
|
+
raise ClnkApi::NotFound.new
|
58
|
+
when 500
|
59
|
+
raise ClnkApi::InformClnk.new
|
60
|
+
when 503
|
61
|
+
raise ClnkApi::Unavailable.new
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def build_response(response)
|
66
|
+
unless response.body.strip.empty?
|
67
|
+
url_data = MultiJson.load(response.body)["data"] || {} rescue {}
|
68
|
+
self.long_url ||= url_data["long_url"]
|
69
|
+
self.short_url ||= url_data["short_url"]
|
70
|
+
self.short_code ||= url_data["short_code"]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
data/lib/clnk_api.rb
ADDED
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: clnk_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- amardaxini
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: httparty
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: multi_json
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Clnk Client API
|
70
|
+
email:
|
71
|
+
- amardaxini@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- clnk_api.gemspec
|
82
|
+
- lib/clnk_api.rb
|
83
|
+
- lib/clnk_api/clnk.rb
|
84
|
+
- lib/clnk_api/errors.rb
|
85
|
+
- lib/clnk_api/link.rb
|
86
|
+
- lib/clnk_api/version.rb
|
87
|
+
homepage: ''
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.2.2
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Clnk Client API
|
111
|
+
test_files: []
|