Skip to content

Instantly share code, notes, and snippets.

@maier-stefan
Created February 10, 2015 09:15
Show Gist options
  • Save maier-stefan/d5ccc63240f06be8265c to your computer and use it in GitHub Desktop.
Save maier-stefan/d5ccc63240f06be8265c to your computer and use it in GitHub Desktop.
API Class how to store a token for several instances
class API
include HTTParty
def initialize(options = {})
@auth = {
:username => options[:username],
:password => options[:password]
}
@token = options[:token]
end
def create_token
endpoint = "https://security.example.com/login"
response = self.class.get endpoint, {
:basic_auth => @auth
}
@token = response.parsed_response["token"]
end
def get_data
header_params = {'auth-token' => "#{@token}", 'Content-Type' => 'application/json' }
body = {name: name, url: url, data: data}
endpoint = "https://security.example.com/data/..."
response = self.class.post(endpoint, headers: header_params, body: body.to_json)
end
end
instance1 = API.new(username, password)
instance1.create_token
instance1.get_data
instance2 = API.new(username, password)
instance2.create_token
instance2.get_data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment