Class: V1::ShortLinksController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- V1::ShortLinksController
- Defined in:
- app/controllers/dynamic_links/v1/short_links_controller.rb
Instance Method Summary collapse
Instance Method Details
#create ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'app/controllers/dynamic_links/v1/short_links_controller.rb', line 5 def create url = params.require(:url) client = DynamicLinks::Client.find_by(api_key: params.require(:api_key)) unless client render json: { error: 'Invalid API key' }, status: :unauthorized return end multi_tenant(client) do render json: DynamicLinks.generate_short_url(url, client), status: :created end rescue DynamicLinks::InvalidURIError render json: { error: 'Invalid URL' }, status: :bad_request rescue => e DynamicLinks::Logger.log_error(e) render json: { error: 'An error occurred while processing your request' }, status: :internal_server_error end |
#expand ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/controllers/dynamic_links/v1/short_links_controller.rb', line 24 def api_key = params.require(:api_key) client = DynamicLinks::Client.find_by(api_key: api_key) unless client render json: { error: 'Invalid API key' }, status: :unauthorized return end multi_tenant(client) do short_link = params.require(:short_url) full_url = DynamicLinks.resolve_short_url(short_link) if full_url render json: { full_url: full_url }, status: :ok else render json: { error: 'Short link not found' }, status: :not_found end end rescue => e DynamicLinks::Logger.log_error(e) render json: { error: 'An error occurred while processing your request' }, status: :internal_server_error end |
#find_or_create ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/controllers/dynamic_links/v1/short_links_controller.rb', line 48 def find_or_create url = params.require(:url) client = DynamicLinks::Client.find_by(api_key: params.require(:api_key)) unless client render json: { error: 'Invalid API key' }, status: :unauthorized return end unless valid_url?(url) render json: { error: 'Invalid URL' }, status: :bad_request return end multi_tenant(client) do short_link = DynamicLinks.find_short_link(url, client) if short_link render json: { shortLink: short_link[:short_url], previewLink: "#{short_link[:short_url]}?preview=true", warning: [] }, status: :ok else render json: DynamicLinks.generate_short_url(url, client), status: :created end end rescue => e DynamicLinks::Logger.log_error(e) render json: { error: 'An error occurred while processing your request' }, status: :internal_server_error end |