Class: Socialcast::CommandLine::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/socialcast/command_line/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.



33
# File 'lib/socialcast/command_line/cli.rb', line 33

def initialize(*args); super; end

Instance Method Details

#authenticateObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/socialcast/command_line/cli.rb', line 49

def authenticate
  user = options[:user] || ask('Socialcast username: ')
  password = options[:password] || HighLine.new.ask("Socialcast password: ") { |q| q.echo = false }.to_s
  domain = options[:domain]

  url = ['https://', domain, '/api/authentication'].join
  say "Authenticating #{user} to #{url}"
  params = { :email => user, :password => password }
  RestClient.log = Logger.new(STDOUT) if options[:trace]
  RestClient.proxy = options[:proxy] if options[:proxy]
  resource = RestClient::Resource.new url
  response = resource.post params, :accept => :json
  say "API response: #{response.body.to_s}" if options[:trace]
  communities = JSON.parse(response.body.to_s)['communities']
  domain = communities.detect {|c| c['domain'] == domain} ? domain : communities.first['domain']

  Socialcast::CommandLine.credentials = {:user => user, :password => password, :domain => domain, :proxy => options[:proxy]}
  say "Authentication successful for #{domain}"
end

#infoObject



37
38
39
40
41
# File 'lib/socialcast/command_line/cli.rb', line 37

def info
  if options["version"]
    say "Socialcast Command Line #{Socialcast::CommandLine::VERSION}"
  end
end

#provisionObject



108
109
110
111
112
113
114
115
116
# File 'lib/socialcast/command_line/cli.rb', line 108

def provision
  config = ldap_config options
  load_plugins options

  Socialcast::CommandLine::Provision.new(config, options).provision

rescue Socialcast::CommandLine::Provision::ProvisionError => e
  Kernel.abort e.message
end

#share(message = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/socialcast/command_line/cli.rb', line 74

def share(message = nil)
  message ||= $stdin.read_nonblock(100_000) rescue nil

  attachment_ids = []
  options[:attachments].each do |path|
    Dir[File.expand_path(path)].each do |attachment|
      say "Uploading attachment #{attachment}..."
      uploader = Socialcast::CommandLine.resource_for_path '/api/attachments', {}, options[:trace]
      uploader.post({:attachment => File.new(attachment)}, {:accept => :json}) do |response, request, result|
        if response.code == 201
          attachment_ids << JSON.parse(response.body)['attachment']['id']
        else
          say "Error uploading attachment: #{response.body}"
        end
      end
    end
  end

  ActiveResource::Base.logger = Logger.new(STDOUT) if options[:trace]
  Socialcast::CommandLine::Message.configure_from_credentials
  Socialcast::CommandLine::Message.create :body => message, :url => options[:url], :message_type => options[:message_type], :attachment_ids => attachment_ids, :group_id => options[:group_id]

  say "Message has been shared"
end

#sync_photosObject



120
121
122
123
124
# File 'lib/socialcast/command_line/cli.rb', line 120

def sync_photos
  config = ldap_config options

  Socialcast::CommandLine::Provision.new(config).sync_photos
end