Class: Ubiquitously::Command::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ubiquitously/commands/base.rb

Direct Known Subclasses

Post, User

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Base

Returns a new instance of Base.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ubiquitously/commands/base.rb', line 94

def initialize(args)
  configure(main_folder)
  self.services = []
  self.attributes = {}
  show if args.blank?
  self.services << args.shift while args.length > 0 && args.first !~ /^-/
  self.services << attributes.delete(:services) if attributes.has_key?(:services)
  self.attributes[:title] = self.services.pop unless Ubiquitously.include?(self.services.last)
  self.attributes = parse_options(args, attributes)
  
  self.attributes.each do |key, value|
    self.send("#{key.to_s}=", value) if self.respond_to?(key)
  end 
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



10
11
12
# File 'lib/ubiquitously/commands/base.rb', line 10

def attributes
  @attributes
end

#servicesObject

Returns the value of attribute services.



10
11
12
# File 'lib/ubiquitously/commands/base.rb', line 10

def services
  @services
end

Class Method Details

.run(args) ⇒ Object



5
6
7
# File 'lib/ubiquitously/commands/base.rb', line 5

def run(args)
  new(args).run
end

Instance Method Details

#configure(folder) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ubiquitously/commands/base.rb', line 16

def configure(folder)
  Dir.mkdir(folder) unless File.exists?(folder)
  secrets_path     = File.join(folder, "secrets.yml")
  tokens_path      = File.join(folder, "tokens.yml")
  
  # usernames and passwords for post services
  unless File.exists?(secrets_path)
    secrets = "# #{secrets_path}, in your home directory\n"
    secrets << "# write your username and passwords for the different services\n"
    secrets << "# as necessary.\n\n"
    Ubiquitously.services.each do |service|
      secrets << "#{service}:\n"
      secrets << "  key: your_username\n"
      secrets << "  secret: your_password\n"
    end
    write(secrets_path, secrets)
    system("open", secrets_path)
    puts "Please configure your service username and passwords and rerun your command."
    exit
  end
  
  Ubiquitously.configure(secrets_path)
  
  # oauth keys and secrets from services
  unless File.exists?(tokens_path)
    tokens = "# #{tokens_path}, in your home directory\n"
    tokens << "# Get app key/secrets from the oauth providers below\n"
    tokens << "# and fill them out as desired.\n\n"
    tokens << "services:\n"
    Passport.services.each do |service|
      tokens << "  #{service}:\n"
      tokens << "    key: app_key\n"
      tokens << "    secrets: app_secret\n"
    end
    write(tokens_path, tokens)
    system("open", tokens_path)
    puts "Please configure your oauth keys and secrets and rerun your command."
    exit
  end
  
  Passport.configure(tokens_path)
end

#main_folderObject



59
60
61
# File 'lib/ubiquitously/commands/base.rb', line 59

def main_folder
  File.expand_path("~/.u.me")
end

#parse_options(attributes) ⇒ Object



109
110
111
# File 'lib/ubiquitously/commands/base.rb', line 109

def parse_options(attributes)
  
end

#runObject



113
114
115
# File 'lib/ubiquitously/commands/base.rb', line 113

def run
  
end

#showObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ubiquitously/commands/base.rb', line 63

def show
  tmp = File.join(main_folder, "post.yml")
  if File.exists?(tmp)
    post = IO.read(tmp)
  else
    post = ""
    post << "services: \n"
    post << "url: \n"
    post << "title: \n"
    post << "description: \n"
    post << "tags: \n"
    write(tmp, post)
  end
  system("open", tmp)
  require 'timeout'
  begin
    Timeout::timeout(30) {
      puts "Will timeout in 30 seconds. Press Enter when you're ready."
      STDIN.gets.chomp
      self.attributes = YAML.load_file(tmp).symbolize_keys
      #File.delete(tmp) if File.exists?(tmp)
      # Something that should be interrupted if it takes too much time...
    }
  rescue Exception => e
    puts e.inspect
    puts "Resubmit post with same command.  Finished process executing to clean things up."
    exit
  end
  
end

#write(to, content = "") ⇒ Object



12
13
14
# File 'lib/ubiquitously/commands/base.rb', line 12

def write(to, content = "")
  File.open(to, "w+") { |file| file.puts content }
end