aws_agcod 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZTJiYjU2ZTg4ZjBlNTQ2YjZmOWQ0MWQ2MTVhN2E1NzllYjkyY2YxZg==
4
+ OGZjMTRlM2Q2ZjhlYjMzZjdiMjE1NmIxNjk3NThjNjM5YTdmODc3YQ==
5
5
  data.tar.gz: !binary |-
6
- MGE1MGUxMTNiMjhmODMwMjQ1ZDdjNWIyYmQ4MzQ0NWMyODY4NDljOA==
6
+ MjZjNzkxNmZhZWVmYzA4OTQzNTUxNWFhMmFjYmNjMDM5MjQwN2Q3OQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NThjZDBiOTMwZTc3YTI0MTZmNjVjY2U3MDI3YWY3NjgwZjQ1Yzk2OWFlMzIx
10
- OGUzYmI3OWMxMjM2YjJjYWQxMjhhZTM0MDEzNjgyMmQ2NDZmMTI5ODJhZWRk
11
- ZmIwNTI4NDFmYzRkNjhlYWJjZTIxMzg0NDIzNThjNTVlNmZjN2Q=
9
+ ZDdlMGI0ZmQ0MGM3ZjhhNzNiZmY0YWVkMTZiNmI0NTY2MTVhNzYwM2VkM2Y4
10
+ NGM5NDIyZjI3YWUwZjAxYjkzZGJkZmNkOWZiZDA5MGM3ODEwOGM5ZmJhZTJh
11
+ YzdiZTc5YTFkNjIzYWY2OWU2OWRkZjNjY2UyNDU5MjM0M2I1ODM=
12
12
  data.tar.gz: !binary |-
13
- MzNkOGQ4ZTY5NjdhYzQ5YTBhN2NjOTJkM2ZiODgyODczODEwZGY3MzIxNGRk
14
- OTMyNjAxMTJhNTQxY2E0NTVhODIwMzM0ZGM4MDdjNjE2ZWUxYTViZDlmZDcw
15
- NjFjMThkMGExZTMyNThmZjJjNmNiNGI0ZjI5ZjlhN2E2MTFlMTA=
13
+ NmMyYzE5ODZiMjc0YThmMDQzY2U4ZDkzZWJhNThkZjI5NmM0MWIyNTMzNGU1
14
+ MmQ4M2QwODUxNDcwMGVjMzAyM2E3ZjE2Y2Q1Yjc4MTg2YWY0NTE3YTEzZDhj
15
+ OTI1YjRiZGY0Njk4OWQyYWE4NjAwM2YyN2Y5MmI3OTdhNThlYzY=
data/README.md CHANGED
@@ -20,8 +20,83 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- TODO: Write usage instructions here
23
+ #### Configure
24
24
 
25
+ ```ruby
26
+ require "aws_agcod"
27
+
28
+ AwsAgcod.configure do |config|
29
+ config.access_key = "YOUR_ACCESS_KEY"
30
+ config.secret_key = "YOUR_SECRET_KEY"
31
+ config.partner_id = "PARTNER_ID"
32
+ config.uri = "https://agcod-v2-gamma.amazon.com" # default, sandbox endpoint
33
+ config.partner_id = "us-east-1" # default
34
+ end
35
+ ```
36
+
37
+ #### Create Gift Code/Card
38
+
39
+ ```ruby
40
+ request_id = "test"
41
+ amount = 10 # USD
42
+
43
+ request = AwsAgcod::CreateGiftCard.new(request_id, amount)
44
+
45
+ # When succeed
46
+ if request.success?
47
+ request.claim_code # => code for the gift card
48
+ request.gc_id # => gift card id
49
+ request.request_id # => your request id
50
+ else
51
+ # When failed
52
+ request.error_message # => Error response from AGCOD service
53
+ end
54
+ ```
55
+
56
+ #### Cancel Gift Code/Card
57
+
58
+ ```ruby
59
+ request_id = "test"
60
+ gc_id = "test_gc_id"
61
+
62
+ request = AwsAgcod::CancelGiftCard.new(request_id, gc_id)
63
+
64
+ # When failed
65
+ unless request.success?
66
+ request.error_message # => Error response from AGCOD service
67
+ end
68
+ ```
69
+
70
+ #### Get Gift Code/Card activities
71
+
72
+ ```ruby
73
+ request_id = "test"
74
+ start_time = Time.now - 86400
75
+ end_time = Time.now
76
+ page = 1
77
+ per_page = 100
78
+ show_no_ops = false # Wehther to show activities with no operation or not
79
+
80
+ request = AwsAgcod::GiftCardActivityList.new(request_id, start_time, end_time, page, per_page, show_no_ops)
81
+
82
+ # When succeed
83
+ if request.success?
84
+ request.results.each do |activity|
85
+ activity.status # => SUCCESS, FAILURE, RESEND
86
+ activity.created_at
87
+ activity.type
88
+ activity.card_number
89
+ activity.amount
90
+ activity.error_code
91
+ activity.gc_id
92
+ activity.partner_id
93
+ activity.request_id
94
+ end
95
+ else
96
+ # When failed
97
+ request.error_message # => Error response from AGCOD service
98
+ end
99
+ ```
25
100
  ## Contributing
