Как передать данные из моего сервисного объекта в контроллер? - PullRequest
0 голосов
/ 11 мая 2018

Это код моего сервисного объекта 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 в конце этого файла, я получаю ответ:

[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 instance в моем контроллере.

1 Ответ

0 голосов
/ 11 мая 2018

Я понял это.

Это то, что я сделал .... в моем services/grab_feed_from_jse.rb файле:

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

Тогда в моем portfolio_controller.rb я сделал это:

class PortfolioController < ApplicationController

  def ticker
    @output = GrabFeedFromJSE.new('http://www.someurl.com')
  end
end

По-моему, теперь я делаю @output.nodes и перебираю эту коллекцию.

Это решает это.

...