What options does the asana api expect in tasks.find_by_id - ruby

I'm using the asana gem to access the asana api.
The client documentation for the class method find_by_id exposed on the tasks resource (i.e. Asana::Task) says that it will take a hash of options. As far as I can tell looking at the little code snippet, it should be the same options as are listed on https://asana.com/developers/documentation/getting-started/input-output-options#paths
However, when I do client.tasks.find_by_id(123456, :fields => "this.assignee.email"), for example, I get an ArgumentError: unknown keyword: fields.
What am I doing wrong? How should this work?
Also: it's unclear to me from the above page when I should be using this in my field specifications and when it is unnecessary.
EDIT: SOLVED!
The correct syntax is client.tasks.find_by_id(123456, :options => { :fields => "this.assignee.email" })
Both :fields and "fields" work.

Judging from the code in the ruby client library: https://github.com/Asana/ruby-asana/blob/423f76c14792bd4712c099161a14a10ce941b2d9/lib/asana/http_client.rb#L42
Perhaps something like client.tasks.find_by_id(123456, {"fields" => "this.assignee.email"}) might work. Could you try that?

Related

Padrino route optional parameter

I am trying to do some tricks with routes. And I need to use optional route parameter in Padrino. I googled that solution for this are "()" parenthesis. I couldn't find in docs.
But when I try to use
get :sort, :with => [:order, :asc, '(:search)'] do
them, mustermann is giving me classic error for missing parameter
cannot expand with keys [:asc, :order], possible expansions: [:asc, :order, :search]
when I try to call
url(:sbirka, :sort, :order => "id", :asc => #asc)
I also tried different style
get :sort, "/:order/:asc/(:search)" do
with the same result
Please any suggestions how to do this?
Since Padrino is based on Sinatra, every routing pattern possible with Sinatra should be possible in Padrino as well. From Sinatra's excellent README file, introductory 'Routes' section:
Route patterns may have optional parameters
In your case, assuming :search is optional, I would try:
get '/:order/:asc/:search?' do
# your code
end

How do I create a Task using Asana Ruby Gem

I'm looking for a really simple "HelloWorld" example of creating a task in Asana using the ruby gem.
Here is what I am trying to run, and I'm just not quite grokking how to pass in the parameters:
Asana::Task.create_in_workspace(client,workspace,{ 'name': 'new task' })
I think you will need to do:
require 'asana'
client = Asana::Client.new do |c|
c.authentication :access_token, 'personal_access_token'
end
workspace = client.workspaces.find_by_id(12)
client.tasks.create_in_workspace(workspace: workspace.id, options: {}, **data)
You can directly pass in a workspace id if you know it before hand. options is a hash of request I/O options and data is a hash of attributes to post.
You can look into the documentation for more details on them.
You can also look at this official Hello World example in Ruby that does not use any SDK.

Mocking methods in google ruby api client

I am trying to mock some methods that use the google-api-ruby-client to make some testing without actually calling the api. Authentication and client and activities methods are taken from the example found on the github page (see link above), which is why I skipped it here.
The method from the example is the following:
def activities
result = client.execute(
:api_method => plus.activities.list,
:parameters => {'collection' => 'public', 'userId' => 'me'}
)
return result.data
end
I previously tried to stub the client (even chained with the execute) methods, however this results in authorization requests for oauth, which the gem uses underneath followed by mocks for the plus.activities.list methods. Is there a way to directly mock client.exectute to return something useful while skipping the whole chain?
I am not sure that I understand your problem correctly, but maybe something a little bit crazy will work
I assume that your method is in Client model so maybe something like that will work
Client.stub_chain(:client, :execute).and_return(true)
Of course if you model have different name you have to adjust. I am not sure but you can give it a try
Checkout their spec helper:
https://github.com/google/google-api-ruby-client/blob/master/spec/spec_helper.rb
And how they do the tests:
https://github.com/google/google-api-ruby-client/blob/master/spec/google/api_client_spec.rb

How to do atomic update in Mongo/Mongomatic?

I'm having an awfully difficult time figuring out how to update a MongoDB document, using the atomic '$set' operator with Mongomatic. I'm pretty sure it's Mongo's criteria/update language I'm having troubles with, not Mongomatic, but I'm willing to be proven wrong.
The link to a gist with a standalone, runnable script is here: https://gist.github.com/3835672
I'm starting out by creating a document that looks like this:
{"videos":[{"video_id":"video1"},{"video_id":"video2"}],"_id":{"$oid": "506ddd53a114604ce3000001"}}
I can get that document using a model instantiated using Mongomatic:
video_group = VideoGroup.find_one('videos.video_id' => 'video1')
Then I'm trying to set a 'views' field, by doing this:
video_group.update!({ 'videos.video_id' => 'video1' }, '$set' => { 'videos.$.views' => 123 })
That's where Mongo blows up, with the following error:
can't append to array using string field name [$]
I know this is a very common question on StackOverflow. I understand generally that the problem is that the positional operator isn't getting any matches. But even reading through dozens of responses, I still can't figure out how to express this statement in a way that works.
Am I just starting out with the wrong data structure?
It is, in fact, a mongomatic problem. You need to pass the underlying mongo ruby driver the option {:multi => true}, as well as including your criteria with the specific _id for the update sent to mongodb instead of as part of the optional parameters. Looks like a bug in mongomatic. Here is the ruby debugger transcript that I used to find it: https://gist.github.com/3836797
Note that I made a change to the file you posted, adding the line debugger before line #41, and changing line #42-44 to this:
video_group.update!({ 'videos.video_id' => 'video1', :multi => true }, '$set' => {
'videos.$.views' => 123,
})

Where did this Ruby parameter convention come from?

There's a piece of Ruby middleware used by Rails and other frameworks to the parse the parameters you've sent to the server into a nested Hash object.
If you send these parameters to the server:
person[id] = 1
person[name] = Joe Blow
person[email] = joe.blow#test.com
person[address][street_address] = 123 Somewhere St.
person[address][city] = Chicago
person[address][zip] = 12345
person[other_field][] = 1
person[other_field][] = 2
person[other_field][] = 3
They get parsed to this:
{
:person => {
:id => "1",
:name => "Joe Blow",
:email => "joe.blow#test.com",
:address => {
:street_address => "123 Somewhere St.",
:city => "Chicago",
:state => "IL",
:zip => "12345"
},
:other_field => [ 1, 2, 3 ]
}
}
I believe this is supported by PHP as well. Can anybody tell me what this convention is called, where it came from, and what other languages support it? (Perl, Python, etc.)
Field research
I'm trying to find out if there's a name for this convention, but I can't find it yet.
Ruby
For what it's worth, the piece of middleware that does this in Ruby is Rack::Utils. See the source on Github.
There is some more information on the subject in the Ruby on Rails Guides.
And here is an interesting ticket about the code being moved from Rails to the Rack middleware.
PHP
I've done some digging in the PHP source, and it seems that all the magic there happens in the main/php_variables.c source file. The SAPI code calls the php_std_post_handler method defined here. This eventually calls the php_register_variable_ex method, which is 185 lines of complex string-parsing in C (I must admit that C isn't my forte).
The only name I could find here was the php_std_post_handler, the PHP standard POST handler.
Python
In Python, this format isn't supported by default. See this question here on stackoverflow.com on the subject.
Perl
The CGI library in Perl doesn't support this format either. It does give easy access to single or multiple values, but not nested values as in your example. See the documentation on feching the value or values of a single named parameter and fetching the parameter list as a hash.
Java
Check out the heated debate on the subject of query parameter parsing in this question. Java doesn't parse this 'nested format' of POST parameters into a data structure by default.
Conclusion
I've looked into this, and haven't found a single name for this way of parameter parsing. Of the languages that I've looked into, only Ruby and PHP support this format natively.
It's not called anything, AFAIK, other than "sending parameters". If anything, it's called "parameter [type] conversion", where Rails just "converts" it into a hash.
Other frameworks go further, using the parameter names as expressions used to create typed objects initialized with type-converted parameter values.
All parameters are is a string value with a name. Any/all structure is imposed by the language/framework in use on the server side; what it gets transformed to is 100% dependent on that language/framework, and what that conversion consists of would determine what it would be (reasonable) called.
that would be a JSON object, which is quite standard and supported by most languages/libraries these days.

Resources