This is the code from my service object 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
When I do a pry
at the end of that file, this is the response I get:
[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
So that has the results I want. Notice all the methods accessible on my @titles
object.
However, when I try to execute this service object in my portfolio_controller.rb
like this:
class PortfolioController < ApplicationController
def ticker
@output = GrabFeedFromJSE.new('http://www.someurl.com')
end
end
This is what the result looks like:
[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
Why are the objects different?
How do I get my @titles
object in my GrabFeedFromJSE
class in that service object, to be accessible to my @output
instance variable in my controller.
I figured it out.
This is what I did....in my services/grab_feed_from_jse.rb
file:
class GrabFeedFromJSE
require 'oga'
require 'net/http'
def initialize(url)
body = Net::HTTP.get(URI.parse(url))
@document = Oga.parse_html(body)
end
def nodes
@document.css('li')
end
end
Then in my portfolio_controller.rb
, I did this:
class PortfolioController < ApplicationController
def ticker
@output = GrabFeedFromJSE.new('http://www.someurl.com')
end
end
In my views, I now do @output.nodes
and I iterate over that collection.
That solves that.