Doorkeeper - Hitting Authorize Results in Error auth = nil - ruby-on-rails

When hitting the "Authorize" button, I get "Called id for nil"
Here is the line where the problem is occurring in my authorizations controller:
46 auth = authorization.authorize
Auth is coming up nil on the error page:
Local Variables
auth nil
Here are my request parameters:
{"utf8"=>"✓", "authenticity_token"=>"0m07v0jefxjeMOMt7U5rjRMJj0qhA27nBsamwKvHSMw=",
"client_id"=>"159c4b355fdef4a8e2887734f2f95af42d6b1199ddf94bb8a0fffb10bd3de0c4",
"redirect_uri"=>"http://localhost:3001/users/auth/provider/callback",
"state"=>"3b654057f568f5f542bfea92f07c9403fcfdc80a7a3208e4", "response_type"=>"code",
"scope"=>"public_read", "commit"=>"Authorize", "action"=>"create",
"controller"=>"oauth/authorizations"}
Also, here is what the #pre_auth variable contains when viewing the Authorize/Deny Form:
#<Doorkeeper::OAuth::PreAuthorization:0x007fc83c8d7a28 #server=
<Doorkeeper::Config:0x007fc83c0a3640 #orm=:active_record, #authenticate_resource_owner=
<Proc:0x007fc83c0a34d8#/Users/austen/sites/provider/config/initializers/doorkeeper.rb:7>,
#access_token_expires_in=nil, #enable_application_owner=true,
#confirm_application_owner=true, #default_scopes=public_read,
#optional_scopes=public_write private_read private_write, #scopes=public_read public_write
private_read private_write>, #client=#<Doorkeeper::OAuth::Client:0x007fc83c8d7a78
#application=#<Doorkeeper::Application id: 1, name: "WhiteDeals", uid:
"159c4b355fdef4a8e2887734f2f95af42d6b1199ddf94bb8a0f...", secret:
"d21e16cc50873d60a3f3aacf421c84766e9c183ea7d989f70e6...", redirect_uri:
"http://localhost:3001/users/auth/provider/callback", created_at: "2013-03-22 02:15:55",
updated_at: "2013-03-23 06:33:29", owner_id: 1, owner_type: "User">>,
#response_type="code", #redirect_uri="http://localhost:3001/users/auth/servant/callback",
#scope=nil, #state="3b654057f568f5f542bfea92f07c9403fcfdc80a7a3208e4", #error=nil>

Related

Assigning a hash as association to an object fails if the hash has id, need to understaand why

I have a hash with id which I want to assign as association to an object, I get this error
{"amount"=>nil, "base_currency_amount"=>nil, "currency_id"=>1, "deal_product_id"=>nil,
"expected_close"=>"2021-06-02", "id"=>3, "name"=>"test", "product_id"=>nil}
(byebug) new_record.deal = current_properties["deal"]
*** ActiveRecord::RecordNotFound Exception: Couldn't find LeadDeal with ID=3 for Lead
with ID=
nil
But when I remove the ids from the hash, it works
(byebug) new_record.deal = current_properties["deal"].except("id")
{"amount"=>nil, "base_currency_amount"=>nil, "currency_id"=>1, "deal_product_id"=>nil, "expected_close"=>"2021-06-02", "name"=>"test", "product_id"=>nil}
(byebug) new_record.deal
#<LeadDeal id: nil, account_id: 1623836761, name: "test", amount: nil, expected_close: "2021-06-02", created_at: nil, updated_at: nil, deal_product_id: nil, currency_id: 1, base_currency_amount: nil, product_id: nil>
I thought it's because the new_record is missing id, but even if I add the id it fails with this error
(byebug) new_record.id = 3
3
(byebug) new_record.company = current_properties["company"]
*** ActiveRecord::RecordNotFound Exception: Couldn't find LeadCompany with ID=3 for Lead with ID=3
nil
all the values are present in db, and it works as expected if I just remove ids from the hash, I want to know why this happens

RoR - Capsens/universign - SignatureField - The coordinates aren't working

