I'm working on a small program to get from and post to several resources for an API. The current version of my program works fine but isn't very legible, so now I'm trying to refactor my code in order to get a better separation of concerns (prep the data, send the data, logging, etc). But now I'm stuck.
I've figured out a way to send for a method ( api_call ) from within another ( send_data ) method (using send), that also feeds into a logger. This seems like a nice seperation of concerns. However, I can't figure out how to apply the necessary parameters to the method I'm sending for.
I've tried following a few other stackoverflow questions and tutorials related to send and params, but I can't seem to figure how to do it correctly.
If I don't include the params, I obviously get a "0 for n" error. If I try including them into the send, I get an error telling me it doesn't expect parameters.
What would be a good way to send for the api_method from within send_data , whilst allowing me to variably set the params?
Should I perhaps set the params in an array, and *splat that array as params? I'm not entirely sure how I'd do that.
Is this even a smart way of approaching this? I'm thinking I might as well just create some more methods for different resources, which inherit from "api_call", so I can get rid of most params? But that doesn't seem very DRY?
Here's a simplified example of my code:
class ApiConnector
def send_data(method_name)
begin
@attempts += 1
puts "#{@attempts}th attempt"
send(method_name) # (how) do I set params here?
rescue Errno::ETIMEDOUT => e
retry if @attempts < 3
end
end
def api_call(endpoint_URL: , resource: 'cases' , action: nil , method: 'get', uuid: nil)
request = Typhoeus::Request.new(
"#{endpoint_URL}/api/v1/#{resource}/#{uuid}/#{action}",
verbose: true,
method: :post,
headers: { 'Content-Type' => "multipart/form-data", "API-key" => "123", "API-Interface-ID" => "123", "User-Agent" => "AGENT" }
)
request.run
end
end
Any referrals to relevant documentation are obviously also welcome. Much obliged.
api_call? Are they all positional? A mix of both?send_datacallapi_callor vice versa? If so, where does that call happen? What kind of "params" do you want to pass and where do these "params" come from? It would certainly help to see the (maybe simplified) current version of your program, i.e. working code.