class Carbonculture::Channel

Attributes

body[RW]
data[RW]
name[RW]
organisation_name[RW]
place_name[RW]
results[RW]

Public Class Methods

new(name, place_name, organisation_name, options = {}) click to toggle source
# File lib/carbonculture/channel.rb, line 6
def initialize(name, place_name, organisation_name, options = {})
  self.name = name
  self.place_name = place_name
  self.organisation_name = organisation_name
  self.results = []

  call_api(options)

  begin
    self.body = JSON.parse(self.data.body)
  rescue
    raise ArgumentError
  end

  build_results

end

Public Instance Methods

method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# File lib/carbonculture/channel.rb, line 28
def method_missing(method_name, *args, &block)
  if body.has_key?(method_name.to_s)
    body[method_name.to_s]
  else
    super
  end
end
place() click to toggle source
# File lib/carbonculture/channel.rb, line 24
def place
  Place.new(place_name, organisation_name)
end

Private Instance Methods

build_results() click to toggle source
# File lib/carbonculture/channel.rb, line 45
def build_results
  set_start = Time.parse(body['start_time'])
  set_end = Time.parse(body['end_time'])
  set_ratio = (set_end - set_start) / body['points'].to_i
  body['results'].each do |result|
    self.results << Result.new(set_start, set_start + set_ratio, result.first)
    set_start = set_start + set_ratio
  end
end
call_api(options = {}) click to toggle source
# File lib/carbonculture/channel.rb, line 38
def call_api(options = {})
  option_params = []
  options.each_pair { |k, v| option_params << "#{ k }=#{ v }" }

  self.data = self.class.get "#{ BASE_URL }/#{ organisation_name }/#{ place_name }/#{ name }" + '?' + option_params.join('&')
end