rails-internet-marke 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8f6a896a9ca7a2b3fda450a450428abd1124bf0c17db3dc9d2404069684ada2c
4
+ data.tar.gz: 196755ce0a78e2a8b0194c99a6f40bdf5eebcdfb004fc388a0c4d852ba1d6b76
5
+ SHA512:
6
+ metadata.gz: 9b3c216f8027add5ad1ceca898dea7ca9d8122a0756c188f6263cb1d16326f2d0e4abe01a9cd0afde5a5c5c6704eef1e4f89dc1c762065b309d92f0228901c9c
7
+ data.tar.gz: c6b80727bdad19e98336d751482f3f129604d96bf62c0dce96369fa26746a2fa105f25b9c19a76be9facadec075305e075084cdbf6d6deedb347a9a17d81fb8a
data/lib/client.rb ADDED
@@ -0,0 +1,184 @@
1
+ module InternetMarke
2
+ class Client
3
+ attr_reader :authToken, :shoppingOrderId, :apiVersion
4
+ mattr_accessor :client_id, :client_secret, :username, :password
5
+
6
+ @@auth_type = "client_credentials"
7
+ @@api_version = "v1.1.14"
8
+
9
+ @@auth_url = "https://api-eu.dhl.com/post/de/shipping/im/v1/user"
10
+ @@api_url = "https://api-eu.dhl.com/post/de/shipping/im/v1/"
11
+ @@profile_url = "https://api-eu.dhl.com/post/de/shipping/im/v1/user/profile"
12
+ @@put_wallet_url = "https://api-eu.dhl.com/post/de/shipping/im/v1/app/wallet?amount="
13
+ @@get_wallet_url = "https://api-eu.dhl.com/post/de/shipping/im/v1/app/wallet?amount=0"
14
+ @@init_shopping_cart_url = "https://api-eu.dhl.com/post/de/shipping/im/v1/app/shoppingcart"
15
+ @@create_order_png_url = "https://api-eu.dhl.com/post/de/shipping/im/v1/app/shoppingcart/png"
16
+ @@create_order_pdf_url = "https://api-eu.dhl.com/post/de/shipping/im/v1/app/shoppingcart/pdf"
17
+
18
+ def initialize(client_id, client_secret, username, password)
19
+ @client_id = client_id
20
+ @client_secret = client_secret
21
+ @username = username
22
+ @password = password
23
+
24
+ self.check_api_version
25
+ if @apiVersion == @@api_version
26
+ @authToken = self.generate_auth_token
27
+ else
28
+ raise "selected api version #{@apiVersion} is not supported"
29
+ end
30
+ end
31
+
32
+ def get_expected_api_version
33
+ return @@api_version
34
+ end
35
+
36
+ def check_api_version
37
+ url = URI(@@api_url)
38
+ http = Net::HTTP.new(url.host, url.port)
39
+ http.use_ssl = true
40
+
41
+ request = Net::HTTP::Get.new(url)
42
+ @request = http.request(request)
43
+ @response = JSON.parse @request.read_body
44
+
45
+ @apiVersion = @response["amp"]["version"]
46
+ end
47
+
48
+ def generate_auth_token
49
+ url = URI(@@auth_url)
50
+ http = Net::HTTP.new(url.host, url.port)
51
+ http.use_ssl = true
52
+
53
+ request = Net::HTTP::Post.new(url)
54
+ request["content-type"] = 'application/x-www-form-urlencoded'
55
+ request["Content-Length"] = '0'
56
+ request["charset"] = 'utf-8'
57
+
58
+ bodyParams = [["grant_type", @@auth_type], ["client_id", @client_id], ["client_secret", @client_secret], ["username", @username], ["password", @password]]
59
+ request.body = URI.encode_www_form(bodyParams)
60
+
61
+ @request = http.request(request)
62
+ @response = JSON.parse @request.read_body
63
+ if @response
64
+ if @response["access_token"]
65
+ return @response["access_token"]
66
+ else
67
+ raise "authorization failed. no access token generated: #{@response}"
68
+ end
69
+ else
70
+ raise "authorization request failed: #{@response}"
71
+ end
72
+ end
73
+
74
+ def get_profile
75
+ if @authToken
76
+ url = URI(@@profile_url)
77
+ http = Net::HTTP.new(url.host, url.port)
78
+ http.use_ssl = true
79
+
80
+ request = Net::HTTP::Get.new(url)
81
+ request["Authorization"] = "Bearer " + @authToken
82
+
83
+ @request = http.request(request)
84
+
85
+ return JSON.parse @request.read_body
86
+ end
87
+ end
88
+
89
+ def init_shopping_cart
90
+ if @authToken
91
+ url = URI(@@init_shopping_cart_url)
92
+ http = Net::HTTP.new(url.host, url.port)
93
+ http.use_ssl = true
94
+
95
+ request = Net::HTTP::Post.new(url)
96
+ request["Authorization"] = "Bearer " + @authToken
97
+ request["Content-Length"] = '0'
98
+
99
+ @request = http.request(request)
100
+ @response = JSON.parse @request.read_body
101
+
102
+ if @response["shopOrderId"]
103
+ @shoppingOrderId = @response["shopOrderId"]
104
+ else
105
+ raise "no shopOrderId generated: #{@response}"
106
+ end
107
+ end
108
+ end
109
+
110
+ def put_wallet(amount)
111
+ if @authToken
112
+ url = URI(@@put_wallet_url + amount.to_s)
113
+ http = Net::HTTP.new(url.host, url.port)
114
+ http.use_ssl = true
115
+
116
+ request = Net::HTTP::Put.new(url)
117
+ request["Authorization"] = "Bearer " + @authToken
118
+
119
+ @request = http.request(request)
120
+
121
+ return JSON.parse @request.read_body
122
+ end
123
+ end
124
+
125
+ def get_wallet
126
+ if @authToken
127
+ url = URI(@@get_wallet_url)
128
+ http = Net::HTTP.new(url.host, url.port)
129
+ http.use_ssl = true
130
+
131
+ request = Net::HTTP::Put.new(url)
132
+ request["Authorization"] = "Bearer " + @authToken
133
+
134
+ @request = http.request(request)
135
+
136
+ return JSON.parse @request.read_body
137
+ end
138
+ end
139
+
140
+ def buy(quantity, productCode, productPrice)
141
+ self.init_shopping_cart
142
+
143
+ if @authToken && @shoppingOrderId
144
+ url = URI(@@create_order_png_url)
145
+ http = Net::HTTP.new(url.host, url.port)
146
+ http.use_ssl = true
147
+
148
+ request = Net::HTTP::Post.new(url)
149
+ request["content-type"] = 'application/json'
150
+ request["Authorization"] = "Bearer " + @authToken
151
+ request["Content-Length"] = '0'
152
+
153
+ positions = Array.new
154
+ position = {
155
+ "productCode": productCode,
156
+ "imageID": 0,
157
+ "additionalInfo": "",
158
+ "voucherLayout": "ADDRESS_ZONE",
159
+ "positionType": "AppShoppingCartPosition"
160
+ }
161
+
162
+ quantity.times do |i|
163
+ positions.push(position)
164
+ end
165
+
166
+ amount = quantity * (productPrice * 100).to_i
167
+ bodyParams = {
168
+ "type": "AppShoppingCartPNGRequest",
169
+ "shopOrderId": @shoppingOrderId,
170
+ "total": amount,
171
+ "createManifest": true,
172
+ "dpi": "DPI300",
173
+ "optimizePNG": true,
174
+ "positions": positions
175
+ }
176
+
177
+ request.body = bodyParams.to_json
178
+ @request = http.request(request)
179
+
180
+ return JSON.parse @request.read_body
181
+ end
182
+ end
183
+ end
184
+ end
@@ -0,0 +1,6 @@
1
+ require 'version'
2
+ require 'client'
3
+
4
+ module InternetMarke
5
+
6
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module InternetMarke
2
+ VERSION = "0.0.3"
3
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-internet-marke
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - cmetzner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-09-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Gem for use Deutsche Post InternetMarke Api in Ruby. Put money on your
14
+ wallet and buy products of Deutsche Post InternetMarke
15
+ email:
16
+ - cmetzner@christmetz-it.de
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/client.rb
22
+ - lib/internet_marke.rb
23
+ - lib/version.rb
24
+ homepage: https://github.com/cmetzner/rails-internet-marke
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubygems_version: 3.4.10
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Ruby Client for Deutsche Post InternetMarke Api
47
+ test_files: []