Class: Rack::Passthrough::Endpoint
- Inherits:
-
Object
- Object
- Rack::Passthrough::Endpoint
- Defined in:
- lib/rack/passthrough.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(endpoint) ⇒ Endpoint
constructor
A new instance of Endpoint.
- #to_normal_string(data) ⇒ Object
Constructor Details
#initialize(endpoint) ⇒ Endpoint
Returns a new instance of Endpoint.
8 9 10 11 |
# File 'lib/rack/passthrough.rb', line 8 def initialize( endpoint ) raise "Required api endpoint path." if endpoint.blank? @endpoint = endpoint end |
Instance Method Details
#call(env) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rack/passthrough.rb', line 23 def call(env) request = Rack::Request.new(env) full_path = URI::join( @endpoint, request.path ) # Set Query String full_path.query = request.query_string data = request.body.read conn = HTTParty.method( request.request_method.downcase.to_sym ) # Handle form-encoded or application/json if env["action_dispatch.request.request_parameters"].blank? body = Rack::Utils.parse_query( data ) unless data.blank? else body = env["action_dispatch.request.request_parameters"] unless data.blank? end response = conn.call( full_path.to_s, { body: body } ) # Headers headers = {} response.headers.each do |key,value| headers.merge!({ key => to_normal_string(value) }) end # Fix no response headers.except!('transfer-encoding') [ response.code , headers , [response.body] ] end |
#to_normal_string(data) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/rack/passthrough.rb', line 13 def to_normal_string(data) if data.class == Array data = data.join else data end data end |