Class: IspUsage::Fetchers::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ispusage/fetchers/fetcher.rb

Direct Known Subclasses

AUExetel, AUIinet, AUInternode, AUOptus, AUThree, AUTpg, AUTranslink

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Fetcher

Returns a new instance of Fetcher.



9
10
11
12
13
14
15
# File 'lib/ispusage/fetchers/fetcher.rb', line 9

def initialize(options)
  @username = options[:username]
  @password = options[:password]
  self.options = options
  self.month_end = options[:month_end]
  self.usage_periods = []
end

Instance Attribute Details

#errorObject

Returns the value of attribute error.



7
8
9
# File 'lib/ispusage/fetchers/fetcher.rb', line 7

def error
  @error
end

#month_endObject

Returns the value of attribute month_end.



7
8
9
# File 'lib/ispusage/fetchers/fetcher.rb', line 7

def month_end
  @month_end
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/ispusage/fetchers/fetcher.rb', line 7

def options
  @options
end

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/ispusage/fetchers/fetcher.rb', line 7

def password
  @password
end

#usage_periodsObject

Returns the value of attribute usage_periods.



7
8
9
# File 'lib/ispusage/fetchers/fetcher.rb', line 7

def usage_periods
  @usage_periods
end

#usernameObject

Returns the value of attribute username.



7
8
9
# File 'lib/ispusage/fetchers/fetcher.rb', line 7

def username
  @username
end

Instance Method Details

#month_usedObject



40
41
42
43
44
45
46
47
# File 'lib/ispusage/fetchers/fetcher.rb', line 40

def month_used
  return nil if month_end.nil?
  month_start = Time.local(month_end.year, month_end.month - 1, month_end.day)

  month_length = month_end.to_f - month_start.to_f

  (1 - (month_end.to_f - Time.now.to_f) / month_length.to_f) * 100
end

#new_usage_period(options = {}) ⇒ Object



49
50
51
52
53
# File 'lib/ispusage/fetchers/fetcher.rb', line 49

def new_usage_period(options = {})
  usage_period = IspUsage::UsagePeriod.new(self, options)
  self.usage_periods << usage_period
  usage_period
end

#to_hashObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ispusage/fetchers/fetcher.rb', line 17

def to_hash
  hash = {
    :isp => self.class.to_s.split('::').last,
    :username => @username,
    :usage_periods => self.usage_periods.map(&:to_hash)
  }

  unless month_end.nil?
    hash[:month_end] = month_end
    hash[:month_used] = month_used
  end

  unless error.nil?
    hash[:error] = error
  end

  hash
end

#to_jsonObject



36
37
38
# File 'lib/ispusage/fetchers/fetcher.rb', line 36

def to_json
  to_hash.to_json
end