Instance ID mismatch with dm-paperclip in a Sinatra + DataMapper app - ruby

I'm trying to use dm-paperclip to handle uploads in my Sinatra app. It works fine with static values. However, when I try to use dynamic interpolation, a key part of the path that the uploaded file is saved in – the :id variable, which comes from dm-paperclip's interpolation – has one value at write time and a different value at read time.
In other words, I have this in my model:
has_attached_file :attachment,
:url => '/system/attachments/:id/:style/:basename.:extension',
:path => "#{APP_ROOT}/public/system/attachments/:id/:style/:basename.:extension"
# saves to path /my/root/public/system/attachments/217880/original/filename.png
It uploads and saves with no problem. However, when I do #file.attachment.path it shows something like:
/my/root/public/system/attachments/218298/original/filename.png
I can't find anything in dm-paperclip documentation or forums that talks about this. Someone please help! I've spent hours...
P.S. I tried switching to carrierwave-datamapper but that won't work either because it fails in a big way with to_json, which is critical for my app.

I know this doesn't answer your question but I encourage you to switch to carrierwave-datamapper - it's a much better solution to file uploads and what's even more important it's maintained.

Related

How to Get FHIR Photo for Patient from a URL

Consider the FHIR Patient data at http://spark.furore.com/fhir/Patient/f201.
How can I get the photo object referenced therein at URL "binary/#f006"??
I would have thought an HTTP GET on http://spark.furore.com/fhir/binary/#f006 would have done it, but alas...
the data there is wrong. Your conversion to the get was correct, but you ended up with a wrong URL because the reference is wrong in the first place.
It should say: url="Binary/f006" which would equate to a get of http://spark.furore.com/fhir/Binary/f006. That doesn't work either, which is another error in the way things are defined.
See http://gforge.hl7.org/gf/project/fhir/tracker/?action=TrackerItemEdit&tracker_item_id=6107 for follow ups
Yes, this reference is outdated, and we are not distributing Binaries currently as part of the examples in the FHIR specification. Our server Spark loads the examples from the specification when we initialize the database, hence the images are not there.
For now, I have uploaded the correct image to Binary/f006 and have updated the link in Patient/f201, so things should work now. When we re-initialize the database (we don't do this often), these changes will be reversed, but a simple PUT to Binary/f006 and an update of Patient/f201 will fix this of course.

Ruby AWS - Programmatically generate list of available AWS instance types

I've recently begun using the aws gem in a Sinatra web application whose purpose is to provide a customized frontend to instance management (integrating non-AWS tools). I am currently working on the form to allow a user to set all the options that might need setting, and one of those options is instance type (m1.small, c1.medium, etc).
What I'd like is to be able to reach out to some source to pull a list of available types. I have looked through the AWS::EC2 documentation and haven't found anything matching this description. I've got no need to insist that a solution be part of the aws gem, but even better if it is, because that's the tool I'm already using.
Do you know of a way to gather this information programmatically?
As far as I can tell this isn't possible. If it were possible, amazon would list the api call in their documentation.
I find the omission a little odd considering the've got apis to list pretty much anything else.
You could maybe kludge it via the DescribeReservedInstancesOfferings call, which lists all the kinds of reserved instances you can buy - I would have thought that extracting the unique instance-types from that would be a reasonable approximation (as far as I know there are no instance types you can't get reserved instances for). Doesn't look like the aws gem supports it though. The official amazon sdk does, as does fog
Here's a somewhat kludgy work-around to the fact that Amazon's still not released an API to enumerate instance types:
instance_types = Set.new()
response = {:next_token => ''}
loop do
response = ec2.client.describe_spot_price_history(
:start_time => (Time.now() - 86400).iso8601,
:end_time => Time.now().iso8601,
:product_descriptions => ['Linux/UNIX'],
:availability_zone => 'us-east-1c',
:next_token => response[:next_token]
)
response[:spot_price_history_set].each do |history_set|
instance_types.add(history_set[:instance_type])
end
if(response[:next_token].nil?)
break
end
end

Twitter Ruby Gem

I am trying to setup the Twitter gem, and I feel like I'm almost there... kind of.
Right now I was trying to follow this link:
http://www.phyowaiwin.com/how-to-download-and-display-twitter-feeds-for-new-year-resolution-using-ruby-on-rails
It is a bit old though, and I guess its instructions are a bit out of date. I have created a twitter model, twitter db migration and a twitter controller(not sure it's needed though), and if i open rails console, and I type:
Twitter.user_timeline("whatever").first.text
It just works. I just can't seem to be able to see it in my view. can you point me in the right direction?
Thanks a lot!
Alex
In your controller that corresponds with your view, you need to assign your results to a variable inside the appropriate functionlike so:
def controller_function
#twitter_data = Twitter.user_timeline("whatever").first.text
end
Then, in your corresponding view you can use the variable
<%= #twitter_data ... %>
Check out http://guides.rubyonrails.org/layouts_and_rendering.html for more guidance on controllers and views

Twitter Ruby Gem - get friends names and ids, nothing more

Is it possible to simply get the people you are following with just an id and full name? I do not need any of the additional data, it's a waste of bandwidth.
Currently the only solution I have is:
twitter_client = Twitter::Client.new
friend_ids = twitter_client.friend_ids['ids']
friends = twitter_client.users(friend_ids).map { |f| {:twitter_id => f.id, :name => f.name} }
is there anyway to just have users returned be an array of ids and full names? better way of doing it than the way depicted above? preferably a way to not filter on the client side.
The users method uses the users/lookup API call. As you can see on the page, the only param available is include_entities. The only other method which helps you find users has the same limitation. So you cannot download only the needed attributes.
The only other thing I'd like to say is that you could directly use the friends variable, I don't see any benefit of running the map on it.

Polymorphic urls with singular resources

I'm getting strange output when using the following routing setup:
resources :warranty_types do
resources :decisions
end
resource :warranty_review, :only => [] do
resources :decisions
end
I have many warranty_types but only one warranty_review (thus the singular route declaration). The decisions are polymorphically associated with both. I have just a single decisions controller and a single _form.html.haml partial to render the form for a decision.
This is the view code:
= simple_form_for #decision, :url => [#decision_tree_owner, #decision.becomes(Decision)] do |form|
The warranty_type url looks like this (for a new decision):
/warranty_types/2/decisions
whereas the warranty_review url looks like this:
/admin/warranty_review/decisions.1
I think because the warranty_review id has no where to go, it's just getting appended to the end as an extension.
Can someone explain what's going on here and how I might be able to fix it?
I can work around it by trying to detect for a warranty_review class and substituting #decision_tree_owner with :warranty_review and this generates the correct url, but this is messy. I would have thought that the routing would be smart enough to realise that warranty_review is a singular resource and thus discard the id from the URL.
This is Rails 3 by the way :)
Apparently it's a long standing rails bug where polymorphic_url has no way of knowing whether a resource is singular or not from the routes setup:
https://rails.lighthouseapp.com/projects/8994/tickets/4077-wrong-redirect-after-creation-of-nested-singleton-resource-using-responder
I'm just going to resort to using a non-singular route even though there will only ever be one warranty_review. It's just aesthetics at the end of the day.

Resources