trim-api 0.0.1 → 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/lib/trim-api/trim-api.rb +23 -7
- data/lib/trim_api.rb +1 -1
- metadata +2 -2
data/History.txt
CHANGED
data/lib/trim-api/trim-api.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
module TrimApi
|
2
2
|
|
3
|
-
VERSION = "0.0.1"
|
4
3
|
API_URL = "http://api.tr.im/api/"
|
5
4
|
|
6
5
|
class TrimError < StandardError
|
@@ -13,13 +12,18 @@ module TrimApi
|
|
13
12
|
|
14
13
|
class Trim
|
15
14
|
|
15
|
+
attr_accessor :username
|
16
|
+
attr_accessor :password
|
17
|
+
|
16
18
|
def initialize(options = {})
|
17
19
|
@client = HTTPClient.new
|
20
|
+
self.username = options[:username] if options[:username]
|
21
|
+
self.password = options[:password] if options[:password]
|
18
22
|
end
|
19
23
|
|
20
|
-
def trim(original_url)
|
24
|
+
def trim(original_url, options = {})
|
21
25
|
raise ArgumentError.new(":url to shorten is required") if original_url.nil?
|
22
|
-
response = @client.get_content(create_url(original_url))
|
26
|
+
response = @client.get_content(create_url("trim_url", :url => original_url))
|
23
27
|
data = JSON.parse(response)
|
24
28
|
unless data["status"]["result"] == "OK"
|
25
29
|
raise TrimError.new(data["status"]["code"],data["status"]["message"])
|
@@ -28,12 +32,24 @@ module TrimApi
|
|
28
32
|
end
|
29
33
|
end
|
30
34
|
|
35
|
+
def trim_destination(trimmed_url, options = {})
|
36
|
+
raise ArgumentError.new(":trimmed url is required") if trimmed_url.nil?
|
37
|
+
raise ArgumentError.new(":username and password are required") if (username.nil? || password.nil?)
|
38
|
+
response = @client.get_content(create_url("trim_destination", {:trimpath => trimmed_url, :username => username, :password => password}))
|
39
|
+
data = JSON.parse(response)
|
40
|
+
unless data["status"]["result"] == "OK"
|
41
|
+
raise TrimError.new(data["status"]["code"],data["status"]["message"])
|
42
|
+
else
|
43
|
+
data["destination"]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
31
47
|
private
|
32
48
|
|
33
|
-
def create_url(
|
34
|
-
base_url = "#{API_URL}
|
35
|
-
|
36
|
-
base_url +
|
49
|
+
def create_url(method, options = {})
|
50
|
+
base_url = "#{API_URL}#{method}.json?"
|
51
|
+
options = options.map { |k,v| "%s=%s" % [CGI.escape(k.to_s), CGI.escape(v.to_s)] }.join("&")
|
52
|
+
base_url + options
|
37
53
|
end
|
38
54
|
|
39
55
|
end
|
data/lib/trim_api.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trim-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: "0.1"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robin Fisher
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-10-
|
12
|
+
date: 2009-10-23 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|