I am trying to get a users friends checkins, I am using omniauth and koala gem.
When a user gets saved this method hits:
def add_friends
friends_data = facebook.get_connections("me", "friends", :fields => "id, name, link, picture, gender, checkins")
friends_data.map do |h|
friend = Friend.new
friend.uid = h["id"]
friend.name = h["name"]
friend.image = h["picture"]
friend.gender = h["gender"]
friend.urls = h["link"]
friend.user_id = self.id
friend.save!
if (!h["checkins"].blank?)
checkin = Checkin.new
checkin.checkin_id = h["id"]
checkin.user_id = h["checkins"]["data"]["from"]["id"]
checkin.user_name = h["checkins"]["data"]["from"]["name"]
checkin.tags = h["checkins"]["tags"]["data"]["name"]
checkin.place_id = h["checkins"]["place"]["id"]
checkin.place_name = h["checkins"]["place"]["name"]
checkin.message = h["checkins"]["message"]
checkin.created_time = h["checkins"]["created_time"]
checkin.friend_id = friend.id
checkin.save!
end
end
end
But I get this error:
Koala::Facebook::APIError: HTTP 500: Response body: {"error_code":1,"error_msg":"An unknown error occurred"}
I dont really know what that means, any ideas? And does anybody know how to define a limit on checkins with the koala gem?
I tried something like this:
u.facebook.get_connections("me","friends", :fields => "checkins.limit(2)")
But I got the same error!
In fields, you're requesting information about a friend, but 'checkins' isn't a profile field, it's an entirely different connection altogether.
What you must do is loop through all the friend IDs and get the checkins for each:
friend_checkins = []
friend_ids = u.facebook.get_connections("me","friends", :fields => "id")
friend_ids.each do |friend_id|
friend_checkins << u.facebook.get_connections(friend_id,"checkins")
end
Also, when doing this, it would be a great time to look into batch requests with Koala, as you could potentially be making a lot of calls to the API here..
Related
I have tried mostly everything. Checked all docs and stack questions and shopify community like:
Shopify API how to do a search query with like
https://community.shopify.com/c/Shopify-APIs-SDKs/Shopify-api-search-products-by-title/td-p/341866
How to search products by title using Shopify product search API?
https://github.com/Shopify/shopify_app
https://community.shopify.com/c/Shopify-APIs-SDKs/Search-product-from-title-handle-and-description/td-p/469156
and found out that
#search = "2018";
#products = ShopifyAPI::Product.find(:all, params: { limit: 10,title:#search })
but this is returning empty array although I have may records containing this in title. https://prnt.sc/sx37o6
I want to get records according to #search
I have tried Product.search too but it causes: undefined method `search' for ShopifyAPI::Product:Class
Using the RestAPI I failed doing filtering (with wildcards for example) as well. But with the GraphQL-API the search functionalities (see here https://shopify.dev/concepts/about-apis/search-syntax) are pretty solid.
This is an example including auth, filtering by title including wildcard-support (for part matching) and mapping results to a simpel array of hashes:
#responses = []
shopify_session = ShopifyAPI::Session.temp(
domain: shop.shopify_domain,
token: shop.shopify_token,
api_version: ShopifyApp.configuration.api_version
) do
client = ShopifyAPI::GraphQL.client
ql_query = <<-GRAPHQL
{
products(first: 10, query: "title:*#{query}*") {
edges {
node {
id
title
handle
}
}
}
}
GRAPHQL
query_result = client.query(client.parse(ql_query))
query_result.data.products.edges.each do |result|
#responses << {
id: result.node.id,
title: result.node.title,
handle: result.node.handle
}
end
end
#responses
I have the following query hooked up to a rails app. It only seems to be returning 30 projects at a time, even with an increased per_page count. However, if I make the same request via the browser, I get the expected number of projects.
https://api.github.com/search/repositories?q=license:gpl+license:lgpl+license:gpl+license:mit+forks:0+stars:1..2000+pushed:%3E2019-12-11+language:ruby+language:javascript&per_page=100
controller:
def request_projects
api_key = Rails.application.credentials.dig(:github)
query = "#{Project::LICENSES}+#{Project::FORKS}+#{Project::STARS}+#{Project::DATE}+#{Project::LANGUAGES}"
uri = URI.parse("#{Project::GITHUB_BASE_URL}?q=#{query}&client_secret=#{api_key[:secret]}&per_page=100")
res = Net::HTTP.get_response(uri)
Project.create_from_request(res.body)
end
Model:
def self.create_from_request(request_body)
data = JSON.parse(request_body, symbolize_names: true)
data[:items].each do |item|
name = item[:name]
owner = item[:owner][:login]
url = item[:html_url]
stars = item[:stargazers_count]
Project.create(name: name, owner: owner, url: url, stars: stars) if !Project.exists?(name: name, owner: owner)
end
end
I have an map that I have to assemble before using...
mailList = Customers.map do |customer|
{
:username => customer.name,
:email => customer.email,
:last_ip_v4 => customer.ipv4,
:last_ip_v6 => customer.ipv6
}
end
This works for a number of test users, but not all users have a last ipv4 AND a last ipv6. When they don't, ruby errors out, but I'd rather it just assign a nil. How do I do that?
If you are sure that this line,
mailList = Customers.map do |customer|
will work fine (may be incase of using a wrong convention), then try to check the nil cases for ipv4 and ipv6 and construct the list as,
mailList = construct_list Customers
def construct_list data
list = []
hash ={}
data.map do |customer|
hash[:username] = customer.name
hash[:email] = customer.email
customer.ipv4.nil? ? hash[:last_ip_v4] = nil : hash[:last_ip_v4] = customer.ipv4
customer.ipv6.nil? ? hash[:last_ip_v6] = nil : hash[:last_ip_v6] = customer.ipv6
list << hash
end
list
end
Though it may not be optimal, it will help you for a while.
Even if ipv4 and ipv6 are nil, it should not throw error. I think the syntax here is incorrect or Customers is nil. Try this way:
mailList = Customer.all.map do |customer|
{
username: customer.name,
email: customer.email,
last_ip_v4: customer.ipv4,
last_ip_v6: customer.ipv6
}
end
I have managed to connect my web service to the database, but now whenever I make a request it returns nothing. The database has a couple of rows, but the web service returns zero.
get '/all_users/' do
conn = TinyTds::Client.new(username: 'nicole', password: 'pass', dataserver: 'Nikki-PC\Mydatabase', database: 'Thedatabase')
recordsArray = "{\"clientList\":["
clientArray = Array.new
sql = 'select * from dbo.ServerUsers'
records = conn.execute(sql) do |record|
client = AndroidtableClientsSearch.new(record[0], record[1], record[2], record[3], record[4])
clientArray << client.to_s
end
recordsArray << clientArray.join(',')
recordsArray << "]}"
recordsArray
end
I'm pretty sure I am doing the execute, but this is the first time I am using tiny_tds and I am very confused.
Thank you for your help.
[EDIT]
This is AndroidClientsSearch:
class AndroidtableClientsSearch
def initialize(username, password, phone_number, profile_state, clasa)
#username = username
#password = password
#phone_number = phone_number
#profile_state = profile_state
#clasa = clasa
end
def to_s
{ :username => "#{#username}", :password => "#{#password}", :phone_number => "#{#phone_number}", :profile_state => "#{#profile_state}", :clasa =>"#{#clasa}"}.to_json
end
end
[UPDATE]
I have modified the code as suggested and it returns a result, but it does not return the data from the database.
This is a result:
{"recordsArray":["{\"username\":\"\",\"password\":\"\",\"phone_number\":\"\",\"profile_state\":\"\",\"clasa\":\"\"}"]}
conn.execute(sql) does not accept a block, it simply returns a result. The proc afterwards is treated by a ruby interpreter as “orphan proc definition” and never gets executed. You might try to put puts 'I am here' inside it and see it is never called.
The solution would be to iterate the result:
get '/all_users/' do
conn = TinyTds::Client.new(...)
sql = 'select * from dbo.ServerUsers'
# ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓ iterate!!!
records = conn.execute(sql).each_with_object([]) do |record, memo|
client = AndroidtableClientsSearch.new(*5.times.map { |i| record[i] })
memo << client.to_s
end
require 'json'
JSON.dump(clientList: records)
end
first_user = User.first
last_user = User.last
How to add two relation object ?
This does not work.
my_users = first_user + last_user
I can use array and push the first and last user.
But pagination will not work for array
users = my_users.paginate# (using will_paginate)
But this works
users = User.limit(2).paginate
This will work, but makes at least three SQL calls:
first_user = User.first
last_user = User.last
users = User.where(id: [first_user.id, last_user.id]).paginate
You cannot add two objects,it should be in array format always then only you can do pagination using will paginate.
you have to follow the below step
user_all = []
first_user = User.first
last_user = User.last
user_all << first_user
user_all << last_user
users = user_all.paginate