Skip to content

Instantly share code, notes, and snippets.

@jacobbednarz
Created February 16, 2015 20:45
Show Gist options
  • Save jacobbednarz/6e714cbfc75995587b0b to your computer and use it in GitHub Desktop.
Save jacobbednarz/6e714cbfc75995587b0b to your computer and use it in GitHub Desktop.
module Example
module Api
class Client
attr_accessor :client
def initialize(options = {})
# Providing that we have both the username and the password, use it
# otherwise we will run through the available authentication sources to
# find our user details.
options[:username] ||= user_credentials[:username]
options[:password] ||= user_credentials[:password]
@client = build_connection(options)
response = @client.get 'sites.json'
fail InvalidUserCredentials, 'Invalid user credentials' if response.status == 401
end
def build_connection(options = {})
# Build our connection using a proxy and correct SSL options.
@client = Faraday.new(url: Example.cloud_api_endpoint, ssl: ssl_opts) do |c|
c.adapter Faraday.default_adapter
c.headers['User-Agent'] = "Example SDK (#{Example::VERSION})"
c.basic_auth(options[:username], options[:password])
c.proxy = proxy_opts if proxy?
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment