trunkly 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +4 -1
- data/README +11 -1
- data/lib/trunkly.rb +40 -20
- data/lib/trunkly/version.rb +1 -1
- metadata +5 -5
data/CHANGELOG
CHANGED
data/README
CHANGED
@@ -80,7 +80,17 @@ t.next_page #returns results from 11..20
|
|
80
80
|
t.prev_page #returns restult 1..10
|
81
81
|
|
82
82
|
|
83
|
-
5)
|
83
|
+
5) Timeline
|
84
|
+
|
85
|
+
To get the list of your friends link (called Timeline)
|
86
|
+
|
87
|
+
t = Trunkly::Trunkly.new('my_api_key_goes_here')
|
88
|
+
links = t.timeline #this retrieves 100 links
|
89
|
+
|
90
|
+
You can apply the usual filters and pagination parameters
|
91
|
+
|
92
|
+
|
93
|
+
6) Save a link
|
84
94
|
|
85
95
|
Saving a link is very simple
|
86
96
|
|
data/lib/trunkly.rb
CHANGED
@@ -11,30 +11,25 @@ module Trunkly
|
|
11
11
|
|
12
12
|
LINKS = "http://trunk.ly/api/v1/links/"
|
13
13
|
LINK = "http://trunk.ly/api/v1/link/"
|
14
|
+
TIMELINE = "http://trunk.ly/api/v1/timeline/"
|
14
15
|
|
15
16
|
def initialize(key = nil)
|
16
17
|
@api_key = key
|
17
18
|
@last_used_params = {}
|
19
|
+
@last_command = :links
|
18
20
|
@next_page, @prev_page = 0, 0
|
19
21
|
end
|
20
22
|
|
23
|
+
def timeline(params = {})
|
24
|
+
@last_used_params = params
|
25
|
+
@last_command = :timeline
|
26
|
+
retrieve_links_from(get_timeline(params))
|
27
|
+
end
|
28
|
+
|
21
29
|
def links(params = {})
|
22
|
-
@last_used_params = params
|
23
|
-
|
24
|
-
|
25
|
-
when Net::HTTPUnauthorized then
|
26
|
-
raise AuthorizationError, "Unauthorized: no or wrong api_key"
|
27
|
-
when Net::HTTPSuccess
|
28
|
-
links = []
|
29
|
-
result = JSON.parse(response.body)
|
30
|
-
@prev_page, @next_page = result['prev_page'] || 0, result['next_page'] || 0
|
31
|
-
result['links'].each do |it|
|
32
|
-
links << Link.new(it)
|
33
|
-
end
|
34
|
-
links
|
35
|
-
else
|
36
|
-
raise ApiError, "Exception trying to get links: "+response.body.to_s
|
37
|
-
end
|
30
|
+
@last_used_params = params
|
31
|
+
@last_command = :links
|
32
|
+
retrieve_links_from get_links(params)
|
38
33
|
end
|
39
34
|
|
40
35
|
def save(link = Link.new)
|
@@ -53,12 +48,12 @@ module Trunkly
|
|
53
48
|
|
54
49
|
def next_page
|
55
50
|
@last_used_params[:page] = @next_page
|
56
|
-
|
51
|
+
send @last_command.to_sym, @last_used_params
|
57
52
|
end
|
58
53
|
|
59
54
|
def prev_page
|
60
55
|
@last_used_params[:page] = @prev_page
|
61
|
-
|
56
|
+
send @last_command.to_sym, @last_used_params
|
62
57
|
end
|
63
58
|
alias :previous_page :prev_page
|
64
59
|
|
@@ -75,10 +70,36 @@ module Trunkly
|
|
75
70
|
|
76
71
|
private
|
77
72
|
|
73
|
+
def retrieve_links_from(url)
|
74
|
+
response = Net::HTTP.get_response(url)
|
75
|
+
case response
|
76
|
+
when Net::HTTPUnauthorized then
|
77
|
+
raise AuthorizationError, "Unauthorized: no or wrong api_key"
|
78
|
+
when Net::HTTPSuccess
|
79
|
+
extract_links_from response
|
80
|
+
else
|
81
|
+
raise ApiError, "Exception trying to get links: "+response.body.to_s
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def extract_links_from(response)
|
86
|
+
result = JSON.parse(response.body)
|
87
|
+
@prev_page, @next_page = result['prev_page'] || 0, result['next_page'] || 0
|
88
|
+
result['links'].map do |it|
|
89
|
+
Link.new(it)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
def get_timeline(params = {})
|
95
|
+
URI.parse(URI.escape(TIMELINE) + query_string(params))
|
96
|
+
end
|
97
|
+
|
98
|
+
|
78
99
|
def get_links(params = {})
|
79
100
|
userpart = params[:user] ? "#{params[:user]}/" : "";
|
80
101
|
URI.parse(URI.escape(LINKS + userpart + query_string(params)))
|
81
|
-
end
|
102
|
+
end
|
82
103
|
|
83
104
|
def query_string(params = {})
|
84
105
|
query_string = ""
|
@@ -86,7 +107,6 @@ module Trunkly
|
|
86
107
|
query_string += "&#{k.to_s}=#{v}" unless k == :user
|
87
108
|
end
|
88
109
|
query_string += "&api_key=#{@api_key}" if @api_key #adds api key if available
|
89
|
-
p query_string[0]
|
90
110
|
query_string[0] = '?' if query_string[0] == 38 #replace initial &
|
91
111
|
query_string
|
92
112
|
end
|
data/lib/trunkly/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trunkly
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Filippo Diotalevi
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-01-
|
18
|
+
date: 2011-01-13 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|