这是我的服务对象的代码app/services/grab_feed_from_jse.rb
:
class GrabFeedFromJSE
def initialize(url)
require 'oga'
require 'net/http'
body = Net::HTTP.get(URI.parse(url))
document = Oga.parse_html(body)
@titles = document.css('li')
end
end
当我pry
在该文件的末尾执行 a 时,这是我得到的响应:
[1] pry(#<GrabFeedFromJSE>)> @titles
=> NodeSet(Element(name: "li" children: NodeSet(Text(" "), Element(name: "input" attributes: [Attribute(name: "class" value: "ticker-status"), Attribute(name: "type" value: "hidden"), Attribute(name: "name" value: "Language"), Attribute(name: "value" value: "summary")]))), Element(name: "li" attributes: [Attribute(name: "class" value: "live-ticker-heading")] children: NodeSet(Text("Market Summary for 2018-05-10 "), Element(name: "br"), Text(" "))), Element(name: "li" children: NodeSet(Text("\n\n "), Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/index-data/jse-index")] children: NodeSet(Text("\n\n \n\n\n JSE Index \n \n "), Element(name: "br"), Text("\n\n 5,201,225 Vol\n "), Element(name: "br"), Text("\n\n 299,198.19 \n "), Element(name: "img" attributes: [Attribute(name: "style" value: "height:16px"), Attribute(name: "src" value: "/img/up.png")]), Text("\n\n \n 22.32\n "))), Text("\n\n\n "))), Element(name: "li" children: NodeSet(Text("\n\n "), Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/index-data/all-jamaican")] children: NodeSet(Text("\n\n \n\n\n JSE All Jamaican Composite Index \n \n "), Element(name: "br"), Text("\n\n 3,842,904 Vol\n "), Element(name: "br"), Text("\n\n 328,387.59 \n "), Element(name: "img" attributes: [Attribute(name: "style" value: "height:16px"), Attribute(name: "src" value: "/img/up.png")]), Text("\n\n \n 24.50\n "))), Text("\n\n\n "))), Element(name: "li" children: NodeSet(Text("\n\n "), Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/index-data/jse-select")] children: NodeSet(Text("\n\n \n\n\n JSE Select Index \n \n "), Element(name: "br"), Text("\n\n 1,995,939 Vol\n "), Element(name: "br"), Text("\n\n 8,948.62 \n "), Element(name: "img" attributes: [Attribute(name: "style" value: "height:16px"), Attribute(name: "src" value: "/img/down.png")]), Text("\n\n \n -31.23\n "))), Text("\n\n\n "))), Element(name: "li" children: NodeSet(Text("\n\n "), Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/index-data/jse-junior")] children: NodeSet(Text("\n\n \n\n\n Junior Market Index \n \n "), Element(name: "br"), Text("\n\n 1,858,849 Vol\n "), Element(name: "br"), Text("\n\n 2,917.69 \n "), Element(name: "img" attributes: [Attribute(name: "style" value: "height:16px"), Attribute(name: "src" value: "/img/down.png")]), Text("\n\n \n -15.45\n "))), Text("\n\n\n "))), Element(name: "li" children: NodeSet(Text("\n\n "), Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/index-data/combined-index")] children: NodeSet(Text("\n\n \n\n\n JSE Combined Index \n \n "), Element(name: "br"), Text("\n\n 7,028,074 Vol\n "), Element(name: "br"), Text("\n\n 306,927.60 \n "), Element(name: "img" attributes: [Attribute(name: "style" value: "height:16px"), Attribute(name: "src" value: "/img/down.png")]), Text("\n\n \n -148.29\n "))), Text("\n\n\n "))), Element(name: "li" children: NodeSet(Text("\n\n "), Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/index-data/us-equities")] children: NodeSet(Text("\n\n \n\n\n JSE USD Equities Index \n \n "), Element(name: "br"), Text("\n\n 220,397 Vol\n "), Element(name: "br"), Text("\n\n 165.29 \n "), Element(name: "img" attributes: [Attribute(name: "style" value: "height:16px"), Attribute(name: "src" value: "/img/down.png")]), Text("\n\n \n -8.87\n "))), Text("\n\n\n "))), Element(name: "li" children: NodeSet(Text("\n\n "), Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/listed-companies/trade-information/CCC/latest")] children: NodeSet(Text("\n\n \n\n\n CCC\n \n "), Element(name: "br"), Text("\n\n 979,272 Vol\n "), Element(name: "br"), Text("\n\n $37.25 \n "), Element(name: "img" attributes: [Attribute(name: "style" value: "height:16px"), Attribute(name: "src" value: "/img/down.png")]), Text("\n\n \n -0.05\n "))), Text("\n\n\n "))), Element(name: "li" children: NodeSet(Text("\n\n "), Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/listed-companies/trade-information/CAR/latest")] children: NodeSet(Text("\n\n \n\n\n CAR\n \n "), Element(name: "br"), Text("\n\n 109,202 Vol\n "), Element(name: "br"), Text("\n\n $10.60 \n "), Element(name: "img" attributes: [Attribute(name: "style" value: "height:16px"), Attribute(name:
[2] pry(#<GrabFeedFromJSE>)> ls @titles
ActiveSupport::ToJsonWithActiveSupportEncoder#methods: to_json
Enumerable#methods:
all? collect_concat each_slice find_index group_by_day_of_month group_by_month_of_year index_by member? partition slice_before to_h
any? cycle each_with_index first group_by_day_of_week group_by_period inject min pluck slice_when to_set
as_json detect each_with_object flat_map group_by_hour group_by_quarter lazy min_by reduce sort uniq
chart_json drop entries grep group_by_hour_of_day group_by_second many? minmax reject sort_by without
chunk drop_while exclude? grep_v group_by_minute group_by_week map minmax_by reverse_each sum zip
chunk_while each_cons find group_by group_by_minute_of_hour group_by_year max none? select take
collect each_entry find_all group_by_day group_by_month include? max_by one? slice_after take_while
Oga::XML::NodeSet#methods:
+ == attr concat delete empty? index inspect length owner= push shift text unshift
<< [] attribute count each equal_nodes? insert last owner pop remove size to_a
instance variables: @existing @nodes @owner
这样就有了我想要的结果。请注意我的@titles
对象上可访问的所有方法。
但是,当我尝试以这样的方式执行此服务对象时portfolio_controller.rb
:
class PortfolioController < ApplicationController
def ticker
@output = GrabFeedFromJSE.new('http://www.someurl.com')
end
end
结果如下所示:
[1] pry(#<PortfolioController>)> @output
=> #<GrabFeedFromJSE:0x00007fe70942d8d8
@titles=
NodeSet(Element(name: "li" children: NodeSet(Text(" "), Element(name: "input" attributes: [Attribute(name: "class" value: "ticker-status"), Attribute(name: "type" value: "hidden"), Attribute(name: "name" value: "Language"), Attribute(name: "value" value: "summary")]))), Element(name: "li" attributes: [Attribute(name: "class" value: "live-ticker-heading")] children: NodeSet(Text("Market Summary for 2018-05-10 "), Element(name: "br"), Text(" "))), Element(name: "li" children: NodeSet(Text("\n\n "), Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/index-data/jse-index")] children: NodeSet(Text("\n\n \n\n\n JSE Index \n \n "), Element(name: "br"), Text("\n\n 5,201,225 Vol\n "), Element(name: "br"), Text("\n\n 299,198.19 \n "), Element(name: "img" attributes: [Attribute(name: "style" value: "height:16px"), Attribute(name: "src" value: "/img/up.png")]), Text("\n\n \n 22.32\n "))), Text("\n\n\n "))), Element(name: "li" children: NodeSet(Text("\n\n "), Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/index-data/all-jamaican")] children: NodeSet(Text("\n\n \n\n\n JSE All Jamaican Composite Index \n \n "), Element(name: "br"), Text("\n\n 3,842,904 Vol\n "), Element(name: "br"), Text("\n\n 328,387.59 \n "), Element(name: "img" attributes: [Attribute(name: "style" value: "height:16px"), Attribute(name: "src" value: "/img/up.png")]), Text("\n\n \n 24.50\n "))), Text("\n\n\n "))), Element(name: "li" children: NodeSet(Text("\n\n "), Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/index-data/jse-select")] children: NodeSet(Text("\n\n \n\n\n JSE Select Index \n \n "), Element(name: "br"), Text("\n\n 1,995,939 Vol\n "), Element(name: "br"), Text("\n\n 8,948.62 \n "), Element(name: "img" attributes: [Attribute(name: "style" value: "height:16px"), Attribute(name: "src" value: "/img/down.png")]), Text("\n\n \n -31.23\n "))), Text("\n\n\n "))), Element(name: "li" children: NodeSet(Text("\n\n "), Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/index-data/jse-junior")] children: NodeSet(Text("\n\n \n\n\n Junior Market Index \n \n "), Element(name: "br"), Text("\n\n 1,858,849 Vol\n "), Element(name: "br"), Text("\n\n 2,917.69 \n "), Element(name: "img" attributes: [Attribute(name: "style" value: "height:16px"), Attribute(name: "src" value: "/img/down.png")]), Text("\n\n \n -15.45\n "))), Text("\n\n\n "))), Element(name: "li" children: NodeSet(Text("\n\n "), Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/index-data/combined-index")] children: NodeSet(Text("\n\n \n\n\n JSE Combined Index \n \n "), Element(name: "br"), Text("\n\n 7,028,074 Vol\n "), Element(name: "br"), Text("\n\n 306,927.60 \n "), Element(name: "img" attributes: [Attribute(name: "style" value: "height:16px"), Attribute(name: "src" value: "/img/down.png")]), Text("\n\n \n -148.29\n "))), Text("\n\n\n "))), Element(name: "li" children: NodeSet(Text("\n\n "), Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/index-data/us-equities")] children: NodeSet(Text("\n\n \n\n\n JSE USD Equities Index \n \n "), Element(name: "br"), Text("\n\n 220,397 Vol\n "), Element(name: "br"), Text("\n\n 165.29 \n "), Element(name: "img" attributes: [Attribute(name: "style" value: "height:16px"), Attribute(name: "src" value: "/img/down.png")]), Text("\n\n \n -8.87\n "))), Text("\n\n\n "))), Element(name: "li" children: NodeSet(Text("\n\n "), Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/listed-companies/trade-information/CCC/latest")] children: NodeSet(Text("\n\n \n\n\n CCC\n \n "), Element(name: "br"), Text("\n\n 979,272 Vol\n "), Element(name: "br"), Text("\n\n $37.25 \n "), Element(name: "img" attributes: [Attribute(name: "style" value: "height:16px"), Attribute(name: "src" value: "/img/down.png")]), Text("\n\n \n -0.05\n "))), Text("\n\n\n "))), Element(name: "li" children: NodeSet(Text("\n\n "), Element(name: "a" attributes: [Attribute(name: "href" value: "/market-data/listed-companies/trade-information/CAR/latest")] children: NodeSet(T
[2] pry(#<PortfolioController>)> ls @output
ActiveSupport::ToJsonWithActiveSupportEncoder#methods: to_json
instance variables: @titles
为什么对象不同?
如何在该服务对象的类中获取我@titles
的对象GrabFeedFromJSE
,以便@output
我的控制器中的实例变量可以访问。