Class: MyUrlShortener
- Inherits:
-
Object
- Object
- MyUrlShortener
- Includes:
- Singleton
- Defined in:
- app/my_lib/my_url_shortener.rb
Constant Summary collapse
- MAX_RETRIES =
3- RETRY_INTERVAL =
1
Class Method Summary collapse
- .by_url(url) ⇒ Object
- .customer_modify_booking_link(reservation_id) ⇒ Object
- .find_or_create(url) ⇒ Object
Instance Method Summary collapse
- #customer_modify_booking_link(reservation_id) ⇒ Object
- #find_or_create_url(long_url) ⇒ Object
-
#initialize ⇒ MyUrlShortener
constructor
A new instance of MyUrlShortener.
- #shorten_url(long_url) ⇒ Object
Constructor Details
#initialize ⇒ MyUrlShortener
Returns a new instance of MyUrlShortener.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/my_lib/my_url_shortener.rb', line 15 def initialize host_url = Figaro.env.RAILS_DYNAMIC_HOST_URL api_key = Figaro.env.RAILS_DYNAMIC_API_KEY raise ArgumentError, "RAILS_DYNAMIC_HOST_URL is missing" if host_url.blank? raise ArgumentError, "RAILS_DYNAMIC_API_KEY is missing" if api_key.blank? @api_key = api_key @connection = Faraday.new(url: host_url) do |conn| conn.request :json conn.response :json, parser_options: { symbolize_names: true } conn.adapter Faraday.default_adapter end rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e APMErrorHandler.report('Failed to initialize ShortIO connection', e: e) nil end |
Class Method Details
.by_url(url) ⇒ Object
33 34 35 |
# File 'app/my_lib/my_url_shortener.rb', line 33 def self.by_url(url) instance.shorten_url(url) end |
.customer_modify_booking_link(reservation_id) ⇒ Object
41 42 43 |
# File 'app/my_lib/my_url_shortener.rb', line 41 def self.customer_modify_booking_link(reservation_id) instance.customer_modify_booking_link(reservation_id) end |
.find_or_create(url) ⇒ Object
37 38 39 |
# File 'app/my_lib/my_url_shortener.rb', line 37 def self.find_or_create(url) instance.find_or_create_url(url) end |
Instance Method Details
#customer_modify_booking_link(reservation_id) ⇒ Object
45 46 47 48 49 50 |
# File 'app/my_lib/my_url_shortener.rb', line 45 def customer_modify_booking_link(reservation_id) url = generate_customer_modify_booking_link(reservation_id) return nil unless url.present? shorten_url(url) end |
#find_or_create_url(long_url) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/my_lib/my_url_shortener.rb', line 70 def find_or_create_url(long_url) Retriable.retriable(on: Faraday::ConnectionFailed, tries: MAX_RETRIES, base_interval: RETRY_INTERVAL) do response = @connection.post('/v1/shortLinks/findOrCreate') do |req| setup_request_headers(req) req.body = { api_key: @api_key, url: long_url, }.to_json end handle_response(response, long_url) end rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e APMErrorHandler.report('Failure calling find_or_create short URL Rails Dynamic Link', e: e) long_url end |
#shorten_url(long_url) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'app/my_lib/my_url_shortener.rb', line 52 def shorten_url(long_url) # Faraday::ParsingError is raised for invalid JSON responses, usually because "Retry later" HTML is returned Retriable.retriable(on: [Faraday::ConnectionFailed, Faraday::ParsingError], tries: MAX_RETRIES, base_interval: RETRY_INTERVAL) do response = @connection.post('/v1/shortLinks') do |req| setup_request_headers(req) req.body = { api_key: @api_key, url: long_url, }.to_json end handle_response(response, long_url) end rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e APMErrorHandler.report('Failure creating short URL Rails Dynamic Link', e: e) long_url # fallback to original URL end |