class RubyPtvApi::Base
Attributes
coord_format[RW]
profile[RW]
Public Instance Methods
connection()
click to toggle source
# File lib/ruby_ptv_api/base.rb, line 58 def connection Faraday.new(url: endpoint) do |faraday| faraday.request :url_encoded # form-encode POST params #faraday.response :logger # log requests to STDOUT faraday.adapter Faraday.default_adapter # make requests with Net::HTTP end end
default_params()
click to toggle source
# File lib/ruby_ptv_api/base.rb, line 66 def default_params { callerContext: { properties: [ { key: 'Profile', value: 'car' }, { key: 'CoordFormat', value: coord_format || :PTV_GEODECIMAL } ] } } end
demodulize(path)
click to toggle source
Avoid active support import
# File lib/ruby_ptv_api/base.rb, line 32 def demodulize(path) path = path.to_s if i = path.rindex('::') path[(i+2)..-1] else path end end
demodulized_name()
click to toggle source
# File lib/ruby_ptv_api/base.rb, line 27 def demodulized_name demodulize(self.class.name) end
endpoint()
click to toggle source
# File lib/ruby_ptv_api/base.rb, line 5 def endpoint RubyPtvApi.config.send("#{parent_module_name.downcase}_endpoint") end
parent_module_name()
click to toggle source
# File lib/ruby_ptv_api/base.rb, line 21 def parent_module_name name = self.class.name.split('::')[-2] return nil unless name underscore(name) end
post(params)
click to toggle source
# File lib/ruby_ptv_api/base.rb, line 45 def post(params) params.merge!(default_params) #p params.to_json response = connection.post do |req| req.url "#{ptv_path}/#{ptv_function}" req.headers['Content-Type'] = 'application/json; charset=utf-8' req.body = Oj.dump(params) end body = Oj.load(response.body) #p body (200..300).include?(response.status) ? body : raise(BadResponse.new(response.body)) end
ptv_function()
click to toggle source
# File lib/ruby_ptv_api/base.rb, line 17 def ptv_function raise "Not implemented PTV function in #{demodulized_name}" end
ptv_path()
click to toggle source
# File lib/ruby_ptv_api/base.rb, line 13 def ptv_path raise "Not implemented PTV path in #{demodulized_name}" end
underscore(path)
click to toggle source
# File lib/ruby_ptv_api/base.rb, line 41 def underscore(path) path.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2').downcase end
uri()
click to toggle source
# File lib/ruby_ptv_api/base.rb, line 9 def uri "#{endpoint}/#{ptv_path}/#{ptv_function}" end
Protected Instance Methods
multiple_response(responses)
click to toggle source
# File lib/ruby_ptv_api/base.rb, line 97 def multiple_response(responses) lists = [] responses.each do |response| lists << response_class.new(response) end lists end
parse_response(response)
click to toggle source
# File lib/ruby_ptv_api/base.rb, line 85 def parse_response(response) if response.is_a? Array multiple_response(response) else single_response(response) end end
single_response(response)
click to toggle source
# File lib/ruby_ptv_api/base.rb, line 93 def single_response(response) response_class.new(response) end