aus_post_api 1.0.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.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +2 -0
  3. data/.rspec +1 -0
  4. data/Gemfile +7 -0
  5. data/Gemfile.lock +37 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +147 -0
  8. data/Rakefile +32 -0
  9. data/aus_post_api.gemspec +14 -0
  10. data/lib/aus_post_api.rb +7 -0
  11. data/lib/aus_post_api/dce.rb +15 -0
  12. data/lib/aus_post_api/dce/endpoint.rb +80 -0
  13. data/lib/aus_post_api/dce/validate_australian_address.rb +12 -0
  14. data/lib/aus_post_api/endpoint.rb +66 -0
  15. data/lib/aus_post_api/endpoint/attributes.rb +54 -0
  16. data/lib/aus_post_api/pac.rb +83 -0
  17. data/lib/aus_post_api/pac/country.rb +9 -0
  18. data/lib/aus_post_api/pac/domestic_letter_size.rb +9 -0
  19. data/lib/aus_post_api/pac/domestic_letter_thickness.rb +9 -0
  20. data/lib/aus_post_api/pac/domestic_letter_weight.rb +9 -0
  21. data/lib/aus_post_api/pac/domestic_parcel_size.rb +9 -0
  22. data/lib/aus_post_api/pac/domestic_parcel_type.rb +9 -0
  23. data/lib/aus_post_api/pac/domestic_parcel_weight.rb +9 -0
  24. data/lib/aus_post_api/pac/domestic_postcode_search.rb +12 -0
  25. data/lib/aus_post_api/pac/endpoint.rb +76 -0
  26. data/lib/aus_post_api/pac/international_letter_weight.rb +9 -0
  27. data/lib/aus_post_api/pac/international_parcel_weight.rb +9 -0
  28. data/lib/aus_post_api/pac/postage_letter_domestic_calculate.rb +12 -0
  29. data/lib/aus_post_api/pac/postage_letter_domestic_service.rb +11 -0
  30. data/lib/aus_post_api/pac/postage_letter_international_calculate.rb +12 -0
  31. data/lib/aus_post_api/pac/postage_letter_international_service.rb +11 -0
  32. data/lib/aus_post_api/pac/postage_parcel_domestic_calculate.rb +13 -0
  33. data/lib/aus_post_api/pac/postage_parcel_domestic_service.rb +12 -0
  34. data/lib/aus_post_api/pac/postage_parcel_international_calculate.rb +12 -0
  35. data/lib/aus_post_api/pac/postage_parcel_international_service.rb +11 -0
  36. data/lib/aus_post_api/uri_handler.rb +35 -0
  37. data/spec/aus_post_api/dce/endpoint_spec.rb +86 -0
  38. data/spec/aus_post_api/dce/validate_australian_address_spec.rb +18 -0
  39. data/spec/aus_post_api/dce_spec.rb +33 -0
  40. data/spec/aus_post_api/endpoint/attributes_spec.rb +35 -0
  41. data/spec/aus_post_api/endpoint_spec.rb +43 -0
  42. data/spec/aus_post_api/pac/country_spec.rb +8 -0
  43. data/spec/aus_post_api/pac/domestic_letter_size_spec.rb +8 -0
  44. data/spec/aus_post_api/pac/domestic_letter_thickness_spec.rb +8 -0
  45. data/spec/aus_post_api/pac/domestic_letter_weight_spec.rb +8 -0
  46. data/spec/aus_post_api/pac/domestic_parcel_size_spec.rb +8 -0
  47. data/spec/aus_post_api/pac/domestic_parcel_type_spec.rb +8 -0
  48. data/spec/aus_post_api/pac/domestic_parcel_weight_spec.rb +8 -0
  49. data/spec/aus_post_api/pac/endpoint_spec.rb +109 -0
  50. data/spec/aus_post_api/pac/international_letter_weight_spec.rb +8 -0
  51. data/spec/aus_post_api/pac/international_parcel_weight_spec.rb +8 -0
  52. data/spec/aus_post_api/pac/postage_letter_domestic_calculate_spec.rb +19 -0
  53. data/spec/aus_post_api/pac/postage_letter_domestic_service_spec.rb +15 -0
  54. data/spec/aus_post_api/pac/postage_letter_international_calculate_spec.rb +20 -0
  55. data/spec/aus_post_api/pac/postage_letter_international_service_spec.rb +13 -0
  56. data/spec/aus_post_api/pac/postage_parcel_domestic_calculate_spec.rb +24 -0
  57. data/spec/aus_post_api/pac/postage_parcel_domestic_service_spec.rb +17 -0
  58. data/spec/aus_post_api/pac/postage_parcel_international_calculate_spec.rb +15 -0
  59. data/spec/aus_post_api/pac/postage_parcel_international_service_spec.rb +8 -0
  60. data/spec/aus_post_api/pac/postcode_search_spec.rb +17 -0
  61. data/spec/aus_post_api/pac_spec.rb +101 -0
  62. data/spec/aus_post_api/uri_handler_spec.rb +14 -0
  63. data/spec/shared_examples/shared_examples_for_endpoint_classes.rb +37 -0
  64. data/spec/spec_helper.rb +41 -0
  65. metadata +136 -0
