Module: Caselaw::Request

Included in:
Client
Defined in:
lib/caselaw/request.rb

Constant Summary collapse

API_ROOT =
"https://api.case.law/v1/"

Instance Method Summary collapse

Instance Method Details

#paginated_request(path, num) ⇒ Object



21
22
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
# File 'lib/caselaw/request.rb', line 21

def paginated_request(path, num)
  token = api_key || Caselaw.configuration[:api_key]
  fail(Caselaw::ConfigurationError, "API key is required.") if token.nil?

  count = 0
  pageCount = 1
  nextPageExists = true
  tempArray = []
  path = API_ROOT + path
  
  initialRes = HTTParty.get(path, headers: {"Authorization" => "Token " + token})
  initialRes = initialRes.parsed_response

  while nextPageExists && count <= num
    puts "-- | Page " + pageCount.to_s
    res = HTTParty.get(path, headers: {"Authorization" => "Token " + token})
    res = res.parsed_response
    data = res['results']
    
    count += data.count
    pageCount += 1
    tempArray.push(*data)
    res['next'] != nil ? path = res['next'] : nextPageExists = false
  end
  tempArray = tempArray[0, num] if tempArray.count > num
  tempArray
end

#parsed_response(res) ⇒ Object



49
50
51
# File 'lib/caselaw/request.rb', line 49

def parsed_response(res)
  res.parsed_response
end

#request(path) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/caselaw/request.rb', line 8

def request(path)
  token = api_key || Caselaw.configuration[:api_key]
  fail(Caselaw::ConfigurationError, "API key is required.") if token.nil?

  res = HTTParty.get(API_ROOT + path, headers: {"Authorization" => "Token " + token}) 
    case res.code
    when 200
      parsed_response(res)
    when 404
      fail(Caselaw::NotFound)
    end
end

#slug(jurisdiction_term) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/caselaw/request.rb', line 53

def slug(jurisdiction_term)
  if jurisdiction_term.is_a?(String)
    Caselaw::Jurisdiction.get_by_name(jurisdiction_term)
  elsif jurisdiction_term.is_a?(Integer)
    Caselaw::Jurisdiction.get_by_id(jurisdiction_term)
  end
end