How do i pass parameters in Mogli - ruby-on-rails

Using Mogli and facebooker gem, how do i get my wall data with a limit of 1000?
using this i get whole posts
def index
redirect_to new_oauth_path and return unless session[:at]
user = Mogli::User.find("me",Mogli::Client.new(session[:at]))
#user = user
#posts = user.posts
end
but i need to filter it with limit 1000

Seems that the only way is to set limit parameter in the client
client = Mogli::Client.new(session[:at])
client.default_params[:limit] = 1000
user = Mogli::User.find("me", client)

Related

Rails Remove Model from ActiveRecord::Relation Query

What's the best way to dynamically remove a model from a query? Basically I want to find all campaigns where a user hasn't already provided a response.
The below method delete_at actually deletes the model which isn't what I want. I only want it remove from the local 'campaigns' ActiveRecord::Relation query set that I got.
def self.appuser_campaigns appuser_id, language
appuser = Appuser.find(appuser_id)
campaigns = Campaign.check_language language
i = -1
campaigns.each do |campaign|
i = i + 1
responses = Response.where(appuser_id: appuser_id, campaign_id: campaign.id)
if responses.length > 0
campaigns.delete_at(i)
end
end
puts campaigns.class.name #"ActiveRecord::Relation"
campaigns
end
def self.check_language language
campaigns = Campaign.where(language: language, status: "In Progress")
end
You can do the following:
already_answered_campaign_ids = Appuser.find(appuser_id).responses.pluck(:campaign_id)
Campaign.where('id NOT IN (?)', already_answered_campaign_ids.presence || -1)

Express Checkout Order Total is Missing

I keep getting error #10400 (Order total is missing) but am not sure what I am leaving out. Everything appears to be processing correctly. This is where the payment is setup:
def setcheckout
api = PayPal::SDK::Merchant::API.new
#set_express_checkout = api.build_set_express_checkout(params[:SetExpressCheckoutRequestType])
# Find Item Total and Order Total
details = #set_express_checkout.SetExpressCheckoutRequestDetails
pay = details.PaymentDetails[0]
pay.PaymentDetailsItem[0].Name = 'Item'
pay.PaymentDetailsItem[0].Amount = 1
pay.PaymentDetailsItem[0].Quantity = 1
pay.ItemTotal = pay.PaymentDetailsItem[0].Amount
pay.OrderTotal.currencyID = pay.ItemTotal.currencyID
pay.OrderTotal.value = pay.ItemTotal.value.to_f
# Notify url
#pay.NotifyURL ||= ipn_notify_url
# Return and cancel url
details.ReturnURL ||= 'http://localhost:3000/confirm'
details.CancelURL ||= 'http://localhost:3000/failed'
#set_express_checkout_response = api.set_express_checkout(#set_express_checkout)
if #set_express_checkout_response.success?
redirect_to "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=#{#set_express_checkout_response.Token}"
end
end
This takes me to paypal, authenticates the user, and returns to the confirmation url as expected. That looks like this:
def confirm
session[:token] = params[:token] if params[:token]
session[:PayerID] = params[:PayerID] if params[:PayerID]
api = PayPal::SDK::Merchant::API.new
#do_express_checkout_payment = api.build_do_express_checkout_payment(params[:DoExpressCheckoutPaymentRequestType])
details = #do_express_checkout_payment.DoExpressCheckoutPaymentRequestDetails
details.Token = session[:token]
details.PayerID = session[:PayerID]
#details.PaymentDetails[0].NotifyURL ||= ipn_notify_url
#do_express_checkout_payment_response = api.do_express_checkout_payment(#do_express_checkout_payment) if request.post?
end
Once the "Confirm and Pay" button is clicked and the above is posted to, the transaction fails with a 10400 Order total is missing. error. It looks to me like I specified the order total above, and the total is displayed when I am taken to paypal. What am I missing?
I don't see the total getting sent in your DoExpressCheckoutPayment request..?? You need to include those same details in DECP that you do in SEC.
As of version 112.0 they introduced the USESESSIONPAYMENTDETAILS parameter, which is supposed to allow to tell DECP to just use what you sent in SEC if you set it to true or 1. There seems to be some discrepancy about whether or not it works, though. I've yet to test it myself.

Ruby on Rails: See if a record exists that matches query on multiple columns

I'm developing an app that allows a user to answer a survey once, when they answer it (the first time) I'm capturing the users ip address and user agent (in the Test_users model).
What I'm trying to do is if the user navigates to the survey again they are redirected to a page telling them that they can only complete it once.
In the test controller, I'm thinking this should be:
def new
#Find the Test
#test = Test.find(params[:test_id])
if [[a record in test_users exists with the test id / ip address / user agent]]
redirect_to already_completed_path
end
end
I'm struggling with what this if statement, any help would be greatly appreciated
You can use the exists? method if you don't need the object for something else:
if Test.exists?(:test_id => params[:test_id], :ip_address => request.remote_ip, :user_agent => request.env['HTTP_USER_AGENT'])
redirect_to already_completed_path
end
Use the where clause and put all your conditions in it:
#test = Test.where("test_id = ? or ip_address = ? or user_agent = ?",
test_id, ip_address, user_agent)
This should work, assuming you are storing request.remote_ip and request.env['HTTP_USER_AGENT'] values in your tests table:
if #test.where(ip: request.remote_ip, user_agent: request.env['HTTP_USER_AGENT']).exists?
redirect_to already_completed_path
end
You Should be doing like this
In your Test Controller
def new
#Find the Test
#test = Test.find(params[:id]) #it should be `:id` not `:test_id` because the model wont be having its own foreign key in its own record
#test_user = TestUser.where("test_id = ? or ip_address = ? or user_agent = ?",
test_id, ip_address, user_agent)
if #test_user.exists?
redirect_to already_completed_path
else
redirect_to new_path

