/
instapaper_full.rb
140 lines (116 loc) · 3.58 KB
/
instapaper_full.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
require 'errors'
require 'json'
require 'faraday'
require 'faraday_middleware'
module InstapaperFull
class API
attr_accessor :options
def initialize(options = {})
@options = options
end
def connection(options = {})
options.merge!({
:proxy => @options[:proxy],
:ssl => {:verify => false},
:url => "https://www.instapaper.com/api/1/"
})
oauth_params = {
:consumer_key => @options[:consumer_key],
:consumer_secret => @options[:consumer_secret]
}
if authenticated?
oauth_params[:token] = @options[:oauth_token]
oauth_params[:token_secret] = @options[:oauth_token_secret]
end
Faraday.new(options) do |builder|
builder.use Faraday::Request::Multipart
builder.use Faraday::Request::OAuth, oauth_params
builder.use Faraday::Request::UrlEncoded
builder.adapter Faraday.default_adapter
end
end
def authenticated?
@options.has_key? :oauth_token and @options.has_key? :oauth_token_secret
end
def authenticate(username, password)
@options.delete(:oauth_token)
@options.delete(:oauth_token_secret)
result = connection.post 'oauth/access_token' do |r|
r.body = { :x_auth_username => username, :x_auth_password => password, :x_auth_mode => "client_auth" }
end
if result.status == 200
data = CGI.parse(result.body)
if data.has_key? 'oauth_token_secret'
@options[:oauth_token] = data['oauth_token'][0]
@options[:oauth_token_secret] = data['oauth_token_secret'][0]
verify_credentials.each { |k,v|
@options[k.to_sym] = v if k != 'type'
}
end
return true
else
return false
end
end
def call(method, params = {}, connection_options = {})
result = connection(connection_options).post(method) do |r|
r.body = params unless params.empty?
end
if result.headers['content-type'] == 'application/json'
JSON.parse(result.body).tap do |d|
if error = d.find { |e| e['type'] == 'error' }
raise InstapaperFull::API::Error.new(error['error_code'], error['message'])
end
end
else
raise InstapaperFull::API::Error.new(-1, result.body) if result.status != 200
result.body
end
end
def verify_credentials
call('account/verify_credentials')[0]
end
def bookmarks_list(params = {})
call('bookmarks/list', params)
end
def bookmarks_update_read_progress(params = {})
call('bookmarks/update_read_progress', params)
end
def bookmarks_add(params = {})
call('bookmarks/add', params)
end
def bookmarks_delete(params = {})
call('bookmarks/delete', params)
end
def bookmarks_star(params = {})
call('bookmarks/star', params)
end
def bookmarks_unstar(params = {})
call('bookmarks/unstar', params)
end
def bookmarks_archive(params = {})
call('bookmarks/archive', params)
end
def bookmarks_unarchive(params = {})
call('bookmarks/unarchive', params)
end
def bookmarks_move(params = {})
call('bookmarks/move', params)
end
def bookmarks_get_text(params = {})
call('bookmarks/get_text', params)
end
def folders_list
call('folders/list')
end
def folders_add(params = {})
call('folders/add', params)
end
def folders_delete(params = {})
call('folders/delete', params)
end
def folders_set_order(params = {})
call('folders/set_order', params)
end
end
end