0

I am working on a data integration app which need to fetch images from one API (with XML's urls) and post the images to a rails built REST API.

I tried paperclip to download all the images however don't know how to handle the Paperclip::Attachment type when trying to post the images with HTTMultiParty.

I am thinking about use open-uri instead of paperclip which will store file into binary. Can anyone give me an example on that? And is there any good option for posting image to API apart from httmultiparty.

B Liu
  • 113
  • 1
  • 5
  • "I am thinking about use open-uri instead of paperclip which will store file into binary. Can anyone give me an example on that?" > http://stackoverflow.com/questions/1074309/how-do-i-download-a-picture-using-ruby – orde Mar 23 '16 at 15:57

1 Answers1

0

It's better to answer this question myself because the solution can be varied.

So image fetch and feed through api can be done by httparty(download&upload text)+paperclip(download image by url)+httmultiparty(upload image), here are some code example I use in my application.

To me, httparty is easiest way to deal with api, codes can be easily done like this:

response = HTTParty.get('url')
response = HTTParty.post('url',
                        :headers => 'head content',
                        :body => {'data':'data content'})

Code example on paperclip is here: answer on stack over flow

The important part is parsing the paperclip image to binary file, code goes:

 Paperclip.io_adapters.for(productData[0].image).read

The last example is HTTmultiparty, When you pass a query with an instance of a File as a value for a PUT or POST request, the wrapper will use a bit of magic and multipart-post to execute a multipart upload,apart from that it is pretty much the same as httparty:

class ImgClient
  include HTTMultiParty
  base_uri 'http://localhost:3000'
end
respond = ImgClient.post('url',
                        :headers => head,
                        :query => {
                            :image => Paperclip.io_adapters.for(product.image)
                        })

Hope this will be helpful for other api newbies.

Community
  • 1
  • 1
B Liu
  • 113
  • 1
  • 5