Class: InternetMarke::Client
- Inherits:
-
Object
- Object
- InternetMarke::Client
- Defined in:
- lib/client.rb
Constant Summary collapse
- @@auth_type =
"client_credentials"- @@api_version =
["v1.1.14", "v1.1.16", "v1.1.18"]
- @@auth_url =
"https://api-eu.dhl.com/post/de/shipping/im/v1/user"- @@api_url =
"https://api-eu.dhl.com/post/de/shipping/im/v1/"- @@profile_url =
"https://api-eu.dhl.com/post/de/shipping/im/v1/user/profile"- @@put_wallet_url =
"https://api-eu.dhl.com/post/de/shipping/im/v1/app/wallet?amount="- @@get_wallet_url =
"https://api-eu.dhl.com/post/de/shipping/im/v1/app/wallet?amount=0"- @@init_shopping_cart_url =
"https://api-eu.dhl.com/post/de/shipping/im/v1/app/shoppingcart"- @@create_order_png_url =
"https://api-eu.dhl.com/post/de/shipping/im/v1/app/shoppingcart/png"- @@create_order_pdf_url =
"https://api-eu.dhl.com/post/de/shipping/im/v1/app/shoppingcart/pdf"
Instance Attribute Summary collapse
-
#apiVersion ⇒ Object
readonly
Returns the value of attribute apiVersion.
-
#authToken ⇒ Object
readonly
Returns the value of attribute authToken.
-
#shoppingOrderId ⇒ Object
readonly
Returns the value of attribute shoppingOrderId.
Instance Method Summary collapse
- #buy(quantity, productCode, productPrice) ⇒ Object
- #check_api_version ⇒ Object
- #generate_auth_token ⇒ Object
- #get_expected_api_version ⇒ Object
- #get_profile ⇒ Object
- #get_wallet ⇒ Object
- #init_shopping_cart ⇒ Object
-
#initialize(client_id, client_secret, username, password) ⇒ Client
constructor
A new instance of Client.
- #put_wallet(amount) ⇒ Object
Constructor Details
#initialize(client_id, client_secret, username, password) ⇒ Client
Returns a new instance of Client.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/client.rb', line 18 def initialize(client_id, client_secret, username, password) @client_id = client_id @client_secret = client_secret @username = username @password = password self.check_api_version if @@api_version.include?(@apiVersion) @authToken = self.generate_auth_token else raise "selected api version #{@apiVersion} is not supported" end end |
Instance Attribute Details
#apiVersion ⇒ Object (readonly)
Returns the value of attribute apiVersion.
3 4 5 |
# File 'lib/client.rb', line 3 def apiVersion @apiVersion end |
#authToken ⇒ Object (readonly)
Returns the value of attribute authToken.
3 4 5 |
# File 'lib/client.rb', line 3 def authToken @authToken end |
#shoppingOrderId ⇒ Object (readonly)
Returns the value of attribute shoppingOrderId.
3 4 5 |
# File 'lib/client.rb', line 3 def shoppingOrderId @shoppingOrderId end |
Instance Method Details
#buy(quantity, productCode, productPrice) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/client.rb', line 140 def buy(quantity, productCode, productPrice) self.init_shopping_cart if @authToken && @shoppingOrderId url = URI(@@create_order_png_url) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Post.new(url) request["content-type"] = 'application/json' request["Authorization"] = "Bearer " + @authToken request["Content-Length"] = '0' positions = Array.new position = { "productCode": productCode, "imageID": 0, "additionalInfo": "", "voucherLayout": "ADDRESS_ZONE", "positionType": "AppShoppingCartPosition" } quantity.times do |i| positions.push(position) end amount = quantity * (productPrice * 100).to_i bodyParams = { "type": "AppShoppingCartPNGRequest", "shopOrderId": @shoppingOrderId, "total": amount, "createManifest": true, "dpi": "DPI300", "optimizePNG": true, "positions": positions } request.body = bodyParams.to_json @request = http.request(request) return JSON.parse @request.read_body end end |
#check_api_version ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/client.rb', line 36 def check_api_version url = URI(@@api_url) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Get.new(url) @request = http.request(request) @response = JSON.parse @request.read_body @apiVersion = @response["amp"]["version"].present? ? @response["amp"]["version"] : "undefined" end |
#generate_auth_token ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/client.rb', line 48 def generate_auth_token url = URI(@@auth_url) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Post.new(url) request["content-type"] = 'application/x-www-form-urlencoded' request["Content-Length"] = '0' request["charset"] = 'utf-8' bodyParams = [["grant_type", @@auth_type], ["client_id", @client_id], ["client_secret", @client_secret], ["username", @username], ["password", @password]] request.body = URI.encode_www_form(bodyParams) @request = http.request(request) @response = JSON.parse @request.read_body if @response if @response["access_token"] return @response["access_token"] else raise "authorization failed. no access token generated: #{@response}" end else raise "authorization request failed: #{@response}" end end |
#get_expected_api_version ⇒ Object
32 33 34 |
# File 'lib/client.rb', line 32 def get_expected_api_version return @@api_version end |
#get_profile ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/client.rb', line 74 def get_profile if @authToken url = URI(@@profile_url) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Get.new(url) request["Authorization"] = "Bearer " + @authToken @request = http.request(request) return JSON.parse @request.read_body end end |
#get_wallet ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/client.rb', line 125 def get_wallet if @authToken url = URI(@@get_wallet_url) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Put.new(url) request["Authorization"] = "Bearer " + @authToken @request = http.request(request) return JSON.parse @request.read_body end end |
#init_shopping_cart ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/client.rb', line 89 def init_shopping_cart if @authToken url = URI(@@init_shopping_cart_url) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Post.new(url) request["Authorization"] = "Bearer " + @authToken request["Content-Length"] = '0' @request = http.request(request) @response = JSON.parse @request.read_body if @response["shopOrderId"] @shoppingOrderId = @response["shopOrderId"] else raise "no shopOrderId generated: #{@response}" end end end |
#put_wallet(amount) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/client.rb', line 110 def put_wallet(amount) if @authToken url = URI(@@put_wallet_url + amount.to_s) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true request = Net::HTTP::Put.new(url) request["Authorization"] = "Bearer " + @authToken @request = http.request(request) return JSON.parse @request.read_body end end |