require 'omniauth' require 'omniauth-oauth2' require 'faraday_middleware' require 'centro/errors' require 'centro/api/base' module Centro class Client class << self def api_version return @api_version if defined?(@api_version) @api_version = 'v1' end def api_version=(api_version) @api_version = api_version end def auth_host return @auth_host if defined?(@auth_host) @auth_host = 'accounts.centro.net' end def auth_host=(auth_host) @auth_host = auth_host end def finance_host return @finance_host if defined?(@finance_host) @finance_host = 'finance.transis.com' end def finance_host=(finance_host) @finance_host = finance_host end def mms_host return @mms_host if defined?(@mms_host) @mms_host = 'the.centro.net' end def mms_host=(mms_host) @mms_host = mms_host end def planner_host return @planner_host if defined?(@planner_host) @planner_host = 'planner.centro.net' end def planner_host=(planner_host) @planner_host = planner_host end def ssl_enabled? return @ssl_enabled if defined?(@ssl_enabled) @ssl_enabled = true end def ssl_enabled=(ssl_enabled) @ssl_enabled = ssl_enabled end def auth_url url_from_host(auth_host) end def finance_url url_from_host(finance_host) end def mms_url url_from_host(mms_host) end def planner_url url_from_host(planner_host) end def url_from_host(host) (ssl_enabled? ? 'https://' : 'http://') + host + "/api/#{self.api_version}" end end attr_accessor :client_id, :client_secret, :access_token, :access_token_expires_at def initialize(opts={}) @client_id = opts.delete(:client_id) if opts[:client_id] @client_secret = opts.delete(:client_secret) if opts[:client_secret] @access_token = opts.delete(:access_token) if opts[:access_token] @basic_auth_username = opts.delete(:basic_auth_username) if opts[:basic_auth_username] @basic_auth_password = opts.delete(:basic_auth_password) if opts[:basic_auth_password] end def fetch(url) connection(url).get.body end # Data will be JSON-ified if it's not already in string-form def put(url, data) connection(url).put("", data).body end def access_token_expired? access_token_expires_at.nil? || access_token_expires_at < Time.now end def retrieve_access_token(username, password) raise 'client_id and client_secret required' unless @client_id && @client_secret connection = Faraday.new(self.class.auth_url) do |conn| set_default_connection_options(conn) end response = connection.post('/oauth/token', :grant_type => 'password', :client_id => @client_id, :client_secret => @client_secret, :username => username, :password => password) self.access_token_expires_at = Time.now + response.body.expires_in self.access_token = response.body.access_token end def get_credentials auth_connection.get("me.json").body end def get_organizations(user_id=nil) options = {} options[:user_id] = user_id if user_id auth_connection.get("organizations.json", options).body end def get_members_of_organization(organization_id) auth_connection.get("organizations/#{organization_id}/members.json").body end ## # Creates a user in an organization. If the user already exists in AuthTransis they will # simply be added to the org (assuming the resource owner has rights), if they don't exist # they will be created and added to the organizations. # # Returns a list of the organizations that they were sucessfully added to. def create_user_in_organization(email, organization_id) org_ids = organization_id.respond_to?(:each) ? organization_id : [organization_id] successfuls = [] org_ids.each do |org_id| response = auth_connection.post("organizations/#{organization_id}/members.json", {:email_address => email}) response.success? && successfuls << response.body end successfuls end def create_user(email) response = auth_connection.post("members.json", {:email_address => email}) response.success? && response.body end def create_organization(org_name) response = auth_connection.post("organizations", {:name => org_name}) response.success? && response.body end private def set_default_connection_options(conn) conn.request :json conn.response :dates conn.response :mashify conn.response :json, :content_type => /\bjson$/ conn.response :xml, :content_type => /\bxml$/ conn.response :raise_error conn.adapter Faraday.default_adapter end def auth_connection raise 'access_token required' unless @access_token @auth_connection ||= connection(self.class.auth_url) end def finance_connection raise 'access_token required' unless @access_token @finance_connection ||= connection(self.class.finance_url) end def mms_connection raise 'access_token required' unless @access_token @mms_connection ||= connection(self.class.mms_url) end def planner_connection raise 'basic_auth_username required' unless @basic_auth_username raise 'basic_auth_password required' unless @basic_auth_password @planner_connection ||= Faraday.new(self.class.planner_url) do |conn| conn.basic_auth(@basic_auth_username, @basic_auth_password) set_default_connection_options(conn) end end def connection(url) raise 'access_token required' unless @access_token Faraday.new url do |conn| conn.request :oauth2, @access_token set_default_connection_options(conn) end end end end