Class: PacerApi::Session

Inherits:
Object
  • Object
show all
Includes:
Translation
Defined in:
lib/pacer_api/session.rb

Constant Summary collapse

HOSTS =
{
  production: "pcl.uscourts.gov",
  qa: "qa-pcl.uscourts.gov"
}.freeze
BASE_PATH =
"/pcl-public-api/rest/"

Instance Method Summary collapse

Methods included from Translation

#decode_response, #encode_request

Constructor Details

#initialize(token, environment: :production) ⇒ Session

Returns a new instance of Session.



19
20
21
22
23
24
25
# File 'lib/pacer_api/session.rb', line 19

def initialize(token, environment: :production)
  @token = token
  @uri = URI::HTTPS.build(
    host: HOSTS.fetch(environment),
    path: BASE_PATH
  )
end

Instance Method Details

#delete(path) ⇒ Object



47
48
49
50
# File 'lib/pacer_api/session.rb', line 47

def delete(path)
  res = request(:delete, path)
  res.status == 204
end

#get(path) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/pacer_api/session.rb', line 27

def get(path)
  res = request(:get, path)
  decode_response(res.body)
rescue PacerApi::Error => e
  raise ResponseError,
    "Error in GET #{path}: #{e.message}", e.backtrace
end

#post(path, doc) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pacer_api/session.rb', line 35

def post(path, doc)
  res = request(:post, path) { |req|
    req.headers["Content-Type"] = "application/json"
    req.body = encode_request(doc)
  }
  decode_response(res.body)
rescue PacerApi::Error => e
  raise ResponseError,
    "Error in POST #{path} with #{doc.inspect}: #{e.message}",
    e.backtrace
end

#request(verb, path, &blk) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/pacer_api/session.rb', line 52

def request(verb, path, &blk)
  res = Faraday.public_send(verb, @uri.merge(path)) { |req|
    req.headers["Accept"] = "application/json"
    req.headers["X-NEXT-GEN-CSO"] = @token
    blk.call(req) if block_given?
  }
  update_token! res.headers
  res
end