Class: Twitterpunch::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/twitterpunch/configuration.rb

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Configuration

Returns a new instance of Configuration.



8
9
10
11
# File 'lib/twitterpunch/configuration.rb', line 8

def initialize(file)
  @configfile = file
  @config     = YAML.load_file(@configfile) rescue defaults
end

Instance Method Details

#authorizeObject



13
14
15
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
# File 'lib/twitterpunch/configuration.rb', line 13

def authorize
  if @config.include? :twitter and @config[:twitter].include? :access_token_secret
    puts "You already have Twitter authorization."
    print "Would you like to re-authorize [y/N]? "
    return unless STDIN.gets.strip.downcase == 'y'
  end

  @config[:twitter]                   ||= {}
  @config[:twitter][:consumer_key]    ||= Twitterpunch::API_KEY
  @config[:twitter][:consumer_secret] ||= Twitterpunch::API_SECRET

  consumer = OAuth::Consumer.new(
    @config[:twitter][:consumer_key],
    @config[:twitter][:consumer_secret],
    {
      :site   => 'https://api.twitter.com/',
      :scheme => :header,
    })

  request_token = consumer.get_request_token

  puts 'Please authorize Twitterpunch to post to your Twitter account.'
  puts "Visit #{request_token.authorize_url} in your browser."
  # if we're on a Mac, open the page automatically
  system("open #{request_token.authorize_url} 2>/dev/null")

  print "Please enter the PIN you are given: "
  pin = STDIN.readline.chomp

  access_token = request_token.get_access_token(:oauth_verifier => pin)
  @config[:twitter][:access_token]        = access_token.token
  @config[:twitter][:access_token_secret] = access_token.secret
end

#configureObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/twitterpunch/configuration.rb', line 47

def configure
  puts "Existing Twitter authorization will not be altered." if @config.include? :twitter
  print "Would you like to save default configuration values [y/N]? "
  return unless STDIN.gets.strip.downcase == 'y'
  @config.merge! defaults

  # This is not in defaults so it doesn't take precedence
  @config[:twitter]                   ||= {}
  @config[:twitter][:consumer_key]    ||= Twitterpunch::API_KEY
  @config[:twitter][:consumer_secret] ||= Twitterpunch::API_SECRET

  puts "Please edit #{@configfile} to configure."
  puts 'If you have your own Twitter consumer key/secret, you may replace'
  puts 'the defaults before running `twitterpunch --authorize`.'
end

#defaultsObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/twitterpunch/configuration.rb', line 71

def defaults
  puts "Generating default configuration options..."
  {
    :messages => [
      "Hello there",
      "I'm a posting fool",
      "minimally viable product"
    ],
    :viewer    => {
      :count => 5,
    },
    :hashtag   => "Twitterpunch",
    :photodir  => "~/Pictures/twitterpunch/",
    :logfile   => '~/.twitterpunch/activity.log',
  }
end

#saveObject



63
64
65
66
67
68
69
# File 'lib/twitterpunch/configuration.rb', line 63

def save
  puts @config.to_yaml
  puts
  print "Save configuration [y/N]? "
  return unless STDIN.gets.strip.downcase == 'y'
  File.open(@configfile, 'w') {|f| f.write(@config.to_yaml) }
end