Sha256: b75558569802e88377937e750aa8ae6ddfc3906d0dd40c47f617b4138e500f55

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

require 'yaml'
require 'fileutils'

require_relative 'socialcast/command_line/ldap_connector'
require_relative 'socialcast/command_line/provision'
require_relative 'socialcast/command_line/authenticate'
require_relative 'socialcast/command_line/message'
require_relative 'socialcast/command_line/cli'
require_relative 'socialcast/command_line/version'

module Socialcast
  module CommandLine
    def self.config_dir
      config_dir = File.expand_path '~/.socialcast'
      FileUtils.mkdir config_dir, :mode => 0700 unless File.exist?(config_dir)
      config_dir
    end

    def self.credentials_file
      ENV['SC_CREDENTIALS_FILE'] || File.join(config_dir, 'credentials.yml')
    end

    def self.credentials
      fail 'Unknown Socialcast credentials.  Run `socialcast authenticate` to initialize' unless File.exist?(credentials_file)
      YAML.load_file(credentials_file)
    end

    def self.credentials=(options)
      File.open(credentials_file, "a+") do |f|
        existing_content = YAML.load(f.read) || {}
        f.truncate(0)
        f.write(existing_content.merge(options).to_yaml)
      end
      File.chmod 0600, credentials_file
    end

    # configure restclient for api call
    def self.resource_for_path(path, options = {}, debug = true)
      RestClient.log = Logger.new(STDOUT) if debug
      RestClient.proxy = credentials[:proxy] if credentials[:proxy]
      url = ['https://', credentials[:domain], path].join
      RestClient::Resource.new url, options.merge(authentication(options))
    end

    def self.authentication(options)
      if options[:external_system]
        { :headers => { :Authorization => "SocialcastApiClient #{credentials[:api_client_identifier]}:#{credentials[:api_client_secret]}" } }
      else
        { :user => credentials[:user], :password => credentials[:password] }
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
socialcast-1.3.7 lib/socialcast.rb