Can't save user profiler image - ruby

I'm working on Ruby script with Twitter API. I try to save profile image fetched but couldn't.
#image = subject.user.profile_image_url
=> #<Addressable::URI:xxxxxxxx URI:http://pbs.twimg.com/profile_images/xxxxxxxxxx/m_xxxxxxxxxxx.jpg>
I want to save only image URI. It's not concise.
How can I remove #<Addressable::URI:xxxxxxxx >?

You have to use path method:
#image = subject.user.profile_image_url.path
Check more in documenation

Related

Ruby Watir-webdriver saving image when navigating directly to the image

I'm trying to grab a set of information from a series of pages that are loaded via JS and to accomplish that I'm using watir-webdriver to load the page and nokogiri to parse them. This is working great, however, I need to grab a picture off of the page. The path of the picture is generated upon the page's loading so I wrote the following to create an array of relative URLS to the images and navigate directly to the absolute URL of the first index of the array, which is always the image I want.
img_srcs = $page_html.css('img').map{ |i| i['src'] } #genereates an array of relative urls pointing to every image
imageURL= "website.com" + img_srcs[1].gsub("..","").to_s #take the relative URL of image at index position 1 (the image) and converts it to an absolute URL
$browser.goto(imageURL)
How can I save this image which the browser has directly loaded? Any help would be appreciated and please let me know if I anything is unclear.
Edit:
I've now added the following code
image_source = $browser.image(:class => "decoded").image.src
File.open("#{$imageID}.txt", "w") do |f|
f.write open(image_source).read
f.close
end
However, I'm getting the error
C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.4/lib/watir-webdriver/el
ements/element.rb:490:in 'assert_exists': unable to locate element, using {:tag_
name=>"img"} (Watir::Exception::UnknownObjectException)
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.4/lib/watir
-webdriver/attribute_helper.rb:71:in 'block in define_string_attribute'
from 12.rb:121:in 'imageDownload'
from 12.rb:134:in 'navAndGrab'
from 12.rb:137:in '<main>'
When you do:
$browser.image(:class => "decoded").image.src
You are looking for the html:
<img class="decoded">
<img src="what_you_want"></img>
</img>
I am guessing your html is not like that, hence you get the exception regarding finding the image within the image.
You probably just want the first image with class decoded (remove the second .image):
image_source = $browser.image(:class => "decoded").src
Or maybe you want the full list of images and then get the first one:
image_source = $browser.images(:class => "decoded").first.src

CodeIgniter retrieve image from secret folder

I have create private folder in application folder: application/private/username, I'm uploading images here with ajax, that works, but how can I retrieve that image with ajax, and display it in img tag. First problem is that http://example.com/application/private/useraname/img.jpg is protected, I can't access it through out url in browser, how to display an image. Just advice, I wanna write code on my own.
This is part of function(that is part of controller) that grabs image:
$filepath = 'path to image';
header("Content-type: image/jpeg");
if (file_exists($filepath)) {
$img_handle = imagecreatefromjpeg($filepath) or die("");
echo $img_handle;
ImageJpeg($img_handle);
}
But how to get this image with ajax?
You might want to create a PHP file that would get the filename eg thumb.php?src=img.jpg and inside that file, you load the image from that path and display it. This way the path of the images are not exposed to the user.

Switching to use a background image from no background image via a multi-part post to the Twitter API with Ruby and OAuth

I would like to change my profile background from no background image to a background image that I upload via the Twitter API using Ruby. I have no problems uploading new background images when my profile is already set to use an image as the background. When my profile is set to not use an image as the background, however, and I try to upload an image with the added param "use=1" to tell Twitter I want to use the background image I'm uploading, Twitter replies with this:
{"error":"You tried to turn on your background, but don't have one selected.",
"request":"\/1\/account\/update_profile_background_image.json"}
suggesting Twitter's not recognizing that I'm uploading an image. In every other case, however, image uploading works perfectly. Adding the "tile" param with the request works too, so the problem doesn't seem to be with mixing file params and non-file params. I can even try to turn my background image off from being on while uploading a new image and Twitter gives me this:
{"error":"You tried to turn off your background while also uploading a new one.",
"request":"\/1\/account\/update_profile_background_image.json"}
showing that Twitter is totally able to detect that I'm uploading an image.
Am I missing something?
Here's the code:
require 'oauth'
require 'net/http/post/multipart'
consumer = OAuth::Consumer.new(APP_CONSUMER_KEY, APP_CONSUMER_SECRET,
{:site => "http://api.twitter.com", :scheme => :header})
access_token = OAuth::AccessToken.from_hash(consumer,
{:oauth_token => OAUTH_TOKEN, :oauth_token_secret => OAUTH_TOKEN_SECRET})
image_file = File.new(image_file_path)
url = URI.parse('http://api.twitter.com/1/account/update_profile_background_image.json')
req = Net::HTTP::Post::Multipart.new url.path, {
"use" => "1",
"image" => UploadIO.new(image_file, image_mime_type, image_file_name)
}
consumer.sign!(req, access_token)
Net::HTTP.new(url.host, url.port).start do |http|
puts http.request(req)
end

Dynamically serving images from Google App engine

having problems serving an image stored as a blob from google app engine - I'm trying to view a stored image with the following code
my datastore model is:
class QuestionTemp(db.Model):
picture = db.BlobProperty()
my post call from the initial form is:
class QuestionAsker3(webapp.RequestHandler):
def post(self):
upload_files = self.request.get('file') # 'file' is file upload
tempQuestion = QuestionTemp(picture= db.Blob(upload_files))
tempQuestion.put()
self.response.headers['Content-Type'] = "image/jpeg"
self.response.out.write(tempQuestion.picture)
The image is stored in the blobstore as I can view it in the GAE admin console
"blob viewer".
In chrome the return screen in blank - firefox I get a url and what looks to be a hashcode.
many thanks in advance.
managed to solve the problem - was reading the data into the Blobstore, rather than as a Blob
so the Blob was just storing a references hence had no image data. The .value thing didn't work for me.
Thanks for helping me figure it out #abdul and #adam

Rails3 - Problem saving base64 image with paperclip?

This is similar to the problem posted here, but that solution doesn't work for me. Maybe it's because I'm not passing the data in correctly.
I'm pulling screenshots from Flash and displaying them on the page using Jquery:
$SNAPSHOT_PREVIEW.attr("src","data:image/jpg;base64," + imgData);
$HIDDEN_BASE64_STRING.val(imgData);
I had it nice and working where you could save the image to Rails in Flash, but Flash won't allow you invoke a post action without the user pressing a button for security reasons. Makes sense. Anyway, now I can't get Paperclip to save the image coming from the HTML form:
#(photo has_attached_file:image)
#photo = params[:photo]
data = StringIO.new(Base64.decode64(params[:base64_string]))
data.class.class_eval { attr_accessor :original_filename, :content_type }
data.original_filename = "screenshots.jpg"
data.content_type = "image/jpg"
#photo.image = data
Yields the error:
NoMethodError (undefined method `image=' for #<ActiveSupport::HashWithIndifferentAccess:0x8c2e420>):
How do I need to finesse the base64 image data into a paperclip attachment?
For bonus points, do I need the hidden field to pass the data or is there a clever, browser compatible way to use the image src as a form value?
You have the code:
#photo = params[:photo]
params is just a hash, so later, when you call #photo.image, Rails bugs out. Perhaps you want:
#photo = Photo.new(params[:photo])
instead?

Resources