Class: Tattle::Collector

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

Constant Summary collapse

KEY_FILE =
File.expand_path('~/.tattle/key')
REPORTING_URL =
if ENV['LOCAL']
  URI.parse('http://localhost:3000/reports/create')
else
  URI.parse('http://tattle.rubygarden.org/reports/create')
end
DESIRED_CONFIG_KEYS =
['target', 
'LIBRUBY', 
'prefix', 
'host_cpu', 
'arch', 
'host_os', 
'build',
'LIBRUBY_SO',
'ruby_install_name',
'target_cpu',
'host_vendor',
'SHELL']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.postObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tattle.rb', line 27

def self.post
  collector = self.new
  Net::HTTP.new(REPORTING_URL.host, REPORTING_URL.port).start do |http|
    params = collector.system_data.to_params + ($DEBUG ? '&debug=1' : '')
    response = http.post(REPORTING_URL.path, params)
    if $DEBUG
      p response 
      p response.body 
    end
    if !collector.key && (returned_key = response.body) =~ /^\s*[[:alnum:]]{64}\s*$/
      collector.key = returned_key.strip
    end
  end
end

.reportObject



42
43
44
# File 'lib/tattle.rb', line 42

def self.report
  self.new.report
end

Instance Method Details

#keyObject

Read key (if available)



61
62
63
64
65
66
67
# File 'lib/tattle.rb', line 61

def key
  @key ||= begin
    FileUtils.mkdir(File.dirname(KEY_FILE)) rescue nil
    value = File.read(KEY_FILE) rescue nil
    (value.nil? || value.empty?) ? nil : value
  end
end

#key=(value) ⇒ Object

Write key



70
71
72
73
74
75
76
77
# File 'lib/tattle.rb', line 70

def key=(value)
  @key ||= begin
    FileUtils.mkdir_p File.dirname(KEY_FILE)
    File.open(KEY_FILE, 'w') do |f|
      f << value
    end
  end
end

#report(io = STDOUT) ⇒ Object



79
80
81
82
83
84
# File 'lib/tattle.rb', line 79

def report(io = STDOUT)
  system_data.each do |key, value|
    io.puts "#{key}, #{value}"
  end
  system_data
end

#system_dataObject



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tattle.rb', line 46

def system_data
   data = DESIRED_CONFIG_KEYS.inject({}) do |hash, key|
     hash[key] = Config::CONFIG[key]
     hash
   end
   data['report_time'] = Time.now.to_s
   data['rubygems_version'] = Gem::RubyGemsVersion
   data['ruby_version'] = RUBY_VERSION
   data['key'] = key
   data.extend(StringExtensions)
   $stderr.puts data.inspect if $DEBUG
   data
end