Class: Qinium::HostManager

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

Defined Under Namespace

Classes: Cache

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ HostManager

Returns a new instance of HostManager.



49
50
51
# File 'lib/qinium/host_manager.rb', line 49

def initialize(config)
  @config = config
end

Instance Attribute Details

#config ⇒ Object (readonly)

Returns the value of attribute config.



47
48
49
# File 'lib/qinium/host_manager.rb', line 47

def config
  @config
end

Class Method Details

.cache ⇒ Object



43
44
45
# File 'lib/qinium/host_manager.rb', line 43

def self.cache
  @cache ||= Cache.new
end

Instance Method Details

#fetch_host(bucket, opts = {}) ⇒ Object



62
63
64
65
# File 'lib/qinium/host_manager.rb', line 62

def fetch_host(bucket, opts = {})
  hosts = hosts(bucket)
  hosts[extract_protocol(opts)]["io"][0]
end

#hosts(bucket) ⇒ Object

Raises:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/qinium/host_manager.rb', line 67

def hosts(bucket)
  raise Error, "access_key is missing" if Utils.blank?(access_key)
  raise Error, "bucket is missing" if Utils.blank?(bucket)

  cache.read(access_key, bucket) do
    query = Utils.to_query_string(ak: access_key, bucket: bucket)
    url = "#{config.uc_host}/v1/query?#{query}"
    http = Client.new(config)
    code, body, headers = http.get(url)
    raise APIError.new(code, body, headers) unless Client.success?(code)

    body
  end
end

#up_host(bucket, opts = {}) ⇒ Object



53
54
55
# File 'lib/qinium/host_manager.rb', line 53

def up_host(bucket, opts = {})
  up_hosts(bucket, opts)[0]
end

#up_hosts(bucket, opts = {}) ⇒ Object



57
58
59
60
# File 'lib/qinium/host_manager.rb', line 57

def up_hosts(bucket, opts = {})
  hosts = hosts(bucket)
  hosts[extract_protocol(opts)]["up"]
end