Class: Spitball::Remote

Inherits:
Object
  • Object
show all
Includes:
ClientCommon
Defined in:
lib/spitball/remote.rb

Instance Method Summary collapse

Methods included from ClientCommon

#copy_to

Constructor Details

#initialize(gemfile, gemfile_lock, opts = {}) ⇒ Remote

Returns a new instance of Remote.



9
10
11
12
13
14
15
16
17
# File 'lib/spitball/remote.rb', line 9

def initialize(gemfile, gemfile_lock, opts = {})
  @gemfile, @gemfile_lock = gemfile, gemfile_lock
  @host      = opts[:host]
  @port      = opts[:port]
  @without   = (opts[:without] || []).map{|w| w.to_sym}
  @cache_dir = ENV['SPITBALL_CACHE'] || "/tmp/spitball-#{ENV['USER']}/client"
  FileUtils.mkdir_p(@cache_dir)
  use_cache_file
end

Instance Method Details

#cache!(sync = true) ⇒ Object

ignore sync



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/spitball/remote.rb', line 34

def cache!(sync = true) # ignore sync
  return if cached?

  url = URI.parse("http://#{@host}:#{@port}/create")
  req = Net::HTTP::Post.new(url.path)
  req.form_data = {'gemfile' => @gemfile, 'gemfile_lock' => @gemfile_lock}
  req.add_field Spitball::PROTOCOL_HEADER, Spitball::PROTOCOL_VERSION
  req.add_field Spitball::WITHOUT_HEADER, @without.join(',')

  res = Net::HTTP.new(url.host, url.port).start do |http|
    http.read_timeout = 3000
    http.request(req) {|r| puts r.read_body }
  end

  case res.code
  when '201', '202' # Created, Accepted
    @tarball_url = res['Location']
  when '403'
  else
    raise Spitball::ServerFailure, "Expected 2xx response code. Got #{res.code}."
  end
rescue URI::InvalidURIError => e
  raise Spitball::ClientError, e.message
end

#cache_fileObject



25
26
27
28
# File 'lib/spitball/remote.rb', line 25

def cache_file
  hash = ::Digest::MD5.hexdigest(([@host, @port, @gemfile, @gemfile_lock, Spitball::PROTOCOL_VERSION] + @without).join('/'))
  File.join(@cache_dir, hash)
end

#cached?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/spitball/remote.rb', line 30

def cached?
  !!@tarball_url
end

#use_cache_fileObject



19
20
21
22
23
# File 'lib/spitball/remote.rb', line 19

def use_cache_file
  if File.exist?(cache_file)
    @tarball_url = cache_file
  end
end