Module: Ballparc

Defined in:
lib/ballparc.rb,
lib/ballparc/version.rb

Constant Summary collapse

PROD_SERVER_DOMAIN =
'https://api.ballparc.com/enforcement'
MOCK_SERVER_DOMAIN =
'https://private-anon-b4097ff57b-ballparcv2.apiary-mock.com/enforcement'
VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.add_headersObject



74
75
76
77
78
79
# File 'lib/ballparc.rb', line 74

def self.add_headers
  @req['accept'] = 'application/x.ballparc.v2+json'
  @req['content_type'] = 'application/json'
  @req['x-api-key'] = ENV['BALLPARC_KEY']
  @req['x-api-secret'] = ENV['BALLPARC_SECRET']
end

.check_endpoint(for_debug) ⇒ Object



81
82
83
# File 'lib/ballparc.rb', line 81

def self.check_endpoint(for_debug)
  @endpoint = for_debug ? MOCK_SERVER_DOMAIN : PROD_SERVER_DOMAIN
end

.create_parking_session(body_data, for_debug = false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ballparc.rb', line 12

def self.create_parking_session(body_data, for_debug = false)
  check_endpoint(for_debug)
  @endpoint += '/paid_vehicles?'
  body_data.each do |key, value|
    @endpoint += "#{key}=#{value}&"
  end
  @endpoint.chop!

  url = URI(@endpoint)
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  @req = Net::HTTP::Post.new(url)
  add_headers
  response = http.request(@req)

  result = JSON.parse(response.body)
  if response.code == '200' && result['data'].present?
    { message: 'Parking session created.', ballparc_result: result, status: :ok }
  else
    { message: 'Parking session is not created.', ballparc_result: result, status: :unauthorized }
  end
rescue StandardError => e
  { message: e.message, status: :internal_server_error }
end

.deactivate_parking_session(paid_vehicle_id, for_debug = false) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ballparc.rb', line 56

def self.deactivate_parking_session(paid_vehicle_id, for_debug = false)
  check_endpoint(for_debug)
  url = URI("#{@endpoint}/paid_vehicles/#{paid_vehicle_id}")
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  @req = Net::HTTP::Delete.new(url)
  add_headers
  response = http.request(@req)

  if response.code == '204'
    { message: 'Successfully deactivated paid vehicle', result_code: response.code.to_i, status: :ok }
  else
    { message: 'Unable to deactivate paid vehicle', result_code: response.code.to_i, status: :unauthorized }
  end
rescue StandardError => e
  { message: e.message, status: :internal_server_error }
end

.view_violation(data, for_debug = false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ballparc.rb', line 37

def self.view_violation(data, for_debug = false)
  check_endpoint(for_debug)
  url = URI("#{@endpoint}/violations?start_date=#{data[:start_date]}&end_date=#{data[:end_date]}&plate_number=#{data[:plate_number]}")
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  @req = Net::HTTP::Get.new(url)
  add_headers
  response = http.request(@req)

  result = JSON.parse(response.body)
  if response.code == '200' && result['data'].present?
    { message: 'Violation found.', ballparc_result: result, status: :ok }
  else
    { message: 'Violation not found.', ballparc_result: result, status: :unauthorized }
  end
rescue StandardError => e
  { message: e.message, status: :internal_server_error }
end