Class: Thingiverse::Connection

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/thingiverse/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id = nil, client_secret = nil, code = nil) ⇒ Connection

Returns a new instance of Connection.



7
8
9
10
11
12
13
14
15
# File 'lib/thingiverse/connection.rb', line 7

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

Instance Attribute Details

#access_tokenObject

debug_output $stderr



5
6
7
# File 'lib/thingiverse/connection.rb', line 5

def access_token
  @access_token
end

#auth_urlObject

debug_output $stderr



5
6
7
# File 'lib/thingiverse/connection.rb', line 5

def auth_url
  @auth_url
end

#base_urlObject

debug_output $stderr



5
6
7
# File 'lib/thingiverse/connection.rb', line 5

def base_url
  @base_url
end

#client_idObject

debug_output $stderr



5
6
7
# File 'lib/thingiverse/connection.rb', line 5

def client_id
  @client_id
end

#client_secretObject

debug_output $stderr



5
6
7
# File 'lib/thingiverse/connection.rb', line 5

def client_secret
  @client_secret
end

#codeObject

debug_output $stderr



5
6
7
# File 'lib/thingiverse/connection.rb', line 5

def code
  @code
end

Instance Method Details

#get_tokenObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/thingiverse/connection.rb', line 35

def get_token
  auth_response = self.class.post(@auth_url, :query => {:client_id => @client_id, :client_secret => @client_secret, :code => @code})

  raise "#{auth_response.code}: #{auth_response.body.inspect}" unless auth_response.success?

  response = CGI::parse(auth_response.parsed_response)

  @access_token = response['access_token'].to_s

  self.class.headers "Authorization" => "Bearer #{@access_token}"

  @access_token
end

#thingsObject



49
50
51
# File 'lib/thingiverse/connection.rb', line 49

def things
  Thingiverse::Things
end

#usersObject



53
54
55
# File 'lib/thingiverse/connection.rb', line 53

def users
  Thingiverse::Users
end