This repository was archived by the owner on Mar 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathconnection.rb
More file actions
61 lines (45 loc) · 1.43 KB
/
connection.rb
File metadata and controls
61 lines (45 loc) · 1.43 KB
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
module Thingiverse
class Connection
include HTTParty
# debug_output $stderr
attr_accessor :client_id, :client_secret, :code, :access_token, :auth_url, :base_url
def initialize(client_id = nil, client_secret = nil, code = nil)
@client_id = client_id
@client_secret = client_secret
@code = code
self.class.base_uri(self.base_url)
self.get_token if @client_id and @client_secret and @code
end
def auth_url
@auth_url || 'https://www.thingiverse.com/login/oauth/access_token'
end
def base_url=(url)
@base_url = url
self.class.base_uri(url)
end
def base_url
@base_url || 'https://api.thingiverse.com'
end
def access_token=(token)
@access_token = token
self.class.headers "Authorization" => "Bearer #{@access_token}"
end
def get_token
auth_response = self.class.post(auth_url, :query => {:client_id => @client_id, :client_secret => @client_secret, :code => @code})
raise ResponseError.new(auth_response) unless auth_response.success?
response = CGI::parse(auth_response.parsed_response)
@access_token = response['access_token'].first.to_s
self.class.headers "Authorization" => "Bearer #{@access_token}"
@access_token
end
def things
Thingiverse::Things
end
def users
Thingiverse::Users
end
def tags
Thingiverse::Tags
end
end
end