26
101
 
27
102
  1. Fork it ( https://github.com/[my-github-username]/aws_agcod/fork )
@@ -1,33 +1,19 @@
1
1
  require "aws_agcod/version"
2
+ require "aws_agcod/config"
2
3
  require "aws_agcod/create_gift_card"
3
4
  require "aws_agcod/cancel_gift_card"
5
+ require "aws_agcod/gift_card_activity_list"
4
6
 
5
7
  module AwsAgcod
6
- ROOT_PATH = if defined?(Rails)
7
- Rails.root
8
- else
9
- Pathname.new(__FILE__).join("../..").expand_path
10
- end
11
-
12
- def self.env
13
- if defined?(Rails)
14
- Rails.env
15
- else
16
- @enviroment || "development"
17
- end
18
- end
8
+ def self.configure(&block)
9
+ @config = Config.new
19
10
 
20
- def self.env=(enviroment)
21
- @enviroment = enviroment
22
- end
11
+ yield @config
23
12
 
24
- def self.config_file
25
- @config_file || ROOT_PATH.join("config/agcod.yml")
13
+ nil
26
14
  end
27
15
 
28
- def self.config_file=(file_path)
29
- if ROOT_PATH.join(file_path).exist?
30
- @config_file = ROOT_PATH.join(file_path)
31
- end
16
+ def self.config
17
+ @config
32
18
  end
33
19
  end
@@ -7,7 +7,10 @@ module AwsAgcod
7
7
  def_delegators :@response, :success?, :error_message
8
8
 
9
9
  def initialize(request_id, gc_id)
10
- @response = Request.new("CancelGiftCard", request_id, "gcId" => gc_id).response
10
+ @response = Request.new("CancelGiftCard",
11
+ "creationRequestId" => "#{AwsAgcod.config.partner_id}#{request_id}",
12
+ "gcId" => gc_id
13
+ ).response
11
14
  end
12
15
  end
13
16
  end
@@ -0,0 +1,11 @@
1
+ module AwsAgcod
2
+ class Config
3
+ attr_accessor :access_key, :secret_key, :uri, :partner_id, :region
4
+
5
+ def initialize
6
+ # API defaults
7
+ @uri = "https://agcod-v2-gamma.amazon.com"
8
+ @region = "us-east-1"
9
+ end
10
+ end
11
+ end
@@ -7,7 +7,8 @@ module AwsAgcod
7
7
  def_delegators :@response, :success?, :error_message
8
8
 
9
9
  def initialize(request_id, amount)
10
- @response = Request.new("CreateGiftCard", request_id,
10
+ @response = Request.new("CreateGiftCard",
11
+ "creationRequestId" => "#{AwsAgcod.config.partner_id}#{request_id}",
11
12
  "value" => {
12
13
  "currencyCode" => "USD",
13
14
  "amount" => amount
@@ -0,0 +1,54 @@
1
+ require "aws_agcod/request"
2
+
3
+ module AwsAgcod
4
+ class GiftCardActivityListError < StandardError; end
5
+
6
+ class GiftCardActivity
7
+ attr_reader :status, :created_at, :type, :card_number, :amount, :error_code,
8
+ :gc_id, :partner_id, :request_id
9
+
10
+ def initialize(payload)
11
+ @payload = payload
12
+ @status = payload["activityStatus"]
13
+ @created_at = payload["activityTime"]
14
+ @type = payload["activityType"]
15
+ @card_number = payload["cardNumber"]
16
+ @amount = payload["cardValue"]["amount"] if payload["cardValue"]
17
+ @error_code = payload["failureCode"]
18
+ @gc_id = payload["gcId"]
19
+ @partner_id = payload["partnerId"]
20
+ @request_id = payload["requestId"]
21
+ end
22
+
23
+ def is_real?
24
+ @payload["isRealOp"] == "true"
25
+ end
26
+ end
27
+
28
+ class GiftCardActivityList
29
+ extend Forwardable
30
+
31
+ LIMIT = 1000 # limit per request
32
+ TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
33
+
34
+ def_delegators :@response, :success?, :error_message
35
+
36
+ def initialize(request_id, start_time, end_time, page = 1, per_page = 100, show_no_ops = false)
37
+ raise GiftCardActivityError, "Only #{LIMIT} records allowed per request." if per_page > LIMIT
38
+
39
+ @response = Request.new("GetGiftCardActivityPage",
40
+ "requestId" => request_id,
41
+ "utcStartDate" => start_time.strftime(TIME_FORMAT),
42
+ "utcEndDate" => end_time.strftime(TIME_FORMAT),
43
+ "pageIndex" => (page - 1) * per_page,
44
+ "pageSize" => per_page,
45
+ "showNoOps" => show_no_ops
46
+ ).response
47
+ end
48
+
49
+ def results
50
+ @response["cardActivityList"].map { |payload| GiftCardActivity.new(payload) }
51
+ end
52
+ end
53
+ end
54
+
@@ -9,9 +9,8 @@ module AwsAgcod
9
9
 
10
10
  attr_reader :response
11
11
 
12
- def initialize(action, request_id, params)
12
+ def initialize(action, params)
13
13
  @action = action
14
- @request_id = request_id
15
14
  @params = params
16
15
 
17
16
  @response = Response.new(HTTParty.post(uri, body: body, headers: signed_headers, timeout: 30).body)
@@ -31,21 +30,16 @@ module AwsAgcod
31
30
  "date" => time.to_s
32
31
  }
33
32
 
34
- Signature.new(config).sign(uri, headers, body)
35
- end
36
-
37
- def config
38
- @config ||= YAML.load_file(AwsAgcod.config_file)[AwsAgcod.env]
33
+ Signature.new(AwsAgcod.config).sign(uri, headers, body)
39
34
  end
40
35
 
41
36
  def uri
42
- @uri ||= URI("#{config["uri"]}/#{@action}")
37
+ @uri ||= URI("#{AwsAgcod.config.uri}/#{@action}")
43
38
  end
44
39
 
45
40
  def body
46
41
  @body ||= @params.merge(
47
- "partnerId" => config["partner_id"],
48
- "creationRequestId" => "#{config["partner_id"]}#{@request_id}"
42
+ "partnerId" => AwsAgcod.config.partner_id
49
43
  ).to_json
50
44
  end
51
45
  end
@@ -11,7 +11,13 @@ module AwsAgcod
11
11
  # SUCCESS -- Operation succeeded
12
12
  # FAILURE -- Operation failed
13
13
  # RESEND -- A temporary/recoverable system failure that can be resolved by the partner retrying the request
14
- @status = payload["status"] ? payload["status"] : payload["agcodResponse"]["status"]
14
+ @status = if payload["status"]
15
+ payload["status"]
16
+ elsif payload["agcodResponse"]
17
+ payload["agcodResponse"]["status"]
18
+ else
19
+ "FAILURE"
20
+ end
15
21
  end
16
22
 
17
23
  def success?
@@ -9,12 +9,11 @@ require "pathname"
9
9
  module AwsAgcod
10
10
  class Signature
11
11
  SERVICE = "AGCODService"
12
- DEFAULT_REGION = "us-east-1"
13
12
 
14
13
  def initialize(credentials)
15
- @access_key = credentials["access_key"]
16
- @secret_key = credentials["secret_key"]
17
- @region = credentials["region"] || DEFAULT_REGION
14
+ @access_key = credentials.access_key
15
+ @secret_key = credentials.secret_key
16
+ @region = credentials.region || DEFAULT_REGION
18
17
  end
19
18
 
20
19
  def sign(uri, headers, body = "")
@@ -1,3 +1,3 @@
1
1
  module AwsAgcod
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws_agcod
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xenor Chang
@@ -85,7 +85,9 @@ files:
85
85
  - Rakefile
86
86
  - lib/aws_agcod.rb
87
87
  - lib/aws_agcod/cancel_gift_card.rb
88
+ - lib/aws_agcod/config.rb
88
89
  - lib/aws_agcod/create_gift_card.rb
90
+ - lib/aws_agcod/gift_card_activity_list.rb
89
91
  - lib/aws_agcod/request.rb
90
92
  - lib/aws_agcod/response.rb
91
93
  - lib/aws_agcod/signature.rb