Class: ValidatorsAppRuby
- Inherits:
-
Object
- Object
- ValidatorsAppRuby
- Includes:
- HTTParty
- Defined in:
- lib/validators_app_ruby.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VALIDATORS_PATH =
"https://validators.app/api/v1"
- AVAILABLE_NETWORKS =
%w(mainnet testnet).freeze
Instance Method Summary collapse
- #headers ⇒ Object
-
#initialize(token:, url: VALIDATORS_PATH) ⇒ ValidatorsAppRuby
constructor
A new instance of ValidatorsAppRuby.
-
#method_missing(method, **args) ⇒ Object
EXAMPLE: get_validators(network: “mainnet”).
- #prepare_path(path:, network: nil, id: nil) ⇒ Object
- #validate_network(network: nil) ⇒ Object
Constructor Details
#initialize(token:, url: VALIDATORS_PATH) ⇒ ValidatorsAppRuby
Returns a new instance of ValidatorsAppRuby.
14 15 16 17 |
# File 'lib/validators_app_ruby.rb', line 14 def initialize(token:, url: VALIDATORS_PATH) @token = token @url = url end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, **args) ⇒ Object
EXAMPLE: get_validators(network: “mainnet”)
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/validators_app_ruby.rb', line 20 def method_missing(method, **args) network = validate_network(network: args[:network]) path = prepare_path(path: method, network: network, id: args[:id]) params = args.reject{ |a| [:network, :id].include?(a) } if method.to_s.start_with?("get_") response = self.class.get(path, query: params, headers: headers) elsif method.to_s.start_with?("post_") response = self.class.post(path, body: params.to_json, headers: headers) else super end raise ArgumentError, "Request failed with #{response.code}: #{response.}" \ unless response.code < 300 response end |
Instance Method Details
#headers ⇒ Object
52 53 54 55 56 57 |
# File 'lib/validators_app_ruby.rb', line 52 def headers { "Content-Type" => "application/json", "Token" => @token } end |
#prepare_path(path:, network: nil, id: nil) ⇒ Object
39 40 41 |
# File 'lib/validators_app_ruby.rb', line 39 def prepare_path(path:, network: nil, id: nil) [@url, path.to_s.split("_")[1..-1].join("-"), network, id].compact.join("/") end |
#validate_network(network: nil) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/validators_app_ruby.rb', line 43 def validate_network(network: nil) return nil unless network raise ArgumentError, "Allowed networks are: #{AVAILABLE_NETWORKS.join(', ')}." \ unless AVAILABLE_NETWORKS.include?(network) network end |