Class: AMON::Session
- Inherits:
-
Object
- Object
- AMON::Session
- Defined in:
- lib/amon/session.rb
Overview
Session objects take care of making the actual HTTP requests to the AMON API
## Caching ##
Sessions support caching which is enabled by default, but can be optionally turned off. When caching is enabled, the same request will not be performed twice in a session. Be aware that this has some important implications:
-
If a long running process uses the same session, the cache may become stale as the backend data changes, but new API requests are not made.
-
There is no mechanism for automatically expiring the cache; this is left up to users of the library.
-
The cache stores the AMON objects, rather than the raw response data. In this sense it functions as a sort of identity map - getting the same entity twice will return exactly the same object twice, rather than two different objects representing the same data.
Often the most natural way to ‘expire the cache’ is to discard the session and use a new one at regular intervals. Alternatively, you can use #clear_cache and keep the same session.
Constant Summary collapse
- DEFAULT_OPTIONS =
The default options for a session
{ :base_uri => nil, :user => nil, :password => nil, :cache => true }.freeze
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
The hash of configuration options to be used when making requests.
Instance Method Summary collapse
-
#clear_cache ⇒ Object
Completely clears the cache.
-
#device(entity_id, device_id) ⇒ Device
Retrieves a device from the API.
-
#entity(entity_id) ⇒ Entity
Retrieves an entity from the API.
-
#initialize(options = {}) ⇒ Session
constructor
(note that this includes the API version, as this library only knows about version 1 of the API).
-
#measurements(entity_id, device_id, start_date, end_date, raw = false) ⇒ Object
Retrieves measurements from the API.
-
#metering_point(metering_point_id) ⇒ MeteringPoint
Retrieves a metering point from the API.
Constructor Details
#initialize(options = {}) ⇒ Session
(note that this includes the API version, as this library only knows about version 1 of the API)
43 44 45 46 |
# File 'lib/amon/session.rb', line 43 def initialize( = {}) @options = DEFAULT_OPTIONS.merge() @options.freeze end |
Instance Attribute Details
#options ⇒ Hash (readonly)
Returns The hash of configuration options to be used when making requests.
27 28 29 |
# File 'lib/amon/session.rb', line 27 def @options end |
Instance Method Details
#clear_cache ⇒ Object
Completely clears the cache
108 109 110 |
# File 'lib/amon/session.rb', line 108 def clear_cache @cache = nil end |
#device(entity_id, device_id) ⇒ Device
Retrieves a device from the API
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/amon/session.rb', line 61 def device(entity_id, device_id) cache(:devices, device_id) do json = get( "/entities/#{ URI.escape(entity_id )}/devices/#{URI.escape(device_id)}" + ";earliest" + ";latest" + ";completeness" )['device'] Device.new(self, json) end end |
#entity(entity_id) ⇒ Entity
Retrieves an entity from the API
51 52 53 54 55 56 |
# File 'lib/amon/session.rb', line 51 def entity(entity_id) cache(:entities, entity_id) do json = get("/entities/#{URI.escape(entity_id)};completeness")['entity'] Entity.new(self, json) end end |
#measurements(entity_id, device_id, start_date, end_date, raw = false) ⇒ Object
Retrieves measurements from the API
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/amon/session.rb', line 92 def measurements(entity_id, device_id, start_date, end_date, raw = false) cache(:measurements, [device_id, start_date, end_date, raw]) do device = device(entity_id, device_id) measurements = get( "/entities/#{ URI.escape(entity_id) }/devices/#{ URI.escape(device.id) }/measurements" + ";aggregation" + "?" + "raw=" + raw.to_s + "&" + "startDate=" + start_date.utc.xmlschema + "&" + "endDate=" + end_date.utc.xmlschema )['measurements'] measurements.map { |measurement| Measurement.new(device, measurement) } end end |
#metering_point(metering_point_id) ⇒ MeteringPoint
Retrieves a metering point from the API
76 77 78 79 80 81 |
# File 'lib/amon/session.rb', line 76 def metering_point(metering_point_id) cache(:metering_points, :metering_point_id) do json = get("/metering-points/#{URI.escape(metering_point_id)}")['metering_point'] MeteringPoint.new(self, json) end end |