0

I've written a short Ruby script which creates a nice export of ASANA tasks into csv, however it takes a while to run because I have to do a GET for every task within each project. I found a way of using opt_expand to get all task data at once for each project which make then number a "Get"s a fraction of what it is at the moment. However the opt_expand code which works in curl does not work in Ruby, it just ignores the expand command.

Any help would be greatly appreciated,

Normal curl code[snippet1]:

curl -u <token>: https://app.asana.com/api/1.0/projects/<project_id>/tasks

Working opt_expand curl code[snippet2]:

curl -u <token>: https://app.asana.com/api/1.0/projects/<project_id>/tasks?opt_expand=.

Normal Ruby code[snippet3]:

uri = URI.parse("https://app.asana.com/api/1.0/projects/<project_id>")

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
...

Broken Ruby code which returns the same as snippet 3 despite using opt_expand

uri = URI.parse"(https://app.asana.com/api/1.0/projects/<project_id>/tasks?opt_expand=.
")

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
...
  • I'm guessing you are making the request in such a way as to not send the URL parameter. The key is what you are doing in the `...` in your code snippets. Can you provide detail there? – Greg S Jun 20 '12 at 15:38

2 Answers2

0

It's a bit hard to answer without seeing the error message (or server return message) you're getting.

However, keep in mind that Net::HTTP is low level, and might be a bit of an overkill to use for such a simple task. Did you consider using other libraries (i.e: rest-client or Faraday) which will be easier for you to work with. For example:

require 'rest_client'

response = RestClient.get "https://app.asana.com/api/1.0/projects/<project_id>"
if response.code == 200
  # process answer
end
Amir
  • 1,882
  • 17
  • 22
  • Hi, thanks for that, you're exactly right I was over complicating the task and have looked through it was that I was simply not sending the query, just the path. Here's what I changed: req = Net::HTTP::Get.new(uri.path, header) req = Net::HTTP::Get.new(urip, header) – user1468963 Jun 20 '12 at 15:35
  • where urip = uri.path + '?' + uri.query THANKS! – user1468963 Jun 20 '12 at 15:42
0

Like Greg said, you are not passing the params to server. Ruby URI parse doesn't take in params. Try something like this:

params = { :opt_expand => 'your opt here' }
uri = URI.parse("https://app.asana.com/api/1.0/projects/<project_id>/tasks")
http = Net::HTTP.new(uri.host, uri.port)
...
uri_with_params = "#{uri.path}?".concat(params.collect { |k,v| "#{k}=#{CGI::escape(v.to_s)}" }.join('&')) if not params.nil?
req = Net::HTTP::Get.new(uri_with_params, header)
req.basic_auth(key, password)
res = http.start { |http| http.request(req) }
Phuoc Do
  • 1,344
  • 10
  • 14