Class: Spider::AppServerClient

Inherits:
Object
  • Object
show all
Defined in:
lib/spiderfw/setup/app_server_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ AppServerClient

Returns a new instance of AppServerClient



11
12
13
# File 'lib/spiderfw/setup/app_server_client.rb', line 11

def initialize(url)
    @url = url
end

Instance Method Details

#fetch_app(app_id) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/spiderfw/setup/app_server_client.rb', line 56

def fetch_app(app_id)
    tmp = Tempfile.new("spider-app-archive")
    tmp.binmode
    res = http_get(@url+"/pack/#{app_id}")
    tmp << res
    tmp.flush
    tmp.path
end

#get_deps(app_ids, options = {}) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/spiderfw/setup/app_server_client.rb', line 48

def get_deps(app_ids, options={})
    app_ids = [app_ids] unless app_ids.is_a?(Array)
    url = "#{@url}/deps/#{app_ids.join('+')}.json"
    url += "?no_optional=true" if options[:no_optional]
    result = http_get(url)
    JSON.parse(result).map{ |app| Spider::App::AppSpec.parse_hash(app) }
end

#get_specs(app_ids) ⇒ Object



42
43
44
45
46
# File 'lib/spiderfw/setup/app_server_client.rb', line 42

def get_specs(app_ids)
    app_ids = [app_ids] unless app_ids.is_a?(Array)
    result = http_get(@url+"/list/#{app_ids.join('+')}.json")
    JSON.parse(result).map{ |app| Spider::App::AppSpec.parse_hash(app) }
end

#http_get(url) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/spiderfw/setup/app_server_client.rb', line 20

def http_get(url)
    uri = URI.parse(url)
    proxy = uri.find_proxy
    klass = nil
    if proxy
        proxy_user, proxy_pass = nil
        proxy_user, proxy_pass = proxy.userinfo.split(/:/) if proxy.userinfo
        klass = Net::HTTP::Proxy(proxy.host, proxy.port, proxy_user, proxy_pass)
    else
        klass = Net::HTTP
    end
    result = klass.get_response(uri)
    raise "#{result.code} #{result.message} #{uri}" if result.is_a?(Net::HTTPClientError)
    result.body
end

#load_specsObject



36
37
38
39
40
# File 'lib/spiderfw/setup/app_server_client.rb', line 36

def load_specs
    result = http_get(@url+'/list.json')
    list = JSON.parse(result)
    @specs = list.map{ |app| Spider::App::AppSpec.parse_hash(app) }
end

#specsObject



15
16
17
18
# File 'lib/spiderfw/setup/app_server_client.rb', line 15

def specs
    load_specs unless @specs
    @specs
end