Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use hardcoded ip address in development #16

Closed
trevormast opened this issue Jun 22, 2016 · 2 comments
Closed

Use hardcoded ip address in development #16

trevormast opened this issue Jun 22, 2016 · 2 comments
Assignees

Comments

@trevormast
Copy link
Collaborator

Geocoder isn't able to detect our location in development.

@trevormast trevormast self-assigned this Jun 22, 2016
@trevormast
Copy link
Collaborator Author

Not very clean, but it works:

#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_filter :development_ip
  ...

  def development_ip
    if Rails.env.development?
      ActionDispatch::Request.class_eval do
        def remote_ip
          Rails.application.secrets[:dev_ip]
        end
      end
    end
  end
end

--------

#config/secrets.yml
development: 
  dev_ip: #hardcoded ip address

@trevormast
Copy link
Collaborator Author

This is a more programmatic method that hits ipify's API and returns your ip:

def development_ip
    if Rails.env.development?
      ActionDispatch::Request.class_eval do
        def remote_ip
          uri = URI.parse('http://api.ipify.org?format=json')
          http = Net::HTTP.new(uri.host, uri.port)

          request = Net::HTTP::Get.new(uri.request_uri)
          response = JSON.parse(http.request(request).body)["ip"]
        end
      end
    end
  end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant