Class: AbrLookup::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/abr_lookup/server.rb

Constant Summary collapse

DEFAULT_PATH =
/^\/abn_lookup(\..+)?$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ Server

Returns a new instance of Server.



7
8
9
10
# File 'lib/abr_lookup/server.rb', line 7

def initialize(app, opts={})
  @app = app
  @match_path = opts.fetch(:match_path, DEFAULT_PATH)
end

Instance Attribute Details

#match_pathObject (readonly)

Returns the value of attribute match_path.



4
5
6
# File 'lib/abr_lookup/server.rb', line 4

def match_path
  @match_path
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/abr_lookup/server.rb', line 12

def call(env)
  req = Rack::Request.new env
  if req.path =~ match_path
    lookup = Lookup.new(req.params['abn']).lookup_abn!
    body = lookup.to_json
    status = status_from_lookup(lookup)
    res = Rack::Response.new lookup.to_json, status, 'Content-Type' => 'application/json'
    res.finish
  else
    @app.call(env)
  end
end