I'm trying to set a transaction to Universign with the "capsens_universign" gem.
Here's my code to create the transaction:
document_from_content = Universign::Document.new(
name: 'another.pdf',
content: File.open("tmp/test.pdf").read
)
signer = Universign::TransactionSigner.new(
first_name: "Signer's first name",
last_name: "Signer's last name",
email: 'test#gmail.com',
phone_number: 'SOME_PHONE_NUMBER',
success_url: 'https://my_app.com',
signature: Universign::SignatureField.new(coordinate: [37,684], page: 5)
)
transaction = Universign::Transaction.create(
documents: [document_from_content],
signers: [signer],
options: { profile: 'default', final_doc_sent: false, handwritten_signature_mode: 0}
)
I got a code 200 response from this. I can sign the pdf BUT signature stamps are not showing in the PDF generated by Universign when the transaction is completed.
Is it because of this configuration ? Or maybe I didn't understand well the different signatures configurations.

Rails: why can I send a single record attribute via controller to new record but not array?

Creating articles via the controller in Rails. A simple method, which more or less works; just call the method from some other place and it generates a new article via the back end and fills in the values:
def test_create_briefing
a = Article.new
a.type_id = 27
a.status = 'published'
a.headline = 'This is a headline'
a.lede = 'Our article is about some interesting topic.'
a.body = test_article_text
a.save!
end
If test_article_text is just a single record, this works fine and prints the existing article body into the new article body. Looks right in the view and looks right in "edit". All perfect.
def test_article_text
a = Article.find_by_id(181)
a.body
end
But if I try to do the same thing with the last ten articles, it doesn't work:
def test_article_text
Article.lastten.each do |a|
a.body
end
end
In the view you get:
[#, #, #, #, #, #, #, #, #, #]
And in "edit" you get:
[#<Article id: 357, headline: "This is a headline", lede: "Our article is about some interesting topic.", body: "[#<Article id: 356, headline: \"This is a headline\"...", created_at: "2017-12-31 20:40:16", updated_at: "2017-12-31 20:40:16", type_id: 27, urgency: nil, main: nil, status: "published", caption: nil, source: nil, video: nil, summary: nil, summary_slug: nil, topstory: false, email_to: nil, notification_slug: nil, notification_message: nil, short_lede: nil, short_headline: nil, is_free: nil, briefing_point: nil>, #<Article id: 356, headline: "This is a headline"…etc, etc, etc.
What do I not know? What am I missing?
It is returned as below because the Article.lastten is the returned variable from your controller.
[#<Article id: 357, headline: "This is a headline", lede: "Our article is about some interesting topic.", body: "[#<Article id: 356, headline: \"This is a headline\"...", created_at: "2017-12-31 20:40:16", updated_at: "2017-12-31 20:40:16", type_id: 27, urgency: nil, main: nil, status: "published", caption: nil, source: nil, video: nil, summary: nil, summary_slug: nil, topstory: false, email_to: nil, notification_slug: nil, notification_message: nil, short_lede: nil, short_headline: nil, is_free: nil, briefing_point: nil>, #<Article id: 356, headline: "This is a headline"…etc, etc, etc.
To return all Article body, do as below:
def test_article_text
arr = Array.new
Article.lastten.each do |a|
arr << a.body
end
arr # should be added so it will be the last value returned from your controller
end
So, #Shiko was nearly right, certainly on the right path. Had to manipulate the array a bit and do two things to get it to work:
.join the bits of the array to strip out all of the rubbish;
Concatenate the different bits for each article in a different way than you normally do in a view. So to_sfor each of the attributes, concatenating things "" + "" and rebuilding the url with information available in the array (no link_to, etc.).
The "**"is markdown, because I'm using that, but I suppose you could bung html tags in there if you needed to.
This works:
def test_article_text
arr = Array.new
Article.lastten.each do |a|
arr << "**" + a.headline.to_s + "**: " + a.text.to_s + "[Read now](/articles/#{a.id}-#{a.created_at.strftime("%y%m%d%H%M%S")}-#{a.headline.parameterize})"
end
arr.join("\n\n")
end

How can I fix this unexpected token (JSON parser error)

I am trying to save data from an API to my database, but I'm running into an error. The error states: unexpected token at 'object' (JSON::ParserError)
Here is my code:
require 'rest-client'
require 'pp'
endpoint = "https://api.leaddyno.com/v1/leads"
class TestDyno
def parser(page_number)
## API key
request = {:params => {:key => Rails.application.secrets.LEADDYNO_PRIVATE, page: page_number}}
## Parse JSON
response = JSON.parse(RestClient.get endpoint, request)
#response_count = response.count # Count results on the page.
pp response
puts response_count
end
def data
until #response_count == 0
1.upto(5) do |page_number|
response['object'].each do |item|
LeaddynoLead.save(
leaddyno_lead_id: item['id'],
email: item['email'],
first_name: item['first_name'],
last_name: item['last_name'],
latest_visitor_id: item['latest_visitor']['id'],
latest_visitor_code: item['latest_visitor']['tracking_code'],
url: item['url']['url'],
referrer_id: item['referrer']['id'],
referrer_url: item['referrer']['url'],
leaddyno_affiliate_id: item['affiliate']['id'],
leaddyno_affiliate_email: item['affiliate']['email'],
search_term: item['search_term']['term'],
search_engine: item['search_term']['search_engine'],
leaddyno_tracking_code: item['tracking_code'],
created_at: item['created_at'],
updated_at: item['updated_at']
)
sleep 0.5
end
end
end
end
end
retrieve = TestDyno.new
retrieve.data
I guess I need another set of eyes to look at this and see what is wrong?
The API docs are here if that helps. Thanks.

fabricated models not the same as on disk

I'm probably misunderstanding something here.
I have a model Secondant that I create with Fabrication in my model rspec.
main_user = Fabricate :user, email: TESTEMAIL
sec = Fabricate :secondant, email: SECEMAIL, user_id: main_user.id
sec_user = Fabricate :user, email: SECEMAIL
ActionMailer::Base.deliveries = []
debugger
At this point when I look at the value of secondant_id in the sec model, the attribute is empty (it get's filled in a after_create callback). When i retrieve the model just created from the database that attribute is filled. Why are those two not in sync?
27 main_user = Fabricate :user, email: TESTEMAIL
28 sec = Fabricate :secondant, email: SECEMAIL, user_id: main_user.id
29 sec_user = Fabricate :user, email: SECEMAIL
30 ActionMailer::Base.deliveries = []
31 debugger
=> 32 sec.destroy
33 end
34
35 it 'should have a secondant_id assigned' do
36 sec.secondant_id.should_not be_nil
(rdb:1) e sec
#<Secondant id: 519, user_id: 1095, email: "secondant#hotmail.com", secondant_id: nil, created_at: "2013-10-10 13:13:29", updated_at: "2013-10-10 13:13:29", reported: false>
(rdb:1) e Secondant.where(id: sec.id).first
#<Secondant id: 519, user_id: 1095, email: "secondant#hotmail.com", secondant_id: 1096, created_at: "2013-10-10 13:13:29", updated_at: "2013-10-10 13:13:29", reported: false>
My after_create callback:
def find_user
user = User.where(email: self.email).first
if user
# create the link to the user
self.secondant_id = user.id
self.save
# see if this is the second one
if Secondant.where('user_id = ? and secondant_id is not null', user_id).count == 2
user.do_somthing
end
end
return
end
EDIT
There is a similar callback in the user class, which is firing in this case (thanks Peter)
def find_secondant
Secondant.where(email: email).find_each do |sec|
sec.secondant_id = id
sec.save
end
end
At the time you create sec, the user with the identical email has not been created, so your after_save callback should not be setting secondant_id.
I can only assume that your find_user method is getting invoked as a result of the User creation or the where operation you are executing in the debugger, resulting in the secondant_id field being set at that time. It won't be reflected in sec unless/until you do a reload, as the Ruby object created by where is distinct from the sec Ruby object.

Resources