Class: WssAgent::GemSha1

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ GemSha1

Returns a new instance of GemSha1.



7
8
9
10
# File 'lib/wss_agent/gem_sha1.rb', line 7

def initialize(spec)
  @spec = spec
  check_version! unless @spec.version > Gem::Version.new('0')
end

Instance Attribute Details

#specObject (readonly)

Returns the value of attribute spec.



5
6
7
# File 'lib/wss_agent/gem_sha1.rb', line 5

def spec
  @spec
end

Instance Method Details

#check_version!Object

check version if version isn’t found get latest version



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/wss_agent/gem_sha1.rb', line 15

def check_version!
  conn = Faraday.new(url: 'https://rubygems.org') do |h|
    h.headers[:content_type] = 'application/x-www-form-urlencoded'
    h.request :url_encoded
    h.adapter :excon
  end
  response = conn.get("/api/v1/versions/#{spec.name}.json")
  versions = MultiJson.load(response.body)
  unless versions.detect { |j| j['number'] == spec.version }
    spec.version = versions.first['number']
  end
rescue

end

#remote_file(retry_request = false) ⇒ Object

download gem from rubygems



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/wss_agent/gem_sha1.rb', line 55

def remote_file(retry_request = false)
  response = Net::HTTP.get_response(remote_file_url)

  case response.code
  when '200' # ok
    Digest::SHA1.hexdigest(response.body)

  when '302' # redirect
    response = Net::HTTP.get_response(URI(response['location']))
    return Digest::SHA1.hexdigest(response.body) if response.code == '200'
  else # gem isn't found
    ''
  end

rescue Timeout::Error
  retry_request ? nil : remote_file(true)
end

#remote_file_urlObject



49
50
51
# File 'lib/wss_agent/gem_sha1.rb', line 49

def remote_file_url
  URI("http://rubygems.org/gems/#{spec.file_name}")
end

#sha1Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/wss_agent/gem_sha1.rb', line 30

def sha1
  case
  when spec.source.is_a?(Bundler::Source::Rubygems)
    path = spec.source.send(:cached_gem, spec).to_s
    Digest::SHA1.hexdigest(File.binread(path))
  when spec.source.is_a?(Bundler::Source::Git)
  # ???
  when spec.source.is_a?(Bundler::Source::Path)
  # ????
  when spec.source.nil?
    remote_file
  end

rescue => ex
  WssAgent.logger.debug "#{ex.message}"
  WssAgent.logger.debug "#{spec}"
  remote_file
end