How To Search a User in Discourse Using Ruby - ruby-on-rails

I am trying to find and delete unwanted user in my discourse using ruby
puts "Search Member you want to Delete"
puts "Search By E-Mail"
usermail = gets
puts client.search(usermail)
in result it takes me to discourse page
is there any possible ways to search and find the exact user by email.
thanks for your nice and helpful comments

i think we cannot search users by email and we can just use the following for geting users list
require 'rest-client'
RestClient.get 'http://example.com/resource', {params: {id: 50, 'foo' => 'bar'}}

we can get a user by name using Http gem
require 'http'
username = 'username'
response = HTTP.get("https://discourse.example.com/user/#{username}.jason?api_key=Your_Discourse_api_key")
puts response.body.to_s
it worked for me

Related

Send sender name with email SendGrid - Rails

I am using SendGrid to send emails from my application. I want to send sender name along with sender email e.g. from: 'Maxcreet <contact#maxcreet.com>'
It is working fine using Letter Opener and MailTrap but SendGrid only show sender email.
Here is my SendGrid functionto send emails.
def deliver!(mail)
email.from = SendGrid::Email.new(email: mail[:from].addrs.first.address)
email.subject = mail.subject
email.add_personalization(build_personalization(mail))
build_content(mail)
send!(email)
end
I have checked mail[:from] values using puts it gives following values:
puts mail[:from] => Maxcreet <support#maxcreet.com>
puts mail[:from].addrs => Maxcreet <support#maxcreet.com>
puts mail[:from].addrs.first => Maxcreet <support#maxcreet.com>
puts mail[:from].addrs.first.address => support#maxcreet.com
Above 3 seems OK for me but when I use any of them in
email.from = SendGrid::Email.new(email: mail[:from].addrs.first.address)
It does not sent my email and even I do not find my email in sendgrid dashboard.
Following this also tried email.fromname but this even did not work.
SendGrid's ruby API has this option with name name and not fromname.
So the following should solve your problem.
email.from = SendGrid::Email.new(
email: mail[:from].addrs.first.address,
name: mail[:from].addrs.first.name
)
I concluded this by trying it from this doc.
It looks like you're mixing gems or objects. SendGrid::Email.new just needs a from String, and might support a name String. You're extracting the address from your mail[:from] Hash with mail[:from].addrs.first.address, but you need to provide the name as a distinct Key.
In the SendGrid Ruby gem 'kitchen sink" example, they don't show a name on the From argument, but they do on the personalization.
Try: email.from = SendGrid::Email.new(email: 'support#maxcreet.com', name: 'Maxcreet'))
or dynamically: email.from = SendGrid::Email.new(email: mail[:from].addrs.first.address, name: mail[:from].addrs.first.name))
if your mail Object recognizes that Key.
Otherwise, you'll need to look at your mail gem's documentation for how to extract a Friendly/Display Name from that mail[:from].addrs Object.

How to get user posts Insights from Facebook API Koala Gem

Hi I am wondering how to get user posts insights from Facebook API Koala Gem.
I only found solutions that works for facebook page posts but not user posts.
I used the code below for user posts but it just returns empty array.
#graph.get_connections('me', 'insights', metric: 'page_impressions', period: 'now')
UPDATE
user = Authentication.where(user_id: current_user.id, provider: "facebook").first
oauth_access_token = user.token
#graph = Koala::Facebook::API.new(oauth_access_token)
#posts = #graph.get_connection('me', 'posts',{ fields: ['id', 'message', 'link', 'name', 'description', "likes.summary(true)", "shares", "comments.summary(true)"]})
The code above works fine, but when I try to get post insights, it returns empty array.
If your using omniauth-facebook gem you just have to make sure you have the right permissions in your scope and you can use the original query.
config/initializers/omniauth.rb
OmniAuth.config.logger = Rails.logger
Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, {id}, {secret},
:scope => 'email,manage_pages,read_stream,read_insights'
end
Also, you can get post insights for a page via koala. This worked for me.
m = Koala::Facebook::API.new(User.find(5).oauth_token)
m = m.get_connections('me', 'accounts')
m = m.first['access_token']
#post_graph = Koala::Facebook::API.new(m)
#feed = #post_graph.get_connection('me', 'feed')
#postid = #feed.first['id']
#post_data = #post_graph.get_connections(#postid, 'likes', since: "2015-05-17", until: "2015-07-17")
https://github.com/arsduo/koala/wiki/Acting-as-a-Page
https://developers.facebook.com/docs/graph-api/reference/v2.3/post
If you check here at 'Edges' you can see that /insights are available only for Pages.
'/insights Insights for this post (only for Pages).'
I hope I am right and helped you.
Here you can see it is only for pages post
/{post-id}/insights (where this is a Page post)

