I'm working on a gem. Here is the homepage of it: https://github.com/scaryguy/vakit
If you check out the source code, you can see that I'm parsing an external HTML page to filter some data from it.
The issue is, eventhough I fetch all data I want with one request, each time I call Vakit.sabah
or Vakit.oglen
a new request is done.
require "vakit/version"
require 'vakit/connect'
require 'Nokogiri'
require 'open-uri'
module Vakit
def self.today
Vakit::Connect.shaber
end
def self.imsak
Vakit::Connect.shaber[:imsak]
end
def self.sabah
Vakit::Connect.shaber[:sabah]
end
def self.oglen
Vakit::Connect.shaber[:oglen]
end
def self.ikindi
Vakit::Connect.shaber[:ikindi]
end
def self.aksam
Vakit::Connect.shaber[:aksam]
end
def self.yatsi
Vakit::Connect.shaber[:yatsi]
end
end
I don't think that it's an efficient way.
I should be able to access attributes of my hash without new request, shouldn't I?
module Vakit
class Connect
def initialize(opt={})
@path = opt[:path]
end
def self.shaber
doc = Nokogiri::HTML(open('http://www.samanyoluhaber.com/'))
x = doc.css('#hnmzT')
times = []
x.each do |vakit|
data = vakit.children.first.children.last.content
data_add = data.slice(0..data.length-2)
times.push(data_add)
end
times
vakit = {
imsak: times[0],
sabah: times[1],
oglen: times[2],
ikindi: times[3],
aksam: times[4],
yatsi: times[5]
}
end
end
end
I need some enlightment.