Class: Depends::RestClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ RestClient

Returns a new instance of RestClient.



11
12
13
14
15
16
# File 'lib/depends/rest_client.rb', line 11

def initialize(source)
  @source_url = source
  cache_path = File.join(Depends::Config.cache,'rest',URI.parse(source.to_s).host )
  Depends::Log.debug ":cache_path => #{cache_path}"
  @cache = ActiveSupport::Cache::FileStore.new(cache_path, :expires_in => 1.day)
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



8
9
10
# File 'lib/depends/rest_client.rb', line 8

def api_key
  @api_key
end

#cacheObject (readonly)

Returns the value of attribute cache.



9
10
11
# File 'lib/depends/rest_client.rb', line 9

def cache
  @cache
end

#source_urlObject

Returns the value of attribute source_url.



7
8
9
# File 'lib/depends/rest_client.rb', line 7

def source_url
  @source_url
end

Instance Method Details

#create_depend(name, description) ⇒ Object



26
27
28
29
# File 'lib/depends/rest_client.rb', line 26

def create_depend(name,description)
  depend = Nestful.post "#{source_url}/depends",   :headers => headers, :format => :json, :params => {:name => name, :description => description}
  depend
end

#create_version(name, options = {}) ⇒ Object



22
23
24
# File 'lib/depends/rest_client.rb', line 22

def create_version(name,options = {})
   Nestful.post "#{source_url}/depends/#{name}/version", :headers => headers, :format => :multipart, :timeout => 3600, :params => {:number => options[:number] , :file => File.open(options[:file]), :sha1 => options[:sha1] }
end

#download_depend_version(name, version) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/depends/rest_client.rb', line 43

def download_depend_version(name,version)
  dest_file = File.join(Depends::Config.cache, "#{name}-#{version}.zip")
  Log.info "#{source_url}/depends/#{name}/version/#{version}/download"

  request = Nestful.get "#{source_url}/depends/#{name}/version/#{version}/download.url",  :headers => headers
  uri = URI.parse(request)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  http.request_get("#{uri.path}#{'?' + uri.query if uri.query}") do |response|
      temp_file = Tempfile.new("download")
      temp_file.binmode

      size = 0
      progress = 0
      total = response.header["Content-Length"].to_i

      response.read_body do |chunk|
        temp_file << chunk
        size += chunk.size
        new_progress = (size * 100) / total
        unless new_progress == progress
          Log.info "\rDownloading %s (%3d%%) " % [name, new_progress]
        end
        progress = new_progress
      end

      temp_file.close
      File.unlink dest_file if File.exists?(dest_file)
      FileUtils.mkdir_p File.dirname(dest_file)
      FileUtils.mv temp_file.path, dest_file, :force => true
  end
  
  dest_file
end

#get_dep_version(name, version) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/depends/rest_client.rb', line 95

def get_dep_version(name,version)
  Depends::Log.debug "[get] :ver => #{version}"
  uri = "#{source_url}/depends/#{name}/version/#{version}"
  #depend = cache.read(uri)
  #if depend.nil?
    depend = Nestful.get uri, :format => :json,  :headers => headers
    #cache.write(uri,depend.to_json)
  #else
  #  depend = ::MultiJson.load(depend)
  #end
  depend
end

#get_depend(name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/depends/rest_client.rb', line 31

def get_depend(name)
  uri = "#{source_url}/depends/#{name}"
  #depend = cache.read(uri)
  #if depend.nil?
    depend = Nestful.get uri, :format => :json,  :headers => headers
    #cache.write(uri,depend.to_json)
  #else
  #  depend = ::MultiJson.load(depend)
  #end
  depend
end

#get_latest_version(name) ⇒ Object



90
91
92
# File 'lib/depends/rest_client.rb', line 90

def get_latest_version(name)
  JSON.parse( Nestful.get "#{source_url}/depends/#{name}/version/latest",  :headers => headers )
end

#headersObject



18
19
20
# File 'lib/depends/rest_client.rb', line 18

def headers
  {'key' => api_key}
end

#list(options = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/depends/rest_client.rb', line 79

def list(options = {})
  params = {}

  if params[:depends_list]
    Nestful.post "#{source_url}/depends/search", :params => params, :format => :json,  :headers => headers
  else
    Nestful.get "#{source_url}/depends", :format => :json,  :headers => headers
  end

end