Class: Navfund::Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/navfund/provider.rb,
lib/navfund/exceptions.rb

Defined Under Namespace

Classes: InvalidFund

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fundsObject (readonly)

Returns the value of attribute funds.



5
6
7
# File 'lib/navfund/provider.rb', line 5

def funds
  @funds
end

Class Method Details

.strip_value(val) ⇒ Object



16
17
18
# File 'lib/navfund/provider.rb', line 16

def self.strip_value(val)
  val.strip.gsub('PHP', '').gsub('USD', '').gsub(',', '').strip
end

Instance Method Details

#fund_namesObject

List supported funds by name



21
22
23
# File 'lib/navfund/provider.rb', line 21

def fund_names
  funds.map{ |x| x[:name] }
end

#fund_valuesObject

Display all fund values



41
42
43
44
45
# File 'lib/navfund/provider.rb', line 41

def fund_values
  fvals = []
  fund_names.each{ |f| fvals << { :name => f, :value => self.value(f) } }
  fvals
end

#scrape(opts = {}) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/navfund/provider.rb', line 7

def scrape(opts={})
  if opts[:check_ssl] == false
    @document = open(@url, :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE).read
  else
    @document = open(@url).read
  end
  @wrapped_document = Nokogiri::HTML(@document)
end

#uitf_com_ph_date_parserObject



62
63
64
65
66
# File 'lib/navfund/provider.rb', line 62

def uitf_com_ph_date_parser
  dtext = @wrapped_document.search("h5").first.text
  dstr = dtext.split("as of").last.strip
  Date.parse(dstr)
end

#uitf_com_ph_parser(fund) ⇒ Object

Generic parser for uitf.com.ph website



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/navfund/provider.rb', line 48

def uitf_com_ph_parser(fund)
  val = nil
  if valid_fund?(fund)
    fname = @wrapped_document.search("[text()*='#{fund}']").first
    if fname
      fval = fname.ancestors('tr').first.search("td[data-title='NAVpu'] div").first rescue nil
    end
    val = Provider.strip_value(fval.text) if fval
  else
    raise InvalidFund
  end
  val
end

#valid_fund?(fund) ⇒ Boolean

Check if the fund name is supported

Returns:

  • (Boolean)


26
27
28
# File 'lib/navfund/provider.rb', line 26

def valid_fund?(fund)
  fund_names.include?(fund)
end

#valueObject

Returns the fund value



31
32
33
# File 'lib/navfund/provider.rb', line 31

def value
  nil
end

#value_atObject

Returns the date of the fund values



36
37
38
# File 'lib/navfund/provider.rb', line 36

def value_at
  nil
end