Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert curl (with --data-urlencode) to ruby

I am trying to convert the following curl command to ruby using net/http but I haven't figured out how to pass in the --data-urlencode script@files/jql/events.js part of the command.

curl https://mixpanel.com/api/2.0/jql -u <apikey>: --data-urlencode script@files/jql/events.js

Using net/http I had the following...

uri = URI.parse("https://mixpanel.com/api/2.0/jql")
request = Net::HTTP::Get.new(uri)
request.basic_auth("<apikey>", "")

response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
http.request(request)
end

Is there anyway to do this? If not within net/http then maybe using another gem?

like image 763
Manpreet Komal Avatar asked Sep 14 '25 05:09

Manpreet Komal


1 Answers

Mixpanel has it's official ruby gem I didn't actually work with it, but I assume it have all needed methods.

But if you don't like to use it, you may use Faraday an awesome HTTP client library for Ruby.

I made a simple example with it. Please have a look:

class MixpanelClient
  def initialize(url = "https://mixpanel.com/api/2.0/jql", api_key = "ce08d087255d5ceec741819a57174ce5")
    @url = url
    @api_key = api_key
  end

  def query_data
    File.read("#{Rails.root}/lib/qry.js")
  end

  def query_params
    '{"from_date": "2016-01-01", "to_date": "2016-01-07"}'
  end

  def get_events
    resp = Faraday.new(url: @url, ssl: { verify: false }) do |faraday|
      faraday.request  :url_encoded
      faraday.response :logger
      faraday.adapter  Faraday.default_adapter
      faraday.basic_auth(@api_key, "")
    end.get do |req|
      req.params['script'] = query_data
      req.params['params'] = query_params
    end

    raise MixpanelError.new("Mixpanel error") unless resp.status == 200
    JSON.parse(resp.body)
  end
end

class MixpanelError < StandardError; end

Here is the result:

[1] pry(main)> m = MixpanelClient.new
=> #<MixpanelClient:0x007fc1442d53b8 @api_key="ce08d087255d5ceec741819a57174ce5", @url="https://mixpanel.com/api/2.0/jql">
[2] pry(main)> m.get_events
I, [2016-06-09T09:05:51.741825 #36920]  INFO -- : get https://mixpanel.com/api/2.0/jql?params=%7B%22from_date%22%3A+%222016-01-01%22%2C+%22to_date%22%3A+%222016-01-07%22%7D&script=function+main%28%29%7B+return+Events%28params%29.groupBy%28%5B%22name%22%5D%2C+mixpanel.reducer.count%28%29%29+%7D
D, [2016-06-09T09:05:51.741912 #36920] DEBUG -- request: Authorization: "Basic Y2UwOGQwODcyNTVkNWNlZWM3NDE4MTlhNTcxNzRjZTU6"
User-Agent: "Faraday v0.9.2"
I, [2016-06-09T09:05:52.773172 #36920]  INFO -- Status: 200
D, [2016-06-09T09:05:52.773245 #36920] DEBUG -- response: server: "nginx/1.9.12"
date: "Thu, 09 Jun 2016 03:05:52 GMT"
content-type: "application/json"
transfer-encoding: "chunked"
connection: "close"
vary: "Accept-Encoding"
cache-control: "no-cache, no-store"
access-control-allow-methods: "GET, POST, OPTIONS"
access-control-allow-headers: "X-PINGOTHER,Content-Type,MaxDataServiceVersion,DataServiceVersion,Authorization,X-Requested-With,If-Modified-Since"
=> [{"key"=>["Change Plan"], "value"=>186}, {"key"=>["View Blog"], "value"=>278}, {"key"=>["View Landing Page"], "value"=>1088}, {"key"=>["login"], "value"=>1241}, {"key"=>["purchase"], "value"=>359}, {"key"=>["signup"], "value"=>116}]

A set ssl: {verufy: false} because Faraday need addtitional workaround to work with ssl certificates: https://github.com/lostisland/faraday/wiki/Setting-up-SSL-certificates

like image 97
retgoat Avatar answered Sep 15 '25 18:09

retgoat