Class: WurflCloud::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/wurfl_cloud/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(e, c) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
# File 'lib/wurfl_cloud/client.rb', line 13

def initialize(e, c)
  @environment = e
  @cache = c
  @device_capabilities = WurflCloud::DeviceCapabilities.new(@environment.user_agent)
  @read_from_cache = false
end

Instance Attribute Details

#device_capabilitiesObject (readonly)

Returns the value of attribute device_capabilities.



11
12
13
# File 'lib/wurfl_cloud/client.rb', line 11

def device_capabilities
  @device_capabilities
end

#read_from_cacheObject (readonly)

Returns the value of attribute read_from_cache.



11
12
13
# File 'lib/wurfl_cloud/client.rb', line 11

def read_from_cache
  @read_from_cache
end

Class Method Details

.detect_device(environment, cache) ⇒ Object



62
63
64
65
66
# File 'lib/wurfl_cloud/client.rb', line 62

def detect_device(environment, cache)
  new(environment, cache).tap do |client|          
    client.detect
  end
end

Instance Method Details

#[](capability_key) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/wurfl_cloud/client.rb', line 20

def [](capability_key)
  if !@device_capabilities.has_key?(capability_key) && @read_from_cache
    load_from_server!
    store_in_cache
  end
  @device_capabilities[capability_key]
end

#detectObject



28
29
30
31
32
33
# File 'lib/wurfl_cloud/client.rb', line 28

def detect
  unless load_from_cache 
    load_from_server!
    store_in_cache
  end
end

#load_from_server!Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/wurfl_cloud/client.rb', line 35

def load_from_server!
  begin
    # make the request
    Net::HTTP.start(WurflCloud.configuration.host,WurflCloud.configuration.port) do |http|
      req = Net::HTTP::Get.new(WurflCloud.configuration.path, http_headers)
      req.basic_auth WurflCloud.configuration.api_user, WurflCloud.configuration.api_password
      res = http.request(req)
      
      # emit the result
      if res.code=="200" 
        # read the response
        @device_capabilities = WurflCloud::DeviceCapabilities.parse(res.body)
        @cache.validate(@device_capabilities.mtime.to_i)
        @read_from_cache = false
      else
        raise WurflCloud::Errors::ConnectionError.new("#{res.code} #{res.message}")
      end
    end    
  rescue WurflCloud::Errors::GenericError => exc
    raise exc
  rescue Exception => exc
    raise WurflCloud::Errors::ConnectionError.new(exc.message)
  end
end