nzbn-ruby 0.1.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 (43) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +26 -0
  3. data/Gemfile +5 -0
  4. data/LICENSE +21 -0
  5. data/README.md +239 -0
  6. data/Rakefile +8 -0
  7. data/lib/nzbn/api/addresses.rb +61 -0
  8. data/lib/nzbn/api/company_details.rb +32 -0
  9. data/lib/nzbn/api/email_addresses.rb +35 -0
  10. data/lib/nzbn/api/entities.rb +120 -0
  11. data/lib/nzbn/api/history.rb +81 -0
  12. data/lib/nzbn/api/industry_classifications.rb +35 -0
  13. data/lib/nzbn/api/organisation_parts.rb +92 -0
  14. data/lib/nzbn/api/phone_numbers.rb +35 -0
  15. data/lib/nzbn/api/privacy_settings.rb +33 -0
  16. data/lib/nzbn/api/roles.rb +53 -0
  17. data/lib/nzbn/api/trading_names.rb +35 -0
  18. data/lib/nzbn/api/watchlists.rb +120 -0
  19. data/lib/nzbn/api/websites.rb +35 -0
  20. data/lib/nzbn/client.rb +180 -0
  21. data/lib/nzbn/configuration.rb +28 -0
  22. data/lib/nzbn/error.rb +62 -0
  23. data/lib/nzbn/models/address.rb +12 -0
  24. data/lib/nzbn/models/base.rb +59 -0
  25. data/lib/nzbn/models/company.rb +20 -0
  26. data/lib/nzbn/models/email_address.rb +11 -0
  27. data/lib/nzbn/models/entity.rb +14 -0
  28. data/lib/nzbn/models/error_response.rb +19 -0
  29. data/lib/nzbn/models/full_entity.rb +50 -0
  30. data/lib/nzbn/models/industry_classification.rb +10 -0
  31. data/lib/nzbn/models/organisation_part.rb +14 -0
  32. data/lib/nzbn/models/phone_number.rb +16 -0
  33. data/lib/nzbn/models/privacy_settings.rb +26 -0
  34. data/lib/nzbn/models/role.rb +27 -0
  35. data/lib/nzbn/models/search_entity.rb +32 -0
  36. data/lib/nzbn/models/search_response.rb +50 -0
  37. data/lib/nzbn/models/trading_name.rb +10 -0
  38. data/lib/nzbn/models/watchlist.rb +14 -0
  39. data/lib/nzbn/models/website.rb +10 -0
  40. data/lib/nzbn/version.rb +5 -0
  41. data/lib/nzbn.rb +67 -0
  42. data/nzbn-ruby.gemspec +35 -0
  43. metadata +175 -0
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nzbn
4
+ module Models
5
+ # Base model class with common functionality
6
+ class Base
7
+ def initialize(attributes = {})
8
+ attributes.each do |key, value|
9
+ setter = "#{underscore(key.to_s)}="
10
+ send(setter, value) if respond_to?(setter)
11
+ end
12
+ end
13
+
14
+ def to_h
15
+ instance_variables.each_with_object({}) do |var, hash|
16
+ key = var.to_s.delete('@')
17
+ value = instance_variable_get(var)
18
+ hash[key] = serialize_value(value)
19
+ end
20
+ end
21
+
22
+ def to_json(*args)
23
+ to_h.to_json(*args)
24
+ end
25
+
26
+ private
27
+
28
+ def underscore(str)
29
+ str.gsub(/::/, '/')
30
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
31
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
32
+ .tr('-', '_')
33
+ .downcase
34
+ end
35
+
36
+ def camelize(str)
37
+ str.split('_').map.with_index { |word, i| i.zero? ? word : word.capitalize }.join
38
+ end
39
+
40
+ def serialize_value(value)
41
+ case value
42
+ when Base
43
+ value.to_h
44
+ when Array
45
+ value.map { |v| serialize_value(v) }
46
+ else
47
+ value
48
+ end
49
+ end
50
+
51
+ def build_model(klass, data)
52
+ return nil if data.nil?
53
+ return data.map { |item| build_model(klass, item) } if data.is_a?(Array)
54
+
55
+ klass.new(data)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nzbn
4
+ module Models
5
+ # Company details model
6
+ class Company < Base
7
+ attr_accessor :annual_return_filing_month, :financial_report_filing_month,
8
+ :nzsx_code, :annual_return_last_filed, :has_constitution_filed,
9
+ :country_of_origin, :overseas_company, :extensive_shareholding,
10
+ :stock_exchange_listed, :shareholding, :ultimate_holding_company,
11
+ :australian_company_number, :insolvencies, :removal_commenced
12
+ end
13
+
14
+ # Non-company entity details (limited partnerships, charities, etc.)
15
+ class NonCompany < Base
16
+ attr_accessor :annual_return_filing_month, :country_of_origin,
17
+ :registered_union_status, :charities_number, :balance_date
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nzbn
4
+ module Models
5
+ # Email address model
6
+ class EmailAddress < Base
7
+ attr_accessor :unique_identifier, :email_address, :email_purpose,
8
+ :email_purpose_description, :start_date
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nzbn
4
+ module Models
5
+ # Entity model representing a basic NZBN entity
6
+ class Entity < Base
7
+ attr_accessor :entity_status_code, :entity_name, :nzbn, :entity_type_code,
8
+ :entity_type_description, :entity_status_description,
9
+ :registration_date, :source_register, :source_register_unique_identifier,
10
+ :last_updated_date, :australian_business_number,
11
+ :hibernation_status_code, :hibernation_status_description
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nzbn
4
+ module Models
5
+ # Error response from API
6
+ class ErrorResponse < Base
7
+ attr_accessor :status, :error_description, :error_code, :list
8
+
9
+ def errors
10
+ list || []
11
+ end
12
+ end
13
+
14
+ # Individual error field
15
+ class ErrorField < Base
16
+ attr_accessor :field, :message
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nzbn
4
+ module Models
5
+ # Full entity model with all details
6
+ class FullEntity < Base
7
+ attr_accessor :entity_status_code, :entity_name, :nzbn, :entity_type_code,
8
+ :entity_type_description, :entity_status_description,
9
+ :registration_date, :source_register, :source_register_unique_identifier,
10
+ :last_updated_date, :australian_business_number,
11
+ :hibernation_status_code, :hibernation_status_description,
12
+ :email_addresses, :addresses, :industry_classifications,
13
+ :phone_numbers, :websites, :trading_names, :privacy_settings,
14
+ :company_details, :non_company_details, :roles,
15
+ :trading_areas, :payment_bank_accounts, :gst_numbers,
16
+ :supporting_information, :business_ethnicity_identifiers
17
+
18
+ def initialize(attributes = {})
19
+ super
20
+ parse_nested_objects(attributes)
21
+ end
22
+
23
+ private
24
+
25
+ def parse_nested_objects(attributes)
26
+ @addresses = build_addresses(attributes['addresses'])
27
+ @email_addresses = build_model_array(EmailAddress, attributes['emailAddresses'])
28
+ @phone_numbers = build_model_array(PhoneNumber, attributes['phoneNumbers'])
29
+ @websites = build_model_array(Website, attributes['websites'])
30
+ @trading_names = build_model_array(TradingName, attributes['tradingNames'])
31
+ @industry_classifications = build_model_array(IndustryClassification, attributes['industryClassifications'])
32
+ @roles = build_model_array(Role, attributes['roles'])
33
+ @privacy_settings = PrivacySettings.new(attributes['privacySettings']) if attributes['privacySettings']
34
+ @company_details = Company.new(attributes['company-details']) if attributes['company-details']
35
+ end
36
+
37
+ def build_addresses(data)
38
+ return [] unless data.is_a?(Hash) && data['addressList']
39
+
40
+ data['addressList'].map { |addr| Address.new(addr) }
41
+ end
42
+
43
+ def build_model_array(klass, data)
44
+ return [] unless data.is_a?(Array)
45
+
46
+ data.map { |item| klass.new(item) }
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nzbn
4
+ module Models
5
+ # Industry classification (ANZSIC) model
6
+ class IndustryClassification < Base
7
+ attr_accessor :unique_identifier, :classification_code, :classification_description
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nzbn
4
+ module Models
5
+ # Organisation part (OPN/GLN) model
6
+ class OrganisationPart < Base
7
+ attr_accessor :opn, :parent_nzbn, :parent_nzbn_name, :parent_gln, :name,
8
+ :purposes, :function, :organisation_part_status,
9
+ :addresses, :phone_numbers, :email_addresses,
10
+ :gst_number, :payment_bank_account_number, :custom_data,
11
+ :privacy, :nzbn_list, :start_date, :status_date
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nzbn
4
+ module Models
5
+ # Phone number model
6
+ class PhoneNumber < Base
7
+ attr_accessor :unique_identifier, :phone_purpose, :phone_purpose_description,
8
+ :phone_country_code, :phone_area_code, :phone_number, :start_date
9
+
10
+ def formatted_number
11
+ parts = [phone_country_code, phone_area_code, phone_number].compact.reject(&:empty?)
12
+ parts.join(' ')
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nzbn
4
+ module Models
5
+ # Privacy settings model
6
+ class PrivacySettings < Base
7
+ attr_accessor :nzbn_private_information, :entity_type_private_information,
8
+ :start_date_private_information, :status_private_information,
9
+ :location_id_private_information, :name_private_information,
10
+ :trading_name_private_information, :web_site_private_information,
11
+ :business_classification_private_information, :gst_status_private_information,
12
+ :phone_private_information, :email_private_information,
13
+ :registered_address_private_information, :postal_address_private_information,
14
+ :principal_address_private_information, :other_address_private_information,
15
+ :partners_private_information, :trustees_private_information,
16
+ :public_suggest_changes_yn, :gst_effective_date_private_information,
17
+ :physical_address_private_information, :australian_business_number_private_information,
18
+ :australian_company_number_private_information, :australian_service_address_private_information,
19
+ :abn_private_information, :gst_number_private_information,
20
+ :payment_bank_account_numbers_private_information,
21
+ :service_address_private_information, :invoice_address_private_information,
22
+ :delivery_address_private_information, :office_address_private_information,
23
+ :business_ethnicity_private_information
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nzbn
4
+ module Models
5
+ # Role model (director, partner, trustee, etc.)
6
+ class Role < Base
7
+ attr_accessor :unique_identifier, :role_type, :role_status,
8
+ :start_date, :end_date, :asic_directorship_yn,
9
+ :asic_name, :acn, :role_entity, :role_person,
10
+ :role_address, :role_asic_address
11
+
12
+ def initialize(attributes = {})
13
+ super
14
+ @role_person = RolePerson.new(attributes['rolePerson']) if attributes['rolePerson']
15
+ end
16
+ end
17
+
18
+ # Person details within a role
19
+ class RolePerson < Base
20
+ attr_accessor :title, :first_name, :middle_names, :last_name
21
+
22
+ def full_name
23
+ [first_name, middle_names, last_name].compact.reject(&:empty?).join(' ')
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nzbn
4
+ module Models
5
+ # Search entity result from entity search
6
+ class SearchEntity < Base
7
+ attr_accessor :entity_status_code, :entity_name, :nzbn, :entity_type_code,
8
+ :entity_type_description, :entity_status_description,
9
+ :registration_date, :source_register_unique_id,
10
+ :trading_names, :classifications, :previous_entity_names, :links
11
+
12
+ def initialize(attributes = {})
13
+ super
14
+ parse_nested_objects(attributes)
15
+ end
16
+
17
+ private
18
+
19
+ def parse_nested_objects(attributes)
20
+ @trading_names = build_model_array(TradingName, attributes['tradingNames'])
21
+ @classifications = build_model_array(IndustryClassification, attributes['classifications'])
22
+ @previous_entity_names = attributes['previousEntityNames'] || []
23
+ end
24
+
25
+ def build_model_array(klass, data)
26
+ return [] unless data.is_a?(Array)
27
+
28
+ data.map { |item| klass.new(item) }
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nzbn
4
+ module Models
5
+ # Paginated search response
6
+ class SearchResponse < Base
7
+ attr_accessor :page_size, :page, :total_items, :sort_by, :sort_order, :items, :links
8
+
9
+ def initialize(attributes = {}, item_class: nil)
10
+ super(attributes)
11
+ @items = parse_items(attributes['items'], item_class) if item_class
12
+ end
13
+
14
+ def each(&block)
15
+ items.each(&block)
16
+ end
17
+
18
+ def map(&block)
19
+ items.map(&block)
20
+ end
21
+
22
+ def first
23
+ items.first
24
+ end
25
+
26
+ def last
27
+ items.last
28
+ end
29
+
30
+ def empty?
31
+ items.nil? || items.empty?
32
+ end
33
+
34
+ def count
35
+ items&.length || 0
36
+ end
37
+
38
+ alias size count
39
+ alias length count
40
+
41
+ private
42
+
43
+ def parse_items(data, klass)
44
+ return [] unless data.is_a?(Array)
45
+
46
+ data.map { |item| klass.new(item) }
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nzbn
4
+ module Models
5
+ # Trading name model
6
+ class TradingName < Base
7
+ attr_accessor :unique_identifier, :name, :start_date, :end_date
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nzbn
4
+ module Models
5
+ # Watchlist model
6
+ class Watchlist < Base
7
+ attr_accessor :watchlist_id, :name, :user_id, :from_date, :to_date,
8
+ :organisation_id, :channel, :frequency, :wildcard,
9
+ :change_event_types, :push_endpoint, :authorization_token,
10
+ :admin_email_address, :watchlist_email_addresses,
11
+ :last_successful_change_notice_id, :suspended, :links, :nzbns
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nzbn
4
+ module Models
5
+ # Website model
6
+ class Website < Base
7
+ attr_accessor :unique_identifier, :url, :website_purpose, :start_date
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nzbn
4
+ VERSION = '0.1.0'
5
+ end
data/lib/nzbn.rb ADDED
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'nzbn/version'
4
+ require_relative 'nzbn/configuration'
5
+ require_relative 'nzbn/error'
6
+ require_relative 'nzbn/client'
7
+
8
+ # Models
9
+ require_relative 'nzbn/models/base'
10
+ require_relative 'nzbn/models/entity'
11
+ require_relative 'nzbn/models/full_entity'
12
+ require_relative 'nzbn/models/search_entity'
13
+ require_relative 'nzbn/models/address'
14
+ require_relative 'nzbn/models/role'
15
+ require_relative 'nzbn/models/trading_name'
16
+ require_relative 'nzbn/models/phone_number'
17
+ require_relative 'nzbn/models/email_address'
18
+ require_relative 'nzbn/models/website'
19
+ require_relative 'nzbn/models/industry_classification'
20
+ require_relative 'nzbn/models/privacy_settings'
21
+ require_relative 'nzbn/models/company'
22
+ require_relative 'nzbn/models/watchlist'
23
+ require_relative 'nzbn/models/organisation_part'
24
+ require_relative 'nzbn/models/search_response'
25
+ require_relative 'nzbn/models/error_response'
26
+
27
+ # API Resources
28
+ require_relative 'nzbn/api/entities'
29
+ require_relative 'nzbn/api/addresses'
30
+ require_relative 'nzbn/api/roles'
31
+ require_relative 'nzbn/api/trading_names'
32
+ require_relative 'nzbn/api/phone_numbers'
33
+ require_relative 'nzbn/api/email_addresses'
34
+ require_relative 'nzbn/api/websites'
35
+ require_relative 'nzbn/api/industry_classifications'
36
+ require_relative 'nzbn/api/privacy_settings'
37
+ require_relative 'nzbn/api/company_details'
38
+ require_relative 'nzbn/api/watchlists'
39
+ require_relative 'nzbn/api/organisation_parts'
40
+ require_relative 'nzbn/api/history'
41
+
42
+ # NZBN Ruby Client - New Zealand Business Number API
43
+ #
44
+ # @example Basic usage
45
+ # Nzbn.configure do |config|
46
+ # config.api_key = 'your-api-key'
47
+ # end
48
+ #
49
+ # client = Nzbn::Client.new
50
+ # entities = client.entities.search(search_term: 'Company Name')
51
+ #
52
+ module Nzbn
53
+ DEFAULT_BASE_URL = 'https://api.business.govt.nz/gateway/nzbn/v5'
54
+
55
+ class << self
56
+ attr_accessor :configuration
57
+
58
+ def configure
59
+ self.configuration ||= Configuration.new
60
+ yield(configuration)
61
+ end
62
+
63
+ def reset_configuration!
64
+ self.configuration = Configuration.new
65
+ end
66
+ end
67
+ end
data/nzbn-ruby.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/nzbn/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'nzbn-ruby'
7
+ spec.version = Nzbn::VERSION
8
+ spec.authors = ['Nhan Nguyen']
9
+ spec.email = ['nnhansg@gmail.com']
10
+
11
+ spec.summary = 'Ruby client for the New Zealand Business Number (NZBN) API'
12
+ spec.description = 'A Ruby gem for interacting with the NZBN API v5. Search entities, manage watchlists, and access NZ business data.'
13
+ spec.homepage = 'https://github.com/nnhansg/nzbn-ruby'
14
+ spec.license = 'MIT'
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = spec.homepage
18
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/main/CHANGELOG.md"
19
+
20
+ spec.files = Dir.chdir(__dir__) do
21
+ `git ls-files -z`.split("\x0").reject do |f|
22
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
23
+ end
24
+ end
25
+ spec.files += Dir['lib/**/*.rb']
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_dependency 'faraday', '>= 1.0', '< 3.0'
29
+ spec.add_dependency 'faraday-multipart', '~> 1.0'
30
+
31
+ spec.add_development_dependency 'bundler', '~> 2.0'
32
+ spec.add_development_dependency 'rake', '~> 13.0'
33
+ spec.add_development_dependency 'rspec', '~> 3.0'
34
+ spec.add_development_dependency 'webmock', '~> 3.0'
35
+ end