wrong content loaded due to browser cache

I used Ruby on Rails for my website. In one web page, it will load a poll information based on the poll id, which set in url like "http://mywebsite/polls/1". The poll information includes the poll owner name, the poll title, the item pictures in the poll and the people's name who voted on the poll.
I found sometimes it loaded wrong information. That is, it loaded the wrong poll owner name, poll title and voted people from the other poll while the item pictures are correct.I checked the back end and found there was nothing wrong in rails controller. All the variables got the right values. But the chrome browser told me the view is wrong.
If I cleared all the cache and reload the page then it would work normally.Anyone knows why does it happen and what should I do? Thanks
The relavant action codes:
def show
#poll=Poll.where("is_deleted = false AND id = #{params[:id]}")[0]
#items=#poll.items.where("is_deleted = false")
#voted_user_ids = #poll.audiences.where('has_voted != 0').map(&:user_id).uniq
#voted_users = User.where('id IN (?)',#voted_user_ids)
#voted_user_names = #voted_users.map(&:user_name)
if current_user.nil?
#poll_vote_url = "/voted_choice"
else
#current_user_name = current_user.user_name
#poll_vote_url = "/audiences/"+#poll.id.to_s
#if_current_user_voted = #voted_users.include?(current_user)
#is_poll_owner = (current_user == #poll.user)
check_item_id_in_cookies
end
end
def check_item_id_in_cookies
if !cookies.signed[:item_id].nil?
item = Item.find(cookies.signed[:item_id].to_i)
#create audience if the voter is not the poll owner
if current_user == item.poll.user
flash.now[:alert] = "You can't vote on your own poll."
cookies.signed[:item_id] = nil
else
create_audience cookies.signed[:item_id]
end
end
end
def create_audience item_id
#item_id = item_id.to_i
#item = Item.find(#item_id)
#poll = #item.poll
#voted_user_ids = #poll.audiences.where('has_voted != 0').map(&:user_id).uniq
if #voted_user_ids.include?(current_user.id)
flash.now[:alert]="You already voted."
else
audience = #item.poll.audiences.find{|audience| audience.user_id == current_user.id} || Audience.create(:poll_id => #poll.id,:user_id => current_user.id)
#update audience
audience.is_following = true
audience.has_voted = #item.id
audience.save
cookies.signed[:item_id]=nil
flash[:alert]="Thank you for your vote"
redirect_to "/polls/#{#poll.id}"
end
end
Please monitor the network while loading and reloading the page. Especially to request to request to http://mywebsite/polls/1. Check the response headers as well. Even if you don't do on application side the web server or a proxy server may be modifying the request.
You can find help on who to use the network panel of chrome here.

Generate a new user programmatically (Auth Logic)

I'm quite new to Ruby on Rails so please bear with me :)
I'm processing an imported .csv file in Rails and I want to programmatically create new users (I'm using the AuthLogic Gem along with Role Requirement), So Far I'm using:
Example Line:
Steve.Jobs#apple.com, Steve, Jobs, 555-APPLE
Code:
def new_user(line)
params = Hash.new
params[:user] = Hash.new
params[:user]["email"] = line[0]
params[:user]["first_name"] = line[1]
params[:user]["last_name"] = line[3]
params[:user]["phone"] = line[4]
user = User.new(params[:user])
user.save
end
The problem being that this doesn't add a new user, it tries to but fails (DB Begin followed by Rollback), I assume because I'm not filling in all the fields, such as login, password etc.
Do I have to explicitly generate values for these fields?
I came across this same problem yesterday. I'm using the oauth addon though so the login/email are not required fields for me, it was failing on the persistence token not being present which I got around by adding
user.reset_persistence_token
just before calling user.save
Hope that helps a bit. Would be nice to find a cleaner way of doing it.
Ok So I've managed to answer my own question, although not in the most ideal of ways:
def new_user(line)
params = Hash.new
params[:user] = Hash.new
params[:user]["email"] = line[0]
params[:user]["first_name"] = line[1]
params[:user]["last_name"] = line[2]
params[:user]["phone"] = line[3]
#generate random password of length 6
password = ActiveSupport::SecureRandom.base64(6)
#generate username by adding first and last name + 3 random characters
username = (line[1] + line[2])
username = username + ActiveSupport::SecureRandom.base64(3)
params[:user]["login"] = username
params[:user]["password"] = password
params[:user]["password_confirmation"] = password
#check to see if user already exists
#existing_user = User.find_by_email(line[0])
if(#existing_user)
#user exists
#do nothing
else
#user is brand new
#new_user = User.new(params[:user])
#new_user.reset_persistence_token
if(#new_user.save)
#new_user = User.find_by_email(line[0])
#user saved successfully
else
#a problem occurred
flash[:errors] = #new_user.errors
end
end
end

Resources