module HttpApiBuilder::Dsl
Module for restful api dsl commands
Constants
- VERBS
Public Instance Methods
Set the initial URL used by the gem
# File lib/http_api_builder/dsl.rb, line 15 def base_url(value = nil) value.nil? ? @base_url : (@base_url = value) end
Protected Instance Methods
Generate whiny and quiet API consumer methods.
Whiny methods have a bang suffix and raise errors if they fail Quiet methods do not have the bang suffix and return nil if they fail
eg:
endpoint '/path', as: :mymethod
results in:
mymethod! <-- whiny mymethod <-- quiet
# File lib/http_api_builder/dsl.rb, line 31 def endpoint(path, as:, using: :get, params: nil, form: nil, body: nil, processors: nil, json: nil) # rubocop:disable Metrics/ParameterLists def_whiny_method as, path, using, processors, params, form, body, json def_quiet_method as end
Private Instance Methods
Generate a consumer method that returns nil when requests raise errors
# File lib/http_api_builder/dsl.rb, line 66 def def_quiet_method(name) define_method name do |opts = {}| begin send(:"#{name}!", opts) rescue StandardError nil end end end
Generate a consumer method that raises exceptions when requests raise an error
# File lib/http_api_builder/dsl.rb, line 46 def def_whiny_method(name, path, using, processors, params, form, body, json) # rubocop:disable Metrics/ParameterLists, Metrics/AbcSize, Metrics/MethodLength required, optional = requirements(path, params) define_method :"#{name}!" do |opts = {}| json = opts.delete(:json) || json body = opts.delete(:body) || body validate_args! opts.keys, required, optional reqpath = interpolate_path(path, opts) query = query_params(path, opts, required, optional) form = Hash(form).merge(Hash(opts[:form])) perform(using, reqpath, form: form, query: query, body: body, json: json) do |resource, *_| run_processors resource, processors end end end
Extract param segments from a path, paperclip style. Returns an array of symbols matching the names of the param segments.
Param segments are sections of the path that begin with `:`, and run to the next /
# File lib/http_api_builder/dsl.rb, line 79 def interpolated_params(path) path.split('/').reject { |i| i.length.zero? || i !~ /^:/ }.uniq.map { |i| i[1..-1].to_sym } end
Parse out and return the required and optional arguments
Required are any that are in the URL or in the required hash Optional are any other arguments.
# File lib/http_api_builder/dsl.rb, line 87 def requirements(path, params) required, optional = Hash(params).values_at(*%i(required optional)).map { |list| Array(list) } required += interpolated_params(path) optional += %i(form body json) [required, optional] end