require 'totter/transport' module Totter # API client for interacting with the Seesaw API class Client Dir[File.expand_path('../client/*.rb', __FILE__)].each { |f| require f } include Avatars include Choices include Comments include Configuration include Decisions include Slugs include Stickers include Timelines include Users include Votes attr_reader :access_token attr_reader :api_scheme attr_reader :api_host attr_reader :api_version attr_reader :transport @@stubbed = false DEFAULTS = { :api_scheme => 'https', :api_host => 'api.seesaw.co', :api_version => '1', :transport => :http } def self.options @options ||= Hashie::Mash.new(DEFAULTS.dup) end def self.options=(val) @options = val end def self.configure yield options end # Initialize a new client. # # @param options [Hash] optionally specify `:access_token`, `:api_scheme`, `:api_host`, `:api_version`, `:client_token`, or `:transport`. def initialize(options = {}) options = { :access_token => options } if options.is_a? String options = self.class.options.merge(options) @access_token = options[:access_token] if options[:access_token] @api_scheme = options[:api_scheme] @api_host = options[:api_host] @api_version = options[:api_version] @client_token = options[:client_token] if options[:client_token] @transport = options[:transport] # Include transport transport_module = Totter::Transport::TRANSPORT_MAP[@transport] raise 'Invalid transport' unless transport_module self.extend transport_module end # API base URL. # # @return [String] API base URL def base_url "#{@api_scheme}://#{@api_host}/v#{@api_version}/" end # Is the client has an access token. # # @return [Boolean] true if it is using one and false if it is not def authenticated? @access_token != nil and @access_token.length > 0 end # Is the client using SSL. # # @return [Boolean] true if it is using SSL and false if it is not def ssl? @api_scheme == 'https' end # Turns on stubbing. When called, all HTTP API calls will return an empty response def self.stub! @@stubbed = true end # Turns off stubing. Once called, HTTP requests will be sent as normal def self.unstub! @@stubbed = false end # Returns the current status of HTTP stubbing def self.stubbed? @@stubbed end end end