diff --git a/README.md b/README.md index 2b13699..94fa353 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ Or install it yourself as: ### Configure the HTTP client +```ruby FirebaseDynamicLink.configure do |config| # the adapter should be supported by Faraday # more info look at https://github.com/lostisland/faraday/tree/master/test/adapters @@ -39,8 +40,8 @@ Or install it yourself as: # default 'UNGUESSABLE' config.suffix_option = 'SHORT' or 'UNGUESSABLE' - # required - config.dynamic_link_domain = 'http://xyz.app.goo.gl' + # required, Don't put http:// + config.dynamic_link_domain = 'xyz.app.goo.gl' # default 3 seconds config.timeout = 3 @@ -48,8 +49,13 @@ Or install it yourself as: # default 3 seconds config.open_timeout = 3 end +``` + +### Shorten a link +```ruby client = FirebaseDynamicLink::Client.new + link = "http://domain.com/path/path" options = { # optional, to override default suffix default config suffix_option: '', @@ -66,11 +72,46 @@ Or install it yourself as: # options argument is optional result = client.shorten_link(link, options) +``` + +### Shorten parameters + +```ruby + client = FirebaseDynamicLink::Client.new + options = { + # optional, to override default suffix default config + suffix_option: '', + + # optional, to override default dynamic_link_domain default config + dynamic_link_domain: '', + + # optional, timeout of each request of this instance + timeout: 10, + + # optional, open timeout of each request of this instance + open_timeout: 10 + } + + parameters = { + link: link, + android_info: { + android_package_name: name, + } + ios_info: {}, + navigation_info: {}, + analytics_info: {}, + social_meta_tag_info: {} + } + + # options argument is optional + result = client.shorten_parameters(parameters, options) +``` if request successful, then the result should be like following hash object or if the request reached daily quota, client will throw `FirebaseDynamicLink::QuotaExceeded` error +```ruby { :link=>"https://--.app.goo.gl/ukph", :preview_link=>"https://--.app.goo.gl/ukph?d=1", @@ -86,14 +127,10 @@ or if the request reached daily quota, client will throw `FirebaseDynamicLink::Q } ] } +``` otherwise it will throw `FirebaseDynamicLink::ConnectionError` error, with message = http error message -# NOTE - -this gem only implemented to shorten long dynamic link til now, next version is supposed to -be able shorten a JSON object - ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. diff --git a/lib/firebase_dynamic_link.rb b/lib/firebase_dynamic_link.rb index 0b8786e..7af4ab8 100644 --- a/lib/firebase_dynamic_link.rb +++ b/lib/firebase_dynamic_link.rb @@ -9,15 +9,22 @@ module FirebaseDynamicLink extend Dry::Configurable + # called when invalid configuration given class InvalidConfig < StandardError; end + + # called when HTTP request failed class ConnectionError < StandardError; end + + # called when Firebase says that no more quota class QuotaExceeded < StandardError; end # @!group Configuration # @!method adapter # @!scope class + # Selected Faraday HTTP adapter # @!method adapter=(adapter_key) # @!scope class + # Set Faraday HTTP adapter # @param adapter_key [Symbol] # @example # FirebaseDynamicLink.adapter = :patron @@ -29,8 +36,10 @@ class QuotaExceeded < StandardError; end # @!method api_key # @!scope class + # Given api key # @!method api_key=(key) # @!scope class + # Set api key # @param key [String] # @since 0.1.0 setting :api_key diff --git a/lib/firebase_dynamic_link/client.rb b/lib/firebase_dynamic_link/client.rb index 9967c38..62d6f4c 100644 --- a/lib/firebase_dynamic_link/client.rb +++ b/lib/firebase_dynamic_link/client.rb @@ -4,6 +4,7 @@ require "firebase_dynamic_link/link_renderer" module FirebaseDynamicLink + # Main class that responsible to shorten link or parameters class Client extend Forwardable diff --git a/lib/firebase_dynamic_link/connection.rb b/lib/firebase_dynamic_link/connection.rb index 3029abe..aebf9df 100644 --- a/lib/firebase_dynamic_link/connection.rb +++ b/lib/firebase_dynamic_link/connection.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true module FirebaseDynamicLink + # Responsible to do HTTP request class Connection extend Forwardable @@ -16,18 +17,22 @@ def initialize(end_point) client.options.open_timeout = FirebaseDynamicLink.config.open_timeout end + # @see Faraday.timeout= def timeout=(time) client.options.timeout = time end + # @see Faraday.timeout def timeout client.options.timeout end + # @see Faraday.open_timeout= def open_timeout=(time) client.options.open_timeout = time end + # @see Faraday.open_timeout def open_timeout client.options.open_timeout end