motdotla / digitalocean

The easiest and most complete rubygem for DigitalOcean.
http://rubygems.org/gems/digitalocean
MIT License
153 stars 42 forks source link

Getting details of two different images/droplets returns only details of the first one. Problem with api url generation #14

Closed tomazy closed 10 years ago

tomazy commented 10 years ago

Example:

images = Digitalocean::Image.all.images
Digitalocean::Image.find(images[0].id).image.id == images[0].id # true
Digitalocean::Image.find(images[1].id).image.id == images[1].id # false
Digitalocean::Image.find(images[1].id).image.id == images[0].id # true

This affects all requests to all resources. The problem is with pre_query variable in closure which gets overwritten after calling any of the resource methods.

DEFINITIONS[resource_name].each do |method_name, url|                                                                                                 
  parts         = url.split("?")                                                                                                                      
  pre_query     = parts[0]                                                                                                                            
  #^^^^^^^^                                                                                                                                           
  post_query    = parts[1]                                                                                                                            

  singleton.send :define_method, "_#{method_name}" do |*args|                                                                                         
    pre_query   = Digitalocean.process_standard_args_from_part(pre_query, args)                                                                       
    #^^^^^^^^                                                  ^^^^^^^^^                                                                              
    post_query  = Digitalocean.process_hash_args_from_part(post_query, args)                                                                          

    [pre_query, post_query].join("?")                                                                                                                 
  end                                                                                                                                                 
  # ...                                                                                                                                                      
end                                                                                                                                                   
motdotla commented 10 years ago

Fixed with big thanks to @mpchlets. Fix is going up as 1.0.4 now on rubygems.