1

Currently, for my project, I'm using different services like for SMS, Push Notification etc...

So I would like to know a way to restrict this service calls based on the environment which I'm currently in.

let's say if the current environment is production obviously we need all service to be work as expected but it may not necessary for other environment.

So I would like to know some way to restrict that service to the specific environment only rather than making it available for all environment. I know as a conditional basis we know we can check it by:

Rails.env.production?

But for this, we are actually modifying the code which is not a good practice to do. I don't want to make it that way or change the actual code. what I'm thinking is intercept the external API request of the service or do some action like giving a preview. I'm not sure that's the better approach if you have any suggestion you can shoot also.

The behaviour of letter opener gem is something like that in development instead of sending a mail it opens a preview for us.

I also found this solution in SO Rails 3: How to intercept any HTTP request if possible can someone explain how this working.

Ganesh
  • 1,924
  • 1
  • 18
  • 31
Vishal Taj PM
  • 1,339
  • 11
  • 23

2 Answers2

1

You can probably have different configurations depending of your environment, in config/production.rb, config/development.rbetc.

An implementation could look like:

# in file config/production.rb

SmsService = RealSmsService
PushNotificationsService = RealPushNotificationsService

# in file config/development.rb

class MockSmsService
  def self.send_sms(recipient, message)
    Rails.logger.info "SMS to #{recipient}: #{message}"
  end
end

class MockNotificationsService
  def self.notify(*args)
  end
end

SmsService = MockSmsService
PushNotificationService = MockNotificationsService

You probably want to define mock classes elsewhere and require them though, but you get the idea.

And then you use the constants you defined instead of the real ones. This will help you avoid having these ugly if Rails.env.production? in your code.

A good idea is to define these classes for testing too.

You will need to keep the APIs of these mocks in sync.

Guillaume
  • 116
  • 6
0

I think you might be looking for something like webmock here https://github.com/bblimke/webmock it is meant for test environment, but i don't think it would be hard to set it up so that it can be used in development too.

Fran
  • 1,073
  • 10
  • 19
  • Thanks, Fran , actually before going to the `Gem` I'm looking for a way to make `module` my own. because I have other logic to implement to it. – Vishal Taj PM Apr 06 '18 at 10:15
  • 1
    I don't know which logic you need to implement but you can use webmock for quite a lot of things you couldn't even imagine. For example: You could create a mock implementation (with sinatra for example) of an API you're using and tell webmock to act as a proxy and redirect any request to that API to your mock. – Fran Apr 06 '18 at 10:43