koala: how to get shares from Facebook Graph API - ruby

I'm trying to get the count of shares from a Facebook page, but I can't seem to get the result out of the hash.
Here's what I have:
y_response = #graph.get_connection('some_fb_page','posts',
{fields: ['message', 'id', 'from', 'type',
'properties', 'link', 'shares', 'likes.summary(true)',
'comments.summary(true)', 'created_time', 'updated_time']
})
So y_response is Koala::Facebook::API::GraphCollection
y_response.each do |post|
and each of the post elements is a Hash
puts post["shares"]
gives me: {"count"=>3}
but
puts post["shares"]["count"]
gives an
undefined method `[]' for nil:NilClass (NoMethodError)
I've also tried
puts post["shares"][:count]
puts post["shares"][count]
puts post["shares"].count
for laughs.
What am I doing wrong?
How do I get the count from the hash?

Related

unable to click on checkbox within shadowroot

I am writing a test to go through a test flow of a monetary registration. The registration has Add-ons that appear in different steps that flow. My goal is to be able to select the checkboxes in the add-on shadow root, so I can get to the next step. The elements within the shadow root that I'm trying to focus on seem like they can be targeted, and they are Enumerators, Arrays, and Hashes.
What I need help with is clicking on the checkbox, but the .click method doesn't work for the elements that I'm targeting. I don't return an error for .select, but it doesn't seem to do anything either.
As I step through the code, I think that I am targeting the correct element(s) which is centered around a checkbox. But none of the elements I see seem to be classes that can be "clicked" or interacted with. What I've tried so far:
sales_channels_reg.execute_script("return arguments[0].shadowRoot", sales_channels_reg.regsaver_section.regsaver_testtest.find("#sales-channels-element", visible: false)).find(css: "label[for='addToCart--false']")
Returns:
#<Enumerator: {"shadow-6066-11e4-a52e-4f735466cecf"=>"416aad9c-c512-496d-93c3-91c17ec6e27d"}:find(css: label[for='addToCart--false'])>
- sales_channels_reg.execute_script("return arguments[0].shadowRoot", sales_channels_reg.regsaver_section.regsaver_testtest.find("#sales-channels-element", visible: false)).find(id: "addToCart--false")
Returns
#<Enumerator: {"shadow-6066-11e4-a52e-4f735466cecf"=>"416aad9c-c512-496d-93c3-91c17ec6e27d"}:find(id: addToCart--false)>
- sales_channels_reg.execute_script("return arguments[0].shadowRoot", sales_channels_reg.regsaver_section.regsaver_testtest.find("#sales-channels-element", visible: false))
Returns
{"shadow-6066-11e4-a52e-4f735466cecf"=>"416aad9c-c512-496d-93c3-91c17ec6e27d"}
sales_channels_reg.execute_script("return arguments[0].shadowRoot", sales_channels_reg.regsaver_section.regsaver_testtest.find("#sales-channels-element", visible: false)).class
Returns
Hash
sales_channels_reg.execute_script("return arguments[0].shadowRoot", sales_channels_reg.regsaver_section.regsaver_testtest.find("#sales-channels-element", visible: false)).find(id: "addToCart--false").each do |c| puts c, c.class end
Returns
shadow-6066-11e4-a52e-4f735466cecf
416aad9c-c512-496d-93c3-91c17ec6e27d
Array
*** NoMethodError Exception: undefined method `call' for {:id=>"addToCart--false"}:Hash
(byebug) sales_channels_reg.execute_script("return arguments[0].shadowRoot", sales_channels_reg.regsaver_section.regsaver_testtest.find("#sales-channels-element", visible: false)).find(id: "addToCart--false", :visible => false).click
*** NoMethodError Exception: undefined method `click' for #<Enumerator:0x00007fbeb9195558>
nil
Code for what I'm writing
-first file-
describe "Showing 4 max add-ons in reg flow", type: :feature, service: "sales_channels1" do
context "yadda yadda" do
subject(:sales_channels_reg) { SalesChannelsRegGenerals.new }
subject(:se_signup_page) { SELogin.new }
# subject(:gen_reg) { RegistrationHelper.new }
let(:form_number) { "848624247" }
it "in test flow does thing" do
couple of other steps first
byebug (where I'm at in the code now)
end
end
end
-different file-
require "./spec/page_models/sales_channels_ncsa_section.rb"
require "./spec/page_models/sales_channels_medsaver_section.rb"
require "./spec/page_models/sales_channels_regsaver_section.rb"
require "./spec/page_models/sales_channels_four_addons_section.rb"
class SalesChannelsRegGenerals < SitePrism::Page
set_url "https://zachpartyka#{SeleniumTest.ngin_site}/register/form/{/form_number}"
section :regsaver_section, RegSaverSection, "div#siteContainer2"
end
-different file-
class RegSaverSection < SitePrism::Section
element :regsaver_testtest, "#pageContentContainer"
end
None of those values should be Enumerators, Hashes, etc (unless site-prism is really screwing with Capybaras returns). I'm guessing the reason is that you're using execute_script when you should be using evaluate_script. execute_script shouldn't be used when you expect a return value, and won't unwrap results into element references, evaluate_script does.
sales_channels_reg.evaluate_script("arguments[0].shadowRoot", ...)

Trying to color the cells of an excel sheet once data is verified in ruby-capybara

Below is my code where i try to find the text from the excel sheet and click that 'text' on the application. Once done I want to color that particular cell(where the text is located ) in green color.
require 'rubyXL'
require 'roo'
workbook = Roo::Spreadsheet.open(file_name)
workbook1 = RubyXL::Parser.parse(file_name) workbook.default_sheet='Left navigation'
worksheet1 = workbook1['Left navigation']
for j in (workbook.first_row..workbook.last_row)
for k in (workbook.first_column..workbook.last_column)
if(k==1)
choose("lookupSearch", :option => "Plan")
fill_in "searchInput", :with => workbook.cell(j,k)
find(:xpath, '//*[#id="searchicon"]').click
elsif(workbook.cell(j,k)=='.')
puts "Completed"
else
find('a', text: workbook.cell(j,k), :match => :prefer_exact)
worksheet1.sheet_data[j][k].change_fill ('008000')
end
end
end
Error message is "undefined method `[]' for nil:NilClass (NoMethodError)"
Note : When I use worksheet1.sheet_data[j-1][k-1].change_fill ('008000') there is no error .But still green color is not there on the cell. Unable to trace . Please Advice

Mongo/Ruby undefined method `update' for Mongo::Collection

I am having an issue within stupid-simple piece of ruby/mongo code below:
require 'mongo'
client = Mongo::Client.new(['127.0.0.1:27017'], database: 'dbs')
items = client[:items].find('issues.category': 'general')
items.each do |item|
item2 = item
client[:items].update({ '_id': item['_id'] } , item2)
end
I get undefined method "update" for #<Mongo::Collection:0x4544580 namespace=dbs.items> (NoMethodError)
There is no update method for the MongoDB ruby driver, there is either update_one or update_many.
In your case, it looks like you're trying to update all:
client[:items].update_many({ :id => item['_id'] }, item2)
See docs here

Twitter API - Ruby Twitter Gem

How can I access Twitter::Cursor hash values returned by the Twitter API?
I am following the Jumpstartlab Microblogger tutorial for using the Twitter gem via the jumpstart_auth gem.
I am on iteration 4 step 1. I can return a friends object with the following code:
def friends_last_tweets
friends = client.friends
puts friends
end
=> Twitter::Cursor:0x00000104051928
However, the example account, 'client' in this case, has two 'friends' not just one so why does it only return one object? I thought maybe that object is the array or arrays with all of the friends accordingly in hash values within, thus use [] to access, but this returns "undefined method for Twitter::Cursor". I run each on the Twitter::Cursor object and it returns two fixnums:
def friends_last_tweets
friends = client.friends
friends.each { |f| puts f }
end
=> 18908095
108528349
So surely these numbers must represent each 'friend object' within the Twitter::Cursor object me thinks. I need to access the key/value pairs within that object, yet my attempted hash accessing results in undefined method or variable.
In case it's version issue related, I'm using Twitter5.11.0 and Jumpstart_auth 0.6.0.
those answers didn't helped me to get the last message (maybe the API changed in the meantime), that's how I finally did it:
def everyones_last_tweet
puts "\n\n here are the latest tweets of your friends:"
friends = #client.friends.collect { |f| #client.user(f) }
friends.each do |friend|
puts "\n\n#{friend.screen_name} wrote: \n\t #{friend.status.text}"
end
return ""
end
I'm not happy with that return string though
Access the 'friends' object in the same way you accessed the 'followers' object earlier in the tutorial in order to get a list of your followers' screen names.
To get an array of followers' screen names:
screen_names = #client.followers.collect {|f| #client.user(f).screen_name }
To get an array of friends' screen names:
screen_names = #client.friends.collect {|f| #client.user(f).screen_name }
To get the last tweet of a friend, you can use the object_id's you posted above, as:
last_tweet = #client.user(object_id).status.tweet
I hope this helps. I was caught on this issue for a while too.

Extract a value from an OpenStruct Ruby object

I get the following Ruby object returned (from a query to the Google Analytics API using the garb gem, comes from the sample call shown on the README.md there, Exits.results(profile, :filters => {:page_path.eql => '/'}))
> data.results
=> [#<OpenStruct page_path="/", exits="3706", pageviews="10440">]
I'd to extract the pageviews value (10440), but cannot figure out how to do it. I see that my object, data.results is class array of length 1, but data.first is class OpenStruct with a return value that looks almost identical:
irb(main):140:0> data.results.class
=> Array
irb(main):141:0> data.results.length
=> 1
irb(main):142:0> data.first
=> #<OpenStruct page_path="/", exits="3706", pageviews="10440">
irb(main):143:0> data.first.class
=> OpenStruct
while data itself seems to be a custom return type called ResultsSet:
irb(main):144:0> data.class
=> Garb::ResultSet
irb(main):145:0> data
=> #<Garb::ResultSet:0x00000002411070 #results=[#<OpenStruct page_path="/", exits="3706", pageviews="10440">], #total_results=1, #sampled=false>
irb(main):146:0>
Lots of data structures, but no idea how to get my desired value out. I gathered OpenStruct was related to a hash, so I thought data.first["pageviews"] would do it,
NoMethodError: undefined method `[]' for #<OpenStruct page_path="/", exits="3706", pageviews="10440">
from (irb):146
from /usr/bin/irb:12:in `<main>'
Meanwhile data.first.keys returns nil. No idea how to get my data out, (short of converting the length-1 array, data.results to a string and parsing with grep, which seems crazy. Any ideas?
Please try this:
data.first.pageviews

Resources