class Thingdeck::Wrapper
Public Class Methods
new(app_id, secret)
click to toggle source
# File lib/thingdeck/wrapper.rb, line 3 def initialize(app_id, secret) @token = authenticate(app_id, secret) end
Public Instance Methods
collections()
click to toggle source
# File lib/thingdeck/wrapper.rb, line 11 def collections @collections ||= Collection.new(self) end
info()
click to toggle source
# File lib/thingdeck/wrapper.rb, line 23 def info perform_request { |c| c.get('/') } end
perform_request(need_auth = true) { |connection| ... }
click to toggle source
# File lib/thingdeck/wrapper.rb, line 27 def perform_request(need_auth = true) raise 'Not Authorized' if need_auth and not authorized? response = yield(connection) parse_body(response) end
search(name)
click to toggle source
# File lib/thingdeck/wrapper.rb, line 19 def search(name) perform_request { |c| c.get('/explore', q: name) } end
things()
click to toggle source
# File lib/thingdeck/wrapper.rb, line 15 def things @things ||= Thing.new(self) end
Private Instance Methods
authenticate(app_id, secret)
click to toggle source
# File lib/thingdeck/wrapper.rb, line 45 def authenticate(app_id, secret) response = Faraday.new(url: OAUTH_URL).post('/oauth/token', application_id: app_id, secret: secret) auth = parse_body(response) auth['token'] unless auth.nil? end
connection()
click to toggle source
# File lib/thingdeck/wrapper.rb, line 41 def connection @connection ||= Faraday.new(url: API_URL, headers: { 'Accept' => 'application/json; version=1', 'Authorization' => "Token #{@token}" }) end
parse_body(response)
click to toggle source
# File lib/thingdeck/wrapper.rb, line 34 def parse_body(response) body = response.env[:body].strip JSON[body] rescue nil end