How to troubleshoot quickbooks-ruby

I have a Rails app set up with the quickbooks-ruby gem.
Oauth seems to be working—there are no errors, I follow the Quickbooks login in a pop-up, and I get a confirmation that the account is connected. No errors.
However, in my audit log on Quickbooks Online, there is not indication that I have logged in.
And when I follow the steps to try to get a list of customers, I get this error:
undefined method 'get' for "qyprd...":String
Which may or may not be a different problem.
This is the code:
def index
#customers = Customer.all
service = Quickbooks::Service::Customer.new
service.company_id = session[:realm_id]
service.access_token = session[:token]
customers = service.query()
end
I don't understand why it isn't working, or how to troubleshoot the issue.
From the above code I see you are not passing the query in last but one line. Query should look something like this i.e customers.query("Select Id, GivenName From Customer")
To understand why it isn't working and to troubleshoot the issue you can use begin and rescue.
begin
oauth_client = OAuth::AccessToken.new($qb_oauth_consumer, token, secret)
service = Quickbooks::Service::Customer.new(:access_token => oauth_client, :company_id => realm_id)
customers = customers.query("Select Id, GivenName From Customer")
rescue Exception => e
# e.message will show response of quickbooks for you request
puts e.message
# e.message.inspect will show logs
puts e.backtrace.inspect
end
You can also try your queries on API explorer and see if get the result set you want.

"campaing_id" is not a symbol error when using mail chimp api with gibbon

Context:
I have a rails application. I have a form where users can enter their email list to be a part of the news letter.
I am using mail chimp and gibbon gem for rails.
I can successfully add new subscribers to a list using the gibbon api
I can successfully create a new campaign as shown in the code below.
In my gem file :
gem 'gibbon', git: 'git://github.com/amro/gibbon.git'
For testing out the functionality, I invoke the create action in CampaignsController(code below).
The campaign is created successfully and I get the result back from the api, but in the next step the app crashes with the following error message :
Problem:
TypeError in CampaignsController#create
["xyz"] is not a symbol where xyz is the campaign_id of the newly created campaign.
Code:
In CampaignsController
def create
mailchimp = Gibbon::API.new(Rails.application.secrets.mailchimp_api_key)
new_campaign = mailchimp.campaigns.create({:type=>"plaintext",
:options=>
{:list_id=>"abcdefgh",
:subject=>"Hello World",:from_email => "xyz#gmail.com",
:from_name => "abc",:to_name =>"Programmer"},
:content =>
{:text => "Hello remote programmers I hope you find this mail."}
})
cid = new_campaign["id"]
mailchimp.campaigns.send(cid)
end
It looks like mailchimp.campaigns.send is looking for a hash of options, not just an id.
From their specs:
expect(#gibbon.campaigns.send({"cid" => "1234567"})).to eq({"cid" => "1234567"})

Error Creating Shopify Webhook through the API

I am trying to create a Webhook through the API.
When the Customer installs the app (Controller):
def init_webhooks
topics = ["products/create", "products/update", "products/delete"]
topics.each do |topic|
webhook = ShopifyAPI::Webhook.create(format: "json", topic: topic, address: "http://#{#current_host}/webhooks/#{topic}")
raise "Webhook invalid: (#{topic}) #{webhook.errors}" unless webhook.valid?
end
end
Here is the error from the log:
RuntimeError (Webhook invalid: (products/create) #<ActiveResource::Errors:0x00000003bd7358>):
EDIT:
I have even tried just creating one webhook without the block code like so:
webhook = ShopifyAPI::Webhook.create topic: "products/create", address: "http://myapp.com/webhooks/products/create", format: "json"
But I get the same thing.
From my Routes file:
match 'webhooks/products/create' => 'webhook#product_new'
match 'webhooks/products/update' => 'webhook#product_updated'
match 'webhooks/products/delete' => 'webhook#product_deleted'
I know that the authorization and shop is in fact installing correctly because if I Comment out the 'Raise' error line, I then proceed to the index page which displays some test orders and test products that I created within the Shopify Admin.
I'm not sure where to go from here. Thanks
b
The params you use (topic,format,address) look good to me, but shouldn't it be ShopifyAPI::Webhook.new instead of create?
Did you now there is a shopify console where you can easily test your ruby code?

Resources