Rails unable to generate instance variable in class initialize method to generate httparty request

Multi tool use
Rails unable to generate instance variable in class initialize method to generate httparty request
I'm having issues generating a Signature
via a Ruby class. When I go into my docker container, I'm able to see that all the instance variables in the initialize
method are nil
expect the @api_key
variable.
Signature
initialize
nil
@api_key
I have the following class
require 'openssl'
require 'base64'
module SeamlessGov
class Form
include HTTParty
attr_accessor :form_id
base_uri "https://nycopp.seamlessdocs.com/api"
def initialize()
@api_key = ENV['SEAMLESS_GOV_API_KEY']
@signature = generate_signature
@form_id = ENV['SEAMLESS_GOV_FORM_ID']
@timestamp = Time.now.to_i.to_s
end
def relative_uri
"/form/#{@form_id}/elements"
end
def create_form
self.class.get(relative_uri, headers: generate_headers)
end
private
def generate_signature
OpenSSL::HMAC.hexdigest('sha256', ENV['SEAMLESS_GOV_SECRET'], "GET+#{relative_uri}+#{@timestamp}")
binding.pry
end
def generate_headers
{
"Authorization" => "HMAC-SHA256 api_key='#{@api_key}' signature='#{@timestamp}'",
Net::HTTP::ImmutableHeaderKey.new('AuthDate') => "#{@timestamp}"
}
end
end
end
As you see, from the binding.pry
in the generate_signature
method I'm able to see the instance variables:
binding.pry
generate_signature
The relative_uri
method needed to generate the signature doesn't load the @form_id
variable in the string.
relative_uri
@form_id
Here is the controller:
class FormsController < ApplicationController
def display_form
@form = SeamlessGov::Form.new().create_form
end
end
If I call create_form
this is the output:
create_form
{"error"=>true,
"error_log"=>
[{"error_code"=>"missing_date_headers",
"error_message"=>"Request is missing date headers",
"error_description"=>
"{"Host":"nycopp.seamlessdocs.com","Connection":"close","X-Real-IP":"71.249.243.7","X-Forwarded-For":"71.249.243.7","X-Forwarded-Host":"nycopp.seamlessdocs.com","X-Forwarded-Port":"443","X-Forwarded-Proto":"https","X-Original-URI":"/api/form//elements","X-Scheme":"https","Authorization":"HMAC-SHA256 api_key='h123xxxxxxxxxx' signature=''","AuthDate":""}"},
{"error_code"=>"external_auth_error", "error_message"=>"Date header is missing or timestamp out of bounds"}]}
What is the issue?
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
"except the @api_key variable" - impossible. At least timestamp must be non-nil too. I don't know what nils it out, it's not in the code you showed us.
– Sergio Tulentsev
3 mins ago