@@ -0,0 +1,54 @@
1
+ module AusPostAPI
2
+ class Endpoint
3
+ # The attributes module adds a class level DSL that allows required and
4
+ # optional attributes to be set using the same interface as `attr_accessor`
5
+ #
6
+ # eg.
7
+ # class Example
8
+ # include Attributes
9
+ #
10
+ # required_attributes :one, :two
11
+ # optional_attributes :three
12
+ # end
13
+ #
14
+ # Additionally it adds two instance methods: `required_attributes` &
15
+ # `optional_attributes` that return an array of attributes specified by the
16
+ # two methods.
17
+ module Attributes
18
+ def self.included(base)
19
+ base.extend(ClassMethods)
20
+ base.include(InstanceMethods)
21
+ end
22
+ end
23
+
24
+ module ClassMethods
25
+ def required_attributes(*args)
26
+ if args.empty?
27
+ @required_attributes || []
28
+ else
29
+ @required_attributes = args
30
+ self.send(:attr_accessor, *args)
31
+ end
32
+ end
33
+
34
+ def optional_attributes(*args)
35
+ if args.empty?
36
+ @optional_attributes || []
37
+ else
38
+ @optional_attributes = args
39
+ self.send(:attr_accessor, *args)
40
+ end
41
+ end
42
+ end
43
+
44
+ module InstanceMethods
45
+ def required_attributes
46
+ self.class.required_attributes
47
+ end
48
+
49
+ def optional_attributes
50
+ self.class.optional_attributes
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,83 @@
1
+ module AusPostAPI
2
+ # The PAC class implements methods specified by the Australia Post Postage
3
+ # Assessment Calculator. A config hash must be supplied that specifies a valid
4
+ # PAC_AUTH_KEY, and request format. Setting the key TEST to true in the config
5
+ # hash will ignore any supplied auth_key and use the test endpoint.
6
+ class PAC
7
+ def initialize(config)
8
+ @config = config
9
+ end
10
+
11
+ def domestic_postcode_search(params = {})
12
+ AusPostAPI::PAC::DomesticPostcodeSearch.new(params, @config).execute
13
+ end
14
+
15
+ def country(params = {})
16
+ AusPostAPI::PAC::Country.new(params, @config).execute
17
+ end
18
+
19
+ def domestic_letter_thickness(params = {})
20
+ AusPostAPI::PAC::DomesticLetterThickness.new(params, @config).execute
21
+ end
22
+
23
+ def domestic_letter_weight(params = {})
24
+ AusPostAPI::PAC::DomesticLetterWeight.new(params, @config).execute
25
+ end
26
+
27
+ def domestic_letter_size(params = {})
28
+ AusPostAPI::PAC::DomesticLetterSize.new(params, @config).execute
29
+ end
30
+
31
+ def international_letter_weight(params = {})
32
+ AusPostAPI::PAC::InternationalLetterWeight.new(params, @config).execute
33
+ end
34
+
35
+ def international_parcel_weight(params = {})
36
+ AusPostAPI::PAC::InternationalParcelWeight.new(params, @config).execute
37
+ end
38
+
39
+ def domestic_parcel_weight(params = {})
40
+ AusPostAPI::PAC::DomesticParcelWeight.new(params, @config).execute
41
+ end
42
+
43
+ def domestic_parcel_type(params = {})
44
+ AusPostAPI::PAC::DomesticParcelType.new(params, @config).execute
45
+ end
46
+
47
+ def domestic_parcel_size(params = {})
48
+ AusPostAPI::PAC::DomesticParcelSize.new(params, @config).execute
49
+ end
50
+
51
+ def postage_letter_domestic_service(params = {})
52
+ AusPostAPI::PAC::PostageLetterDomesticService.new(params, @config).execute
53
+ end
54
+
55
+ def postage_parcel_domestic_service(params = {})
56
+ AusPostAPI::PAC::PostageParcelDomesticService.new(params, @config).execute
57
+ end
58
+
59
+ def postage_letter_international_service(params = {})
60
+ AusPostAPI::PAC::PostageLetterInternationalService.new(params, @config).execute
61
+ end
62
+
63
+ def postage_parcel_international_service(params = {})
64
+ AusPostAPI::PAC::PostageParcelInternationalService.new(params, @config).execute
65
+ end
66
+
67
+ def postage_parcel_domestic_calculate(params = {})
68
+ AusPostAPI::PAC::PostageParcelDomesticCalculate.new(params, @config).execute
69
+ end
70
+
71
+ def postage_parcel_international_calculate(params = {})
72
+ AusPostAPI::PAC::PostageParcelInternationalCalculate.new(params, @config).execute
73
+ end
74
+
75
+ def postage_letter_domestic_calculate(params = {})
76
+ AusPostAPI::PAC::PostageLetterDomesticCalculate.new(params, @config).execute
77
+ end
78
+
79
+ def postage_letter_international_calculate(params = {})
80
+ AusPostAPI::PAC::PostageLetterInternationalCalculate.new(params, @config).execute
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,9 @@
1
+ module AusPostAPI
2
+ class PAC
3
+ class Country < AusPostAPI::PAC::Endpoint
4
+ def api_uri
5
+ "postage/country"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module AusPostAPI
2
+ class PAC
3
+ class DomesticLetterSize < AusPostAPI::PAC::Endpoint
4
+ def api_uri
5
+ "postage/letter/domestic/size"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module AusPostAPI
2
+ class PAC
3
+ class DomesticLetterThickness < AusPostAPI::PAC::Endpoint
4
+ def api_uri
5
+ "postage/letter/domestic/thickness"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module AusPostAPI
2
+ class PAC
3
+ class DomesticLetterWeight < AusPostAPI::PAC::Endpoint
4
+ def api_uri
5
+ "postage/letter/domestic/weight"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module AusPostAPI
2
+ class PAC
3
+ class DomesticParcelSize < AusPostAPI::PAC::Endpoint
4
+ def api_uri
5
+ "postage/parcel/domestic/size"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module AusPostAPI
2
+ class PAC
3
+ class DomesticParcelType < AusPostAPI::PAC::Endpoint
4
+ def api_uri
5
+ "postage/parcel/domestic/type"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module AusPostAPI
2
+ class PAC
3
+ class DomesticParcelWeight < AusPostAPI::PAC::Endpoint
4
+ def api_uri
5
+ "postage/parcel/domestic/weight"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ module AusPostAPI
2
+ class PAC
3
+ class DomesticPostcodeSearch < AusPostAPI::PAC::Endpoint
4
+ required_attributes :q
5
+ optional_attributes :state, :excludepostboxflag
6
+
7
+ def api_uri
8
+ "postcode/search"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,76 @@
1
+ module AusPostAPI
2
+ class PAC
3
+ # Abstract class that defines a PAC::Endpoint. It implements the `uri` &
4
+ # `headers` methods and expects the `api_uri` method to be implemented.
5
+ class Endpoint < AusPostAPI::Endpoint
6
+ LIVE_URI = 'https://auspost.com.au/api'
7
+ TEST_URI = 'https://test.npe.auspost.com.au'
8
+ TEST_KEY = '28744ed5982391881611cca6cf5c240'
9
+ FORMATS = ['json', 'xml']
10
+
11
+ def uri
12
+ "#{base_uri}/#{api_uri}.#{format}?#{params}"
13
+ end
14
+
15
+ def headers
16
+ { "AUTH-KEY" => @config[:TEST] ? TEST_KEY : auth_key }
17
+ end
18
+
19
+ private
20
+
21
+ def api_uri
22
+ raise ImplementationError.new('api_uri')
23
+ end
24
+
25
+ def base_uri
26
+ @config[:TEST] ? TEST_URI : LIVE_URI
27
+ end
28
+
29
+ def format
30
+ if @config[:FORMAT].nil?
31
+ raise NoFormatProvidedError
32
+ else
33
+ if !FORMATS.include?(@config[:FORMAT])
34
+ raise InvalidFormatError
35
+ else
36
+ @config[:FORMAT]
37
+ end
38
+ end
39
+ end
40
+
41
+ def params
42
+ [].tap { |result|
43
+ @attributes.keys.each { |a| result << "#{a}=#{self.send(a)}" }
44
+ }.join('&')
45
+ end
46
+
47
+ def auth_key
48
+ if @config[:PAC_AUTH_KEY].nil? && !@config[:TEST]
49
+ raise NoAuthKeyProvidedError
50
+ end
51
+
52
+ @config[:PAC_AUTH_KEY]
53
+ end
54
+
55
+ class NoAuthKeyProvidedError < StandardError
56
+ def initialize
57
+ super("The called endpoint requires the PAC_AUTH_KEY to be set")
58
+ end
59
+ end
60
+
61
+ class InvalidFormatError < StandardError
62
+ def initialize
63
+ super("Accepted formats are: #{AusPostAPI::PAC::Endpoint::FORMATS.join(', ')}")
64
+ end
65
+ end
66
+
67
+ class NoFormatProvidedError < InvalidFormatError; end
68
+
69
+ class ImplementationError < StandardError
70
+ def initialize(method)
71
+ super("No #{method} implemented")
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,9 @@
1
+ module AusPostAPI
2
+ class PAC
3
+ class InternationalLetterWeight < AusPostAPI::PAC::Endpoint
4
+ def api_uri
5
+ "postage/letter/international/weight"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module AusPostAPI
2
+ class PAC
3
+ class InternationalParcelWeight < AusPostAPI::PAC::Endpoint
4
+ def api_uri
5
+ "postage/parcel/international/weight"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ module AusPostAPI
2
+ class PAC
3
+ class PostageLetterDomesticCalculate < AusPostAPI::PAC::Endpoint
4
+ required_attributes :service_code, :weight
5
+ optional_attributes :option_code, :suboption_code, :extra_cover
6
+
7
+ def api_uri
8
+ "postage/letter/domestic/calculate"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module AusPostAPI
2
+ class PAC
3
+ class PostageLetterDomesticService < AusPostAPI::PAC::Endpoint
4
+ required_attributes :length, :width, :thickness, :weight
5
+
6
+ def api_uri
7
+ "postage/letter/domestic/service"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ module AusPostAPI
2
+ class PAC
3
+ class PostageLetterInternationalCalculate < AusPostAPI::PAC::Endpoint
4
+ required_attributes :country_code, :service_code
5
+ optional_attributes :weight, :option_code, :suboption_code, :extra_cover
6
+
7
+ def api_uri
8
+ "postage/letter/international/calculate"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module AusPostAPI
2
+ class PAC
3
+ class PostageLetterInternationalService < AusPostAPI::PAC::Endpoint
4
+ required_attributes :country_code, :weight
5
+
6
+ def api_uri
7
+ "postage/letter/international/service"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module AusPostAPI
2
+ class PAC
3
+ class PostageParcelDomesticCalculate < AusPostAPI::PAC::Endpoint
4
+ required_attributes :from_postcode, :to_postcode, :length, :width,
5
+ :height, :weight, :service_code
6
+ optional_attributes :option_code, :suboption_code, :extra_cover
7
+
8
+ def api_uri
9
+ "postage/parcel/domestic/calculate"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ module AusPostAPI
2
+ class PAC
3
+ class PostageParcelDomesticService < AusPostAPI::PAC::Endpoint
4
+ required_attributes :from_postcode, :to_postcode, :length, :width, :height,
5
+ :weight
6
+
7
+ def api_uri
8
+ "postage/parcel/domestic/service"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module AusPostAPI
2
+ class PAC
3
+ class PostageParcelInternationalCalculate < AusPostAPI::PAC::Endpoint
4
+ required_attributes :country_code, :weight, :service_code
5
+ optional_attributes :option_code, :suboption_code, :extra_cover
6
+
7
+ def api_uri
8
+ "postage/parcel/international/calculate"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module AusPostAPI
2
+ class PAC
3
+ class PostageParcelInternationalService < AusPostAPI::PAC::Endpoint
4
+ required_attributes :country_code, :weight
5
+
6
+ def api_uri
7
+ "postage/parcel/international/service"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,35 @@
1
+ require 'net/http'
2
+
3
+ # The UriHandler class is wrapper around a http technology. It takes a uri and
4
+ # a hash of header key values pairs and makes a http request.
5
+ module AusPostAPI
6
+ class UriHandler
7
+ def self.call(uri, headers={})
8
+ self.new(uri, headers).call
9
+ end
10
+
11
+ def initialize(uri, headers)
12
+ @headers = headers
13
+ @uri = URI.parse(uri)
14
+ end
15
+
16
+ def call
17
+ http.request(request)
18
+ end
19
+
20
+ private
21
+
22
+ def http
23
+ @http ||= Net::HTTP.new(@uri.host, @uri.port).tap do |h|
24
+ h.use_ssl = true
25
+ h.verify_mode = OpenSSL::SSL::VERIFY_PEER
26
+ end
27
+ end
28
+
29
+ def request
30
+ @request ||= Net::HTTP::Get.new(@uri).tap do |r|
31
+ @headers.each { |key, val| r.add_field(key, val) }
32
+ end
33
+ end
34
+ end
35
+ end