Class: Simperium::Auth
- Inherits:
-
Object
- Object
- Simperium::Auth
- Defined in:
- lib/simperium.rb
Instance Method Summary collapse
- #_auth_header ⇒ Object
- #_request(url, data = nil, headers = nil, method = nil) ⇒ Object
- #authorize(username, password) ⇒ Object
- #create(username, password) ⇒ Object
-
#initialize(appname, api_key, host = nil, scheme = 'https') ⇒ Auth
constructor
A new instance of Auth.
Constructor Details
#initialize(appname, api_key, host = nil, scheme = 'https') ⇒ Auth
Returns a new instance of Auth.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/simperium.rb', line 14 def initialize(appname, api_key, host=nil,scheme='https') if host == nil host = ENV['SIMPERIUM_AUTHHOST'] || 'auth.simperium.com' end @appname = appname @api_key = api_key @host = host @scheme = scheme end |
Instance Method Details
#_auth_header ⇒ Object
25 26 27 |
# File 'lib/simperium.rb', line 25 def _auth_header return {"X-Simperium-API-Key" => "#{@api_key}"} end |
#_request(url, data = nil, headers = nil, method = nil) ⇒ Object
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 |
# File 'lib/simperium.rb', line 29 def _request(url, data=nil, headers=nil, method=nil) url = "#{@scheme}://#{@host}/1/#{url}" opts = {:url => url, :method => :post, :open_timeout => 30, :timeout => 80} if data opts = opts.merge({:payload => data}) end if headers.nil? headers = {} end opts = opts.merge({:headers => headers}) if method opts = opts.merge({:method => method}) end begin response = RestClient::Request.execute(opts) rescue SocketError => e ErrorHandling.handle_restclient_error(e) rescue NoMethodError => e if e. =~ /\WRequestFailed\W/ e = StandardError.new('Unexpected HTTP response code') ErrorHandling.handle_restclient_error(e) else raise end rescue RestClient::ExceptionWithResponse => e if rcode = e.http_code and rbody = e.http_body ErrorHandling.handle_api_error(rcode, rbody) else ErrorHandling.handle_restclient_error(e) end rescue RestClient::Exception, Errno::ECONNREFUSED => e ErrorHandling.handle_restclient_error(e) end return response end |
#authorize(username, password) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/simperium.rb', line 83 def (username, password) data = { 'username' => username, 'password' => password } response = self._request(@appname+'/authorize/', data, headers=_auth_header()) return JSON.load(response.body)['access_token'] end |
#create(username, password) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/simperium.rb', line 73 def create(username, password) data = { 'client_id' => @api_key, 'username' => username, 'password'=> password } response = self._request(@appname+'/create/', data) return JSON.load(response.body)['access_token'] end |