Class: Swisspost::PriceService

Inherits:
BaseService show all
Defined in:
lib/swisspost/price_service.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

API_URL =
"https://vsc.apis.post.ch/product/v1"
PRODUCTS_VALIDATOR =
Dry::Validation.Contract do
  VALID_FORMATS = %w[B4 B5 MB PK SP RL FF].freeze
  VALID_COUNTRIES = %w[AF EG AX AL DZ VI UM AD AO AI AQ AG GQ AR AM AW AZ ET AU BS BH BD BB BY BE BZ BJ BM BT BO BQ BA BW BV BR IO BN BG BF BI KY CL CN CK CR CW DK DE DJ DM DO EC CI SV ER EE SZ FK FO FJ FI FR GF PF TF GA GM GE GH GI GD GR GL GB GP GU GT GG GN GW GY HT HM HN HK IN ID IQ IR IE IS IL IT JM JP YE JE JO KH CM CA CV KZ KE KG KI CC CO KM CD CG KP KR XZ HR CU KW LA LS LV LB LR LY LI LT LU MO MG MW MY MV ML MT IM MP MA MH MQ MR MU YT MX FM MD MC MN ME MS MZ MM NA NR NP NC NZ NI NL NE NG NU MK NF NO OM AT PK PS PW PA PG PY PE PH PN PL PT PR QA RE RO RU RW SB ZM AS WS SM SA SE CH SN RS SC SL SG SK SI SO ES LK BL KN SH LC SX MF PM ST VC ZA SD GS SS SR SJ SY TJ TW TZ TH TL TG TK TO TT TD CZ TN TR TM TC TV UG UA HU UY US UZ VU VA VE AE US VN VG WF CX EH CF ZW CY].freeze

  params do
    required(:country).value(:string, included_in?: VALID_COUNTRIES)
    required(:format).filled(:string, included_in?: VALID_FORMATS)
    required(:weight).value(:integer, gteq?: 0)
  end
end

Instance Method Summary collapse

Methods inherited from BaseService

#access_token, #httpx_apikey, #httpx_oauth

Instance Method Details

#products(country, format, weight) ⇒ Object

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/swisspost/price_service.rb', line 20

def products(country, format, weight)
  path = "/products"

  result = PRODUCTS_VALIDATOR.call(country: country, format: format, weight: weight)
  raise ValidationError, result.errors.to_h.inspect if result.failure?

  response = httpx_apikey.get(API_URL + path, params: {
    isoCode: country,
    format: format,
    weight: weight,
  })

  response.raise_for_status

  result = response.json

  return result
end