promotions_evaluation 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a67901318b39a67f6cfe1b9da7e49075e28c2a3d15f9a863ec7231b627e8b0be
4
- data.tar.gz: 16f8f96b60caba3ebd28f86d52e86226f6bcec5b2866a7ab156ec087316dbf50
3
+ metadata.gz: d003760910d88b211d7eea4706830481fbc6b4f77813aeb2b3e85f4e5279f1d9
4
+ data.tar.gz: 323a03734a55e003656f73d61e6a1b5812a260252f8a78e29d63fb7977519d7f
5
5
  SHA512:
6
- metadata.gz: 912b4dfa0e25a311f873b5149686aa62938d2c20ba31dd9ddc4a08d6cce948b2c2213a7111cc98a004661530f4df2672cae5559778f0c52c74877abb35f2e7fb
7
- data.tar.gz: 6415943d5c021d73fbf4770cdabd918c2c1396e22df54dfbf590e48e7ba41545563ed94ad76355b34ab6237d11c6674060be0945844a6007993fb745f67dc2c4
6
+ metadata.gz: a0f5fa36262c54e9e762569a92956e2fff11469786c78756fde9d892134dd87e3c66fedbe7c84e33dccd947157ebbe53653de917f985299c98dfa1569d31ec2f
7
+ data.tar.gz: 79350dd68631a9cf7272aa9b4d06ff83b444ca2611480e8bf404a2a85a78e4d7242d5a960d540ecc7ac805a7e87998a4b4c402e0cd4d989e1a77715fec2038e8
@@ -4,8 +4,43 @@ require "./lib/promotions_evaluation/promotions_service.rb"
4
4
  module PromotionsEvaluation end
5
5
 
6
6
  class PromotionsEvaluator
7
- def self.perform_evaluation(url, route, arguments)
8
- service = PromotionsService.new(url, route)
9
- service.evaluate(arguments)
7
+ def self.perform_evaluation(promotion_code, evaluation_args)
8
+ return PromotionsService.instance.evaluate(promotion_code, evaluation_args)
9
+ end
10
+ end
11
+
12
+ class EvaluationParameters
13
+
14
+ def initialize(args)
15
+ @app_key = args[:app_key]
16
+ @coupon_code = args[:coupon_code]
17
+ @user_id = args[:user_id]
18
+ @attributes = args[:attributes]
19
+ @country = args[:country]
20
+ @city = args[:city]
21
+ @birth_date = args[:birth_date]
22
+ @transaction_id = args[:transaction_id]
23
+ end
24
+
25
+ def generate_request_body()
26
+
27
+ base_body = {
28
+ token: @app_key,
29
+ attributes: @attributes,
30
+ demography: {
31
+ country: @country,
32
+ city: @city,
33
+ birth_date: @birth_date
34
+ }
35
+ }
36
+
37
+ if !@transaction_id.nil?
38
+ base_body[:transaction_id] = @transaction_id
39
+ else
40
+ base_body[:coupon_code] = @coupon_code,
41
+ base_body[:user] = @user_id
42
+ end
43
+
44
+ return base_body
10
45
  end
11
46
  end
@@ -4,22 +4,32 @@ require_relative './evaluation_response.rb'
4
4
 
5
5
  class PromotionsService
6
6
 
7
- def initialize(url, route)
8
- @promotions_server_url = url
9
- @promotions_server_route = route
10
- @connection = create_connection
7
+ PROMOTIONS_SERVER_URL = 'https://coupons-evaluation.herokuapp.com'
8
+ PROMOTIONS_SERVER_ROUTE_1 = '/v1/promotions/'
9
+ PROMOTIONS_SERVER_ROUTE_2 = '/evaluate'
10
+ SERVER_ERROR_MESAGE_KEY = 'error'
11
+
12
+
13
+ def self.instance
14
+ @instance = @instance || PromotionsService.new()
15
+ return @instance
11
16
  end
12
17
 
13
- def evaluate(body)
14
- response = post @promotions_server_route, body
15
- puts "La respuesta es #{response.inspect}"
18
+ def evaluate(promo_code, body)
19
+ route = PROMOTIONS_SERVER_ROUTE_1 + promo_code + PROMOTIONS_SERVER_ROUTE_2
20
+ response = post route, body.generate_request_body()
16
21
  end
17
22
 
18
23
  private
24
+
25
+ def initialize()
26
+ @connection = create_connection
27
+ end
28
+
19
29
 
20
30
  def create_connection
21
31
 
22
- conn = Faraday.new(url: @promotions_server_url) do |c|
32
+ conn = Faraday.new(url: PROMOTIONS_SERVER_URL) do |c|
23
33
  c.response :logger
24
34
  c.request :json
25
35
  c.use Faraday::Adapter::NetHttp
@@ -35,7 +45,7 @@ def post(url, payload)
35
45
  request.headers['Content-Type'] = 'application/json'
36
46
  request.body = payload.to_json
37
47
  end
38
-
48
+
39
49
  return create_response(resp)
40
50
 
41
51
  rescue Faraday::Error::ConnectionFailed => e
@@ -46,7 +56,7 @@ def create_response(resp)
46
56
  body = JSON.parse(resp.body)
47
57
  successful = resp.status < 300
48
58
  if ! successful
49
- response = EvaluationResponse.new(success: successful, message: body['error_message'])
59
+ response = EvaluationResponse.new(success: successful, message: body[SERVER_ERROR_MESAGE_KEY])
50
60
  else
51
61
  response = EvaluationResponse.new(success: successful, message: 'Success!', payload: body)
52
62
  end
@@ -1,3 +1,3 @@
1
1
  module PromotionsEvaluation
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: promotions_evaluation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - '='
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-23 00:00:00.000000000 Z
11
+ date: 2019-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler