dljbz 0.0.3 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Gemfile +2 -2
- data/README.md +7 -4
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/dljbz.gemspec +7 -3
- data/lib/dljbz/request.rb +26 -2
- data/lib/dljbz/v2/short.rb +17 -0
- data/lib/dljbz/version.rb +1 -1
- data/lib/dljbz.rb +5 -20
- metadata +13 -12
- data/lib/dljbz/base.rb +0 -31
- data/lib/dljbz/shorten.rb +0 -25
- data/lib/dljbz/utils.rb +0 -26
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 93a255272b20392a7e31af2415c83137e0382a7199345e937fb9eac05c13626c
|
4
|
+
data.tar.gz: f6e0244cd274dfe54946501f21a92f56a286e5885fe6e05731c0b2c2d9cf0901
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33f2cc09b1f6ac16791c4815d1e26e020133d8a954ea46bf81598e979d35b8183992f42171a232d5fb584b7412ace1281cc000d31c4582b8794bf5d0fa9a7fa4
|
7
|
+
data.tar.gz: 7ccd22fcb875eae5a0cd463cbb8788dcfb8327b3b8bfa31bbcffc8ccd3be907e333ed82e7b4b7db9436eae065bb4e7e4404ef1fed7a586e94862880936f9c1b2
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -18,16 +18,19 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
+
create rails file: config/initializers/dljbz.rb
|
22
|
+
```
|
23
|
+
Dljbz.api_key = 'api_key' # from: http://dlj.bz/dashboard/api_key
|
24
|
+
```
|
21
25
|
|
22
26
|
```
|
23
|
-
url = Dljbz.
|
24
|
-
url.
|
25
|
-
=> "http://dlj.bz/51qd"
|
27
|
+
url = Dljbz::V2::Short.request('http://www.sina.com.cn')
|
28
|
+
=> {"short"=>"http://dlj.bz/H0Jo6e", "url"=>"http://www.sina.com.cn"}
|
26
29
|
```
|
27
30
|
|
28
31
|
## Contributing
|
29
32
|
|
30
|
-
1. Fork it ( https://github.com/
|
33
|
+
1. Fork it ( https://github.com/growcn/dljbz )
|
31
34
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
32
35
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
33
36
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "dljbz"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/dljbz.gemspec
CHANGED
@@ -13,13 +13,17 @@ Gem::Specification.new do |spec|
|
|
13
13
|
spec.homepage = "http://github.com/growcn/dljbz"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
|
-
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
# spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
end
|
17
20
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
21
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
22
|
spec.require_paths = ["lib"]
|
20
23
|
|
21
24
|
spec.add_development_dependency "bundler", "~> 1.6"
|
22
25
|
spec.add_development_dependency "rake"
|
23
|
-
spec.
|
24
|
-
spec.add_dependency
|
26
|
+
spec.add_development_dependency "pry" if ENV['PRY']
|
27
|
+
spec.add_dependency "rest-client"
|
28
|
+
spec.add_dependency(%q<json>, [">= 1.4.6"])
|
25
29
|
end
|
data/lib/dljbz/request.rb
CHANGED
@@ -1,5 +1,29 @@
|
|
1
1
|
module Dljbz
|
2
|
-
class Request
|
3
|
-
|
2
|
+
class Request
|
3
|
+
API_URL = 'http://dlj.bz/api'
|
4
|
+
|
5
|
+
class << self
|
6
|
+
def post(api_path, params = {}, headers = {})
|
7
|
+
begin
|
8
|
+
JSON.parse(
|
9
|
+
RestClient.post(parse_path(api_path), params, app_headers.merge(headers))
|
10
|
+
)
|
11
|
+
rescue RestClient::ExceptionWithResponse => err
|
12
|
+
JSON.parse(err.response.body)
|
13
|
+
rescue => e
|
14
|
+
raise e.inspect
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def app_headers
|
19
|
+
{}
|
20
|
+
end
|
21
|
+
|
22
|
+
def parse_path(path)
|
23
|
+
host = Dljbz.endpoint || API_URL
|
24
|
+
|
25
|
+
"#{host}#{path}"
|
26
|
+
end
|
27
|
+
end
|
4
28
|
end
|
5
29
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Dljbz
|
2
|
+
module V2
|
3
|
+
class Short
|
4
|
+
# attr_accessor :params
|
5
|
+
API_SHORT_V2_URL = '/v2/short'
|
6
|
+
# Dljbz.api_key = 'c9bc59f083b691828006391ff92e0fe5'
|
7
|
+
# Dljbz::V2::Short.request('http://www.sina.com.cn')
|
8
|
+
# def initialize(long_url, opts = {})
|
9
|
+
# end
|
10
|
+
|
11
|
+
def self.request(long_url, opts = {})
|
12
|
+
params = { url: long_url, api_key: Dljbz.api_key.to_s }.merge(opts)
|
13
|
+
Dljbz::Request.post(API_SHORT_V2_URL, params)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/dljbz/version.rb
CHANGED
data/lib/dljbz.rb
CHANGED
@@ -1,25 +1,10 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
3
|
-
|
4
|
-
require 'dljbz/base'
|
5
|
-
require 'dljbz/utils'
|
1
|
+
require 'pry' if ENV['PRY']
|
2
|
+
require 'rest-client'
|
6
3
|
require 'dljbz/request'
|
7
|
-
require 'dljbz/
|
4
|
+
require 'dljbz/v2/short'
|
8
5
|
|
9
6
|
module Dljbz
|
10
|
-
|
11
|
-
|
12
|
-
# Creates a new short URL
|
13
|
-
#
|
14
|
-
# url = Dljbz.shorten('http://51qiangda.com')
|
15
|
-
# url.short_url
|
16
|
-
# => "http://dlj.bz/51qd"
|
17
|
-
#
|
18
|
-
def shorten(url=nil)
|
19
|
-
raise ArgumentError.new("URL to shorten is required") if url.nil? || url.strip.empty?
|
20
|
-
Dljbz::Shorten.new(url)
|
7
|
+
class << self
|
8
|
+
attr_accessor :api_key, :endpoint
|
21
9
|
end
|
22
|
-
|
23
|
-
|
24
|
-
|
25
10
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dljbz
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Chen
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-11-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: rest-client
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
@@ -69,7 +69,9 @@ dependencies:
|
|
69
69
|
description: Small library for dlj.bz URL Shortener API
|
70
70
|
email:
|
71
71
|
- cgg5207@sina.com
|
72
|
-
executables:
|
72
|
+
executables:
|
73
|
+
- console
|
74
|
+
- setup
|
73
75
|
extensions: []
|
74
76
|
extra_rdoc_files: []
|
75
77
|
files:
|
@@ -78,18 +80,18 @@ files:
|
|
78
80
|
- LICENSE.txt
|
79
81
|
- README.md
|
80
82
|
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
81
85
|
- dljbz.gemspec
|
82
86
|
- lib/dljbz.rb
|
83
|
-
- lib/dljbz/base.rb
|
84
87
|
- lib/dljbz/request.rb
|
85
|
-
- lib/dljbz/
|
86
|
-
- lib/dljbz/utils.rb
|
88
|
+
- lib/dljbz/v2/short.rb
|
87
89
|
- lib/dljbz/version.rb
|
88
90
|
homepage: http://github.com/growcn/dljbz
|
89
91
|
licenses:
|
90
92
|
- MIT
|
91
93
|
metadata: {}
|
92
|
-
post_install_message:
|
94
|
+
post_install_message:
|
93
95
|
rdoc_options: []
|
94
96
|
require_paths:
|
95
97
|
- lib
|
@@ -104,9 +106,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
106
|
- !ruby/object:Gem::Version
|
105
107
|
version: '0'
|
106
108
|
requirements: []
|
107
|
-
|
108
|
-
|
109
|
-
signing_key:
|
109
|
+
rubygems_version: 3.1.4
|
110
|
+
signing_key:
|
110
111
|
specification_version: 4
|
111
112
|
summary: Wrapper for dlj.bz URL Shortener API
|
112
113
|
test_files: []
|
data/lib/dljbz/base.rb
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
module Dljbz
|
2
|
-
|
3
|
-
class Base
|
4
|
-
|
5
|
-
# URL for QR Code
|
6
|
-
#
|
7
|
-
# url = Dljbz.shorten('http://51qiangda.com')
|
8
|
-
# ur.qr_code
|
9
|
-
# => http://dlj.bz/51qd.qr
|
10
|
-
#
|
11
|
-
# Usage:
|
12
|
-
#
|
13
|
-
# <img src="http://dlj.bz/51qd.qr" />
|
14
|
-
#
|
15
|
-
def qr_code
|
16
|
-
"#{short_url}.qr"
|
17
|
-
end
|
18
|
-
|
19
|
-
# URL for analytics
|
20
|
-
#
|
21
|
-
# url = Dljbz.shorten('http://51qiangda.com')
|
22
|
-
# ur.info
|
23
|
-
# => http://dlj.bz/51qd.info
|
24
|
-
#
|
25
|
-
def info
|
26
|
-
"#{short_url}.info"
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
data/lib/dljbz/shorten.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
module Dljbz
|
2
|
-
|
3
|
-
class Shorten < Base
|
4
|
-
|
5
|
-
include Dljbz::Utils
|
6
|
-
|
7
|
-
attr_accessor :short_url, :long_url
|
8
|
-
|
9
|
-
# Creates a new short URL, see Dljbz.shorten
|
10
|
-
#
|
11
|
-
def initialize(long_url)
|
12
|
-
modify_headers('Content-Type' => 'application/json')
|
13
|
-
options = {"long_url" => long_url}
|
14
|
-
resp = post(API_URL, :body => options)
|
15
|
-
if resp.code == 200 or resp.code == 201
|
16
|
-
self.short_url = resp['short_url']
|
17
|
-
self.long_url = resp['long_url']
|
18
|
-
else
|
19
|
-
raise exception(resp.parsed_response)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
data/lib/dljbz/utils.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
module Dljbz
|
2
|
-
|
3
|
-
module Utils
|
4
|
-
|
5
|
-
API_URL = 'http://dlj.bz/api/urlshortener/'
|
6
|
-
|
7
|
-
private
|
8
|
-
def modify_headers(item)
|
9
|
-
Dljbz::Request.headers.merge!(item)
|
10
|
-
end
|
11
|
-
|
12
|
-
def post(url, params={})
|
13
|
-
Dljbz::Request.post(url, params)
|
14
|
-
end
|
15
|
-
|
16
|
-
def get(url, params={})
|
17
|
-
Dljbz::Request.get(url, params)
|
18
|
-
end
|
19
|
-
|
20
|
-
def exception(msg)
|
21
|
-
Exception.new(msg)
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|