class EOS::Client
Public Class Methods
ar()
click to toggle source
# File lib/eosrb.rb, line 13 def self.ar new('https://api.eosargentina.io') end
new(server='http://localhost:8888', hc=false)
click to toggle source
# File lib/eosrb.rb, line 7 def initialize(server='http://localhost:8888', hc=false) @server = server @hc = hc load_specs end
Private Instance Methods
api_call(method_name, args)
click to toggle source
The actual http call
# File lib/eosrb.rb, line 80 def api_call(method_name, args) args ||= {} known = known_params(method_name) body = @hc ? args : args.select{ |key, _| known.include?(key.to_s) } post path(method_name), body end
connection()
click to toggle source
# File lib/eosrb.rb, line 19 def connection @connection ||= Faraday.new(@server){|f| f.adapter :net_http_persistent } end
extract_endpoint(name)
click to toggle source
# File lib/eosrb.rb, line 64 def extract_endpoint(name) name.to_s.split('_',2) end
known_params(method_name)
click to toggle source
# File lib/eosrb.rb, line 68 def known_params(method_name) api, endpoint = extract_endpoint(method_name) return {} unless @specs[api] && @specs[api][endpoint] @specs[api][endpoint]['params'] || {} end
load_specs()
click to toggle source
Load API specification from spec files
# File lib/eosrb.rb, line 29 def load_specs @specs = {} spec_files.each do |name| @specs[name] = read_spec(name) end end
method_missing(method_name, *args, &block)
click to toggle source
Add API methods to class for seamless usage
Calls superclass method
# File lib/eosrb.rb, line 50 def method_missing(method_name, *args, &block) if respond_to_missing?(method_name) api_call(method_name, args.first) else super end end
path(method_name)
click to toggle source
# File lib/eosrb.rb, line 74 def path(method_name) api, endpoint = extract_endpoint(method_name) "/#{version}/#{api}/#{endpoint}" end
post(path, body)
click to toggle source
# File lib/eosrb.rb, line 87 def post(path, body) headers = {'Content-Type' => 'application/json'} response = connection.post path, body.to_json, headers JSON.parse(response.body) end
read_spec(name)
click to toggle source
# File lib/eosrb.rb, line 44 def read_spec(name) JSON.parse(File.read("#{specs_path}#{name}.json")) end
respond_to_missing?(method_name, include_private = false)
click to toggle source
# File lib/eosrb.rb, line 58 def respond_to_missing?(method_name, include_private = false) return true if @hc api, endpoint = extract_endpoint(method_name) @specs.key?(api) && @specs[api].key?(endpoint) end
spec_files()
click to toggle source
# File lib/eosrb.rb, line 40 def spec_files Dir["#{specs_path}*"].map{|filename| File.basename(filename, '.json') }.compact end
specs_path()
click to toggle source
# File lib/eosrb.rb, line 36 def specs_path "#{File.dirname __dir__}/specs/" end
version()
click to toggle source
# File lib/eosrb.rb, line 23 def version 'v1' end