class MT::DataAPI::Client::Endpoint
Send request to endpoint.
Attributes
api_url[W]
client_id[W]
verb[R]
Public Class Methods
new(hash)
click to toggle source
# File lib/mt/data_api/client/endpoint.rb, line 18 def initialize(hash) raise ArgumentError, 'parameter should be hash' unless hash.is_a? Hash hash = hash.symbolize_keys @id = hash[:id] @route = hash[:route] @version = hash[:version] @verb = hash[:verb] raise ArgumentError, "invalid verb: #{@verb}" unless valid_verb? end
Public Instance Methods
call(access_token = nil, args = {})
click to toggle source
# File lib/mt/data_api/client/endpoint.rb, line 30 def call(access_token = nil, args = {}) res = APIRequest.new(self).send( access_token, client_id_hash.clone.merge(args) ) return nil if res.body.nil? JSON.parse(res.body) end
request_url(args)
click to toggle source
# File lib/mt/data_api/client/endpoint.rb, line 39 def request_url(args) url = self.class.instance_variable_get(:@api_url) + route(args) url += query_string(args) if @verb == 'GET' url end
Private Instance Methods
auth_id?()
click to toggle source
# File lib/mt/data_api/client/endpoint.rb, line 47 def auth_id? %w[authenticate authorize].include?(@id) end
client_id_hash()
click to toggle source
# File lib/mt/data_api/client/endpoint.rb, line 51 def client_id_hash if auth_id? { clientId: self.class.instance_variable_get(:@client_id) } else {} end end
query_string(args = {})
click to toggle source
# File lib/mt/data_api/client/endpoint.rb, line 70 def query_string(args = {}) return '' if args.empty? query = args.keys.sort.map do |key| [key, args[key]].join('=') end '?' + query.join('&') end
route(args = {})
click to toggle source
# File lib/mt/data_api/client/endpoint.rb, line 59 def route(args = {}) route = @route.dup route.scan(%r{:[^:/]+(?=/|$)}).each do |m| key = m.sub(/^:/, '').to_sym value = args.delete(key) raise ArgumentError, %(parameter "#{key}" is required) unless value route.sub!(/#{m}/, value.to_s) end route end
valid_verb?()
click to toggle source
# File lib/mt/data_api/client/endpoint.rb, line 78 def valid_verb? %w[GET POST PUT DELETE].include? @verb end