0

New to programming and Ruby. I'm working with the iStock API and need to send a fileID to request information about files I have.

Example files I have that I need get to request info on iStock:

["iStock_000001053628Small.jpg", "iStock_000002270952Big_Web.mov", "ist2_7034929-blurred-commuter.jpg", iStock_000000042199Medium.jpg] 

Working in irb, the code I send to iStock's API:

require 'xmlrpc'
parameters = {:apiKey=>"#", :fileID=>"1053628"}
server = XMLRPC::Client.new2( "http://api.istockphoto.com/webservices/xmlrpc")
server.call("istockphoto.image.getInfo", parameters)

Challenge: I need to extract the fileID numbers out of the array. This means I have to take all the zeros off the front of the file number. However, the file numbers differ in length and the number of zeros in the front differs for each image.

istock.each do |x|
  result = ' '
  x.scan(/\d+/) do |y|
    if y.to_i > 4
      result << y
    end
    puts result
  end
end

000001053628
000002270952
7034929
000000042199

I'm not sure how to approach this problem because if I set up rules for the sets of 0's, there might be a time when the actual fileID also has these in it.

2 Answers 2

2

I am not sure about what different filename formats you have to support, but something like this works for me:

$ irb
irb(main):001:0> test_data = ["iStock_*000001053628*Small.jpg", "iStock_000002270952BigWeb.mov", "ist2_7034929-blurred-commuter.jpg", "iStock_*000000042199*Medium.jpg"]
=> ["iStock_*000001053628*Small.jpg", "iStock_000002270952Big Web.mov", "ist2_7034929blurred-commuter.jpg", "iStock_*000000042199*Medium.jpg"]
irb(main):002:0> test_data.map do |d|
irb(main):003:1*   d.scan(/\d{4,}/).first.gsub(/^0+/,'')
irb(main):004:1> end
=> ["1053628", "2270952", "7034929", "42199"]
irb(main):005:0>
Sign up to request clarification or add additional context in comments.

1 Comment

Instead of gsub, a sub would have been sufficient here. But you do without a sub as well: d.scan(/[1-9]\d{3,}/).first, or simply d.scan(/\d{4,}/).first.to_i to get the raw numbers.
0

I'm not sure what this line is about:

if y.to_i > 4

But it sounds like you just want to remove leading zeros, correct?

In that case, just try this:

# Parse the string as an integer, then re-format as a string.
result << y.to_i.to_s

This will populate your result array with strings whose leading zeros have effectively been removed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.