Class: NBS::Metric

Inherits:
Object
  • Object
show all
Extended by:
MemcachedMemoize
Defined in:
lib/next-big-sound/metric.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MemcachedMemoize

remember

Constructor Details

#initialize(artist_id, service_type, metric, sdate, edate) ⇒ Metric

Returns a new instance of Metric.



9
10
11
12
13
14
15
# File 'lib/next-big-sound/metric.rb', line 9

def initialize(artist_id,service_type,metric,sdate,edate)
  self.artist_id = artist_id
  self.service_type = service_type
  self.metric = metric
  self.sdate = Date.parse(sdate.to_s)
  self.edate = Date.parse(edate.to_s)  
end

Instance Attribute Details

#artist_idObject

Returns the value of attribute artist_id.



6
7
8
# File 'lib/next-big-sound/metric.rb', line 6

def artist_id
  @artist_id
end

#edateObject

Returns the value of attribute edate.



6
7
8
# File 'lib/next-big-sound/metric.rb', line 6

def edate
  @edate
end

#metricObject

Returns the value of attribute metric.



6
7
8
# File 'lib/next-big-sound/metric.rb', line 6

def metric
  @metric
end

#sdateObject

Returns the value of attribute sdate.



6
7
8
# File 'lib/next-big-sound/metric.rb', line 6

def sdate
  @sdate
end

#service_typeObject

Returns the value of attribute service_type.



6
7
8
# File 'lib/next-big-sound/metric.rb', line 6

def service_type
  @service_type
end

#xmlObject

Returns the value of attribute xml.



6
7
8
# File 'lib/next-big-sound/metric.rb', line 6

def xml
  @xml
end

Instance Method Details

#data_pointsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/next-big-sound/metric.rb', line 21

def data_points
  unless @data_points.empty?
    return @data_points
  end
  begin
    points = to_hash["Profiles"][0]["Profile"][0]["DataPoint"]
    dps = []
    points.each do |dp|
      dps << NBS::DataPoint.new(dp["date"],dp["value"])
    end
    return dps
  rescue
    return []
  end
end

#fetch(myopts_test = {}) ⇒ Object



16
17
18
19
20
# File 'lib/next-big-sound/metric.rb', line 16

def fetch(myopts_test={})
  options = {"service"=>self.service_type,"metric"=>self.metric,"start"=>self.sdate.to_s,"end"=>self.edate.to_s,"artistID"=>self.artist_id,"apiKey"=>$nbs_api_key,"format"=>"xml"}
  puts "#{NBS::NBS_CONFIG["base_url"]}getDataForArtist?#{options.to_url_params}"
  statsxml = Net::HTTP.get(URI.parse("#{NBS::NBS_CONFIG["base_url"]}getDataForArtist?#{options.to_url_params}")).to_s
end

#load_datapoints(data_points) ⇒ Object

this will allow me to load the data that I previously fetched.



37
38
39
# File 'lib/next-big-sound/metric.rb', line 37

def load_datapoints(data_points)
  @data_points = data_points
end

#to_hashObject



49
50
51
# File 'lib/next-big-sound/metric.rb', line 49

def to_hash
  Hash.from_xml(to_xml)
end

#to_xmlObject

end



46
47
48
# File 'lib/next-big-sound/metric.rb', line 46

def to_xml
  self.xml ||= fetch({"service"=>self.service_type,"metric"=>self.metric,"start"=>self.sdate.to_s,"end"=>self.edate.to_s,"artistID"=>self.artist_id,"apiKey"=>$nbs_api_key,"format"=>"xml"})
end