2

I'm new to open-uri and trying to set an outgoing IP address using open-uri in ruby on rails. I used this post as a reference to get started. I'm porting an app from PHP where I could use CURLOPT_INTERFACE in curl_setopt. What's the best way to do this using open-uri in rails? (Doing this from the controller - not command line.)

If there's not a way to do this - any suggestions on an alternative to open-uri? My goal is to take in and parse JSON data.

Community
  • 1
  • 1
matthewvb
  • 932
  • 2
  • 11
  • 21
  • `open-uri` is not a CURL bindings, there is a `curb` gem, that might have such feature. – taro Jan 16 '12 at 14:54
  • @taro: I'm trying to find the best way in rails to do a function similar to the curlopt_interface option. I'll check out curb. – matthewvb Jan 16 '12 at 17:16
  • Here are the docs: http://curb.rubyforge.org/classes/Curl/Easy.html#M000015 – taro Jan 16 '12 at 18:08
  • Not sure the interface is setting correctly using Curb. I've opened a ticket on the [curb git page](https://github.com/taf2/curb/issues/102). – matthewvb Jan 19 '12 at 22:47

2 Answers2

0

What I understand from your questions is you want to hit another server from a specific IP which suggests you have a server with couple of addresses.

What I can suggest you is try to execute curl directly and do what you want to do or use a wrapper for it.

Yavor Ivanov
  • 449
  • 3
  • 8
0

Doesn't look like open-uri can do this. But with net/https it's fairly easy.

require 'net/https'
require 'json'
uri = URI('https://jsonvat.com/')
http = Net::HTTP.new(uri.host, uri.port)
http.local_host = '1.2.3.4'
http.use_ssl = true
request = Net::HTTP::Get.new('/')
request.content_type = 'application/json'
request.initialize_http_header('Content-Type' => 'application/json')
response = http.request(request)
json = JSON.parse(response.body)

Probably you don't need the "require" lines inside Rails Controllers. You can specify the outgoing IP address with the http.local_host line.

xoryves
  • 1,447
  • 18
  • 13