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

⚡ Write a generateShortUrl() Function using Ruby #39

Closed

Conversation

Platiplus
Copy link

Github Ticket

Closes APPWRITE-4189

Description

This PR adds an example function for generating a short URL using different providers.
The currently supported providers are:

  • Bitly
  • TinyURL

Screenshots

Success Bitly

success_bit

Success TinyURL

success_tiny

Error Bitly

error_bitly

Error TinyURL

error_tiny

@Platiplus
Copy link
Author

@tessamero Any news about this one?

@stnguyen90 stnguyen90 self-requested a review October 26, 2022 23:31
Copy link
Contributor

@stnguyen90 stnguyen90 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is beautiful code! 🤯 Just a few requests!

ruby/generate-short-url/README.md Outdated Show resolved Hide resolved
ruby/generate-short-url/README.md Outdated Show resolved Hide resolved
ruby/generate-short-url/index.rb Outdated Show resolved Hide resolved
ruby/generate-short-url/index.rb Show resolved Hide resolved
@Meldiron
Copy link
Contributor

Thank you so much for the PR 🤩. We're adding the hacktoberfest-accepted label to ensure this PR counts towards your Hacktoberfest contributions count. With that said, please stay active on this PR to address any comments once you receive a review. Happy Hacktoberfest! 🎃

@Platiplus
Copy link
Author

@stnguyen90 Sorry about the delay! Please check now if everything is okay!

3. Start the Open Runtime:

```
docker run -p 3000:3000 -e INTERNAL_RUNTIME_KEY=secret-key -e INTERNAL_RUNTIME_ENTRYPOINT=index.rb --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/ruby:3.1 sh /usr/local/src/start.sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This image still looks outdated.

is_invalid_provider = payload['provider'].nil? || payload['provider'].empty?
provider_not_supported = !VALID_PROVIDERS.include?(payload['provider'])

raise error_message if is_invalid_url || is_invalid_provider || provider_not_supported
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make sure to catch all exceptions and return a proper response message. For example, this isn't helpful:

$ curl -H "X-Internal-Challenge: secret-key" \
>     -H "Content-Type: application/json" \
>     -X POST http:https://localhost:3000/ \
>     -d '{"payload": {}}' 
{"stdout":"","stderr":"\n/usr/code-start/index.rb:48:in `validate_params'\n/usr/code-start/index.rb:15:in `main'\n/usr/local/src/server.rb:93:in `block in <top (required)>'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:1684:in `call'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:1684:in `block in compile!'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:1002:in `block (3 levels) in route!'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:1020:in `route_eval'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:1002:in `block (2 levels) in route!'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:1051:in `block in process_route'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:1049:in `catch'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:1049:in `process_route'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:1000:in `block in route!'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:997:in `each'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:997:in `route!'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:1121:in `block in dispatch!'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:1092:in `catch'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:1092:in `invoke'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:1116:in `dispatch!'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:932:in `block in call!'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:1092:in `catch'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:1092:in `invoke'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:932:in `call!'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:921:in `call'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/rack-protection-3.0.3/lib/rack/protection/xss_header.rb:20:in `call'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/rack-protection-3.0.3/lib/rack/protection/path_traversal.rb:18:in `call'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/rack-protection-3.0.3/lib/rack/protection/json_csrf.rb:28:in `call'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/rack-protection-3.0.3/lib/rack/protection/base.rb:53:in `call'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/rack-protection-3.0.3/lib/rack/protection/base.rb:53:in `call'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/rack-protection-3.0.3/lib/rack/protection/frame_options.rb:33:in `call'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/logger.rb:17:in `call'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/common_logger.rb:38:in `call'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:261:in `call'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:254:in `call'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/head.rb:12:in `call'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/method_override.rb:24:in `call'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:219:in `call'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:1995:in `call'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:1554:in `block in call'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:1770:in `synchronize'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/sinatra-3.0.3/lib/sinatra/base.rb:1554:in `call'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/configuration.rb:268:in `call'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/request.rb:89:in `block in handle_request'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/thread_pool.rb:343:in `with_force_shutdown'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/request.rb:88:in `handle_request'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/server.rb:430:in `process_client'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/server.rb:233:in `block in run'\n/usr/local/src/vendor/bundle/ruby/3.1.0/gems/puma-6.0.0/lib/puma/thread_pool.rb:150:in `block in spawn_thread'"}

}.freeze

def main(req, res)
payload = req.payload
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make this compatible with Appwrite functions, would you please test this with payload as a JSON string and parse it here. For example:

curl -H "X-Internal-Challenge: secret-key" \
    -H "Content-Type: application/json" \
    -X POST http:https://localhost:3000/ \
    -d '{"payload": "{\"url\": \"https://google.com\"}"}' 

ruby/generate-short-url/index.rb Show resolved Hide resolved
ruby/generate-short-url/index.rb Show resolved Hide resolved
@Meldiron
Copy link
Contributor

Hey there 👋
We are temporarily closing your PR as there has been no activity on it. Feel free to re-open it when you're ready to start working on it again 😇

@Meldiron Meldiron closed this Jan 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

⚡ Write a generateShortUrl() Function using Ruby
3 participants