class SteamController < ApplicationController require 'rubygems' require 'scrubyt' def index @steam_data = get_steam_data(params[:steam_id]) end private def get_steam_data(steam_id) steam_data = Scrubyt::Extractor.define do fetch 'http://steamcommunity.com/id/' << steam_id friendly_name "//div[@id='mainContents']/h1[1]" last_online "//p[@id='statusOfflineText'][1]" member_since "//div[@class='statsItem'][1]" steam_rating "//div[@class='statsItem'][3]" playing_time "//div[@class='statsItem'][5]" image "//div[@id='profileAvatar']/div/div/img[1]" do image_src "src", :type => :attribute end link "//div[@id='rightActionBlock']/div/a[1]" do add_link "href", :type => :attribute end end steam_data = steam_data.to_hash end end
<h1><%= @steam_data[0][:friendly_name] %></h1> <p><%= link_to 'Goto ' << @steam_data[0][:friendly_name].pluralize << ' Steam profile', 'http://steamcommunity.com/id/' << params[:steam_id] %></p> <p><%= link_to 'Add ' << @steam_data[0][:friendly_name] << ' as a Steam friend', @steam_data[5][:add_link] %></p> <p><%= image_tag @steam_data[4][:image_src] %></p> <p><%= debug @steam_data %></p>
Refactorings
No refactoring yet !
Adam
December 19, 2008, December 19, 2008 04:29, permalink
You need a model.
class SteamData < OpenStruct
class << self
def find(id)
# You may have to massage the results from to_hash
new extract_steam_data(id).to_hash
end
def extract_steam_data(id)
Scrubyt::Extractor.define do
fetch profile_url(id)
# Clipped for brevity ...
end
end
def profile_url(id)
"http://steamcommunity.com/id/#{id}"
end
end
def profile_url
self.class.profile_url(id)
end
end
class SteamDataController < ApplicationController
def show
@steam_data = SteamData.find(params[:id])
end
end
<h1><%=h @steam_data.friendly_name %></h1>
<p>
<% link_to @steam_data.profile_url do %>
Goto <%=h @steam_data.friendly_name.pluralize %> Steam profile
<% end %>
</p>
<p>
<% link_to @steam_data.add_link do %>
Add <%=h @steam_data.friendly_name %> as a Steam friend
<% end %>
</p>
<p>
<%= image_tag @steam_data.image_src %>
</p>
sfusion.myopenid.com
December 19, 2008, December 19, 2008 17:38, permalink
still getting hassle with array indexes in the view. any ideas?
<%= image_tag @steam_data[4][:image_src] %> # works <%= image_tag @steam_data.image_src %> # doesn't work
Adam
December 21, 2008, December 21, 2008 20:19, permalink
Like I noted in the comments, you will probably need to "massage" the results from Scrubt::Extractor. I have no idea what the results look like, but if it's just an array of hashes that needs to be merged, try this:
class SteamData < OpenStruct
class << self
def find(id)
# You may have to massage the results from to_hash
new extract_steam_data(id).to_hash.inject({}) { |hash,element| hash.merge!(element) }
end
# ...
Paul
April 2, 2010, April 02, 2010 01:44, permalink
Agence de Location de voitures au Maroc
Site Web a vendre concu pour les agences de location
Jesica
April 13, 2010, April 13, 2010 20:17, permalink
Terapia psicologica en monterrey, psicologos en monterrey, zona centro, informes y citas tel. 1777-0678
http://www.vitalti.com
Terapia psicologica en monterrey, psicologos en monterrey, zona centro, informes y citas tel. 1777-0678 Terapia psicologica en monterrey, psicologos en monterrey, zona centro, informes y citas tel. 1777-0678 Terapia psicologica en monterrey, psicologos en monterrey, zona centro, informes y citas tel. 1777-0678 Terapia psicologica en monterrey, psicologos en monterrey, zona centro, informes y citas tel. 1777-0678 Terapia psicologica en monterrey, psicologos en monterrey, zona centro, informes y citas tel. 1777-0678 Terapia psicologica en monterrey, psicologos en monterrey, zona centro, informes y citas tel. 1777-0678 Terapia psicologica en monterrey, psicologos en monterrey, zona centro, informes y citas tel. 1777-0678 Terapia psicologica en monterrey, psicologos en monterrey, zona centro, informes y citas tel. 1777-0678 Terapia psicologica en monterrey, psicologos en monterrey, zona centro, informes y citas tel. 1777-0678
lorenzo
May 8, 2010, May 08, 2010 18:40, permalink
case vacanze affitto appartamenti annunci da Privati Toscana offerte vacanze
case vacanze affitto appartamenti annunci da Privati Toscana offerte vacanze
http://www.affittocasevacanze.net
Can't help but feel I might be doing this wrong?
Anyone care to shed some light.