diff --git a/config/routes.rb b/config/routes.rb index 3fe67be8..8e414caa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -26,6 +26,7 @@ # Example: :via => :get ====> :via => :get RedmineApp::Application.routes.draw do ## oauth相关 + match 'oauth', to: 'oauth#index' match 'oauth/authorize', to: 'oauth#authorize', :via => [:get, :post] match 'oauth/token', to: 'oauth#token', :via => :post diff --git a/lib/trustie/http/http.rb b/lib/trustie/http/http.rb new file mode 100644 index 00000000..be6099fe --- /dev/null +++ b/lib/trustie/http/http.rb @@ -0,0 +1,46 @@ +#coding=utf-8 + +require 'net/http' +require 'uri' + + +module Trustie + module Http + + def get(url) + uri = URI(url) + res = Net::HTTP.start(uri.host, uri.port, use_ssl: url.start_with?('https')) do |http| + req = Net::HTTP::Get.new(uri) + #req['Content-Type'] = 'application/json' + # The body needs to be a JSON string, use whatever you know to parse Hash to JSON + #req.body = {a: 1}.to_json + http.request(req) + end + + res.body + end + + def post(url, data=nil) + uri = URI(url) + res = Net::HTTP.start(uri.host, uri.port, use_ssl: url.start_with?('https')) do |http| + req = Net::HTTP::Post.new(uri) + #req['Content-Type'] = 'application/json' + # The body needs to be a JSON string, use whatever you know to parse Hash to JSON + req.body = data if data + http.request(req) + end + + res.body + end + + def decode(s) + begin + obj = ActiveSupport::JSON.decode(s) + rescue ActiveSupport::JSON.parse_error + logger.error("Attempted to decode invalid JSON: #{s}") + end + end + + + end +end \ No newline at end of file