brreg_grunndata 0.1.0

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
+ SHA1:
3
+ metadata.gz: 8c7872a9ecab4f94b66773b90b69ce5cd11e8a17
4
+ data.tar.gz: f9dbec572665afcd6cfde816734b58d611efefd9
5
+ SHA512:
6
+ metadata.gz: 1e029e32e64679064bc986579f7575487a49848af27702ac5890d3d9ad82cb052c79685b3d80069069921019b49397839f05c6b4f676d686bfb77b233857bea1
7
+ data.tar.gz: eab73cef78ce031e4abed34beb5143d3d6d887392e4cb1cc1127d29fb8d5e2dade4cd2ee64a6b523eae75d64c67f153fd98e42a4e48c31cb0f3409181c5121e2
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .envrc
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ Style/Documentation:
2
+ Enabled: false
3
+
4
+ Metrics/LineLength:
5
+ Max: 100
6
+
7
+ AllCops:
8
+ TargetRubyVersion: 2.3
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.13.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at thhermansen@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in brreg_grunndata.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Thorbjørn Hermansen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # BRREG / Enhetsregisteret Grunndata
2
+
3
+ Ruby wrapper for brreg / enhetsregisteret's [soap web service](https://www.brreg.no/produkter-og-tjenester/bestille/tilgang-til-enhetsregisteret-via-web-services/).
4
+
5
+ [![Build Status](https://snap-ci.com/gramo-org/brreg_grunndata/branch/master/build_image)](https://snap-ci.com/gramo-org/brreg_grunndata/branch/master)
6
+
7
+ # Usage
8
+
9
+ This lib exposes a couple of ways getting data from BRREG. You can either use
10
+ the `Client` directly which gives direct access to response, it's `#message` (data)
11
+ and `#header` (BRREG's status and sub statuses) or you can use the `Service`
12
+ which returns data put in predefined types.
13
+
14
+ ## How to use the client
15
+
16
+ ```ruby
17
+ # Configure and create a client
18
+ config = BrregGrunndata::Configuration.new userid: 'x', password: 'y'
19
+ client = BrregGrunndata::Client.new configuration: config
20
+
21
+ # Make a request and get a response
22
+ #
23
+ # Calling an operation may raise an error, for instance if you
24
+ # are no authenticated.
25
+ response = client.hent_basisdata_mini orgnr: '123456789'
26
+
27
+ # The response has two main methods
28
+ # header() returns brreg's "responseHeader"
29
+ header = response.header
30
+ header.main_status # An integer
31
+ header.sub_statuses # An array of hashes, contains code (int) and message.
32
+
33
+ # message() contains the message. It has different data
34
+ # corresponding to the operation you called.
35
+ #
36
+ # Calling message may raise an error, for instance if no
37
+ # message is found in the response. You should inspect the
38
+ # header before asking for a message.
39
+ message = response.message # Returns a hash
40
+ ```
41
+
42
+ ## How to use the service
43
+
44
+ ```ruby
45
+ # You need to have an initialized client, see "How to use the client" above.
46
+ service = BrregGrunndata::Service.new client: client
47
+
48
+ # Get data for a given org number. Will return either the organization or null
49
+ # If an error occurs an error will be raised
50
+ organization = service.hent_basisdata_mini orgnr: '123456789'
51
+
52
+ organization.orgnr # Not surprisingly the organization number
53
+ organization.name # The name of this organization
54
+ organization.business_address # An address object, responds to street etc.
55
+ organization.organizational_form.name # ENK, ASA, etc
56
+ # ..etc
57
+ ```
58
+
59
+ ### Merge data from different soap operations
60
+
61
+ ```ruby
62
+ # Some data are fetched from other soap operations
63
+ organization = service.hent_basisdata_mini orgnr: '123456789'
64
+
65
+ organization.telephone_number # nil
66
+ organization.email # nil
67
+
68
+ merged_organization = organization.merge service.hent_kontaktdata orgnr: '123456789'
69
+ merged_organization.telephone_number # 77 66 55 44
70
+ merged_organization.email # 'email@example.com'
71
+ ```
72
+
73
+ ### Get data efficiently
74
+
75
+ Data for one organization is spread over multiple operations.
76
+ If you want you can use `#service.run_concurrently` to use threads
77
+ and execute different operations at the same time. Data returned from
78
+ each service will be `#merge`-ed together.
79
+
80
+ ```ruby
81
+ operations = [:hent_basisdata_mini, :hent_kontaktdata]
82
+ organization = service.run_concurrently operations, orgnr: '123456789'
83
+
84
+ organization.name # Filled from operation hent_basisdata_mini
85
+ organization.mobile_number # Filled from operation hent_kontaktdata
86
+ ```
87
+
88
+ # Web service documentation
89
+ For a better understanding of what to expect of `response.header`
90
+ and `response.message` please take a look at [this page from brreg](https://www.brreg.no/produkter-og-tjenester/bestille/tilgang-til-enhetsregisteret-via-web-services/teknisk-beskrivelse-web-services/grunndataws/).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'brreg_grunndata'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ require 'pry'
12
+
13
+ #
14
+ # Easy access to a brreg client from within the console
15
+ #
16
+ # If you have credentials set in ENV all you should need to do
17
+ # in the console is:
18
+ #
19
+ # client.hent_basisdata_mini orgnr: 'xyz' OR
20
+ # c.hent_basisdata_mini orgnr: 'xyz'
21
+ #
22
+ def client(debug: false, userid: nil, password: nil)
23
+ config = {
24
+ userid: userid || ENV['BRREG_USERNAME'],
25
+ password: password || ENV['BRREG_PASSWORD']
26
+ }
27
+
28
+ if debug
29
+ config[:logger] = Logger.new(STDOUT)
30
+ config[:log_level] = :debug
31
+ end
32
+
33
+ BrregGrunndata::Client.new configuration: BrregGrunndata::Configuration.new(config)
34
+ end
35
+ alias c client
36
+
37
+ def service(debug: false, userid: nil, password: nil)
38
+ BrregGrunndata::Service.new client: client(debug: debug, userid: userid, password: password)
39
+ end
40
+ alias s service
41
+
42
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path('../lib', __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require 'brreg_grunndata/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'brreg_grunndata'
10
+ spec.version = BrregGrunndata::VERSION
11
+ spec.authors = ['Thorbjørn Hermansen']
12
+ spec.email = ['thhermansen@gmail.com']
13
+
14
+ spec.summary = %(Access BRREG's grunndata)
15
+ spec.description = %(Access BRREG's grunndata)
16
+ spec.homepage = ''
17
+ spec.license = 'MIT'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_dependency 'savon', '~> 2.11'
28
+ spec.add_dependency 'dry-struct', '~> 0.1.1'
29
+
30
+ spec.add_development_dependency 'bundler', '~> 1.13'
31
+ spec.add_development_dependency 'rake', '~> 10.0'
32
+ spec.add_development_dependency 'rspec', '~> 3.0'
33
+ spec.add_development_dependency 'webmock', '~> 2.3.0'
34
+ spec.add_development_dependency 'pry'
35
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'brreg_grunndata/version'
4
+
5
+ require_relative 'brreg_grunndata/error'
6
+ require_relative 'brreg_grunndata/configuration'
7
+ require_relative 'brreg_grunndata/client'
8
+ require_relative 'brreg_grunndata/service'
9
+
10
+ module BrregGrunndata
11
+ end
@@ -0,0 +1,182 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'savon'
4
+
5
+ require_relative 'client/response'
6
+ require_relative 'client/response_validator'
7
+ require_relative 'client/sok_enhet_query_to_xml'
8
+
9
+ module BrregGrunndata
10
+ # Brreg client to access brreg's data
11
+ #
12
+ # This is a thin wrapper around BRREG's soap API using
13
+ # Savon to communicate.
14
+ #
15
+ # Compared to simply use savon directly use of this client offers:
16
+ # - Unwrap the return value (which is a XML string)
17
+ # - Handle common errors
18
+ # - Has a clear interface of the soap operations we support.
19
+ #
20
+ # @see Service
21
+ class Client
22
+ # Raised when we have a timeout error
23
+ class TimeoutError < Error; end
24
+
25
+ def initialize(configuration:, service: nil)
26
+ @configuration = configuration
27
+ @service = service || build_savon_service
28
+ end
29
+
30
+ # Get basic mini data of an organization
31
+ #
32
+ # Arguments
33
+ # orgnr - The orgnr you are searching for
34
+ #
35
+ # @return BrregGrunndata::Response
36
+ def hent_basisdata_mini(orgnr:)
37
+ call :hent_basisdata_mini, orgnr: orgnr
38
+ end
39
+
40
+ # Get contact information for an organization
41
+ #
42
+ # Arguments
43
+ # orgnr - The orgnr you are searching for
44
+ #
45
+ # @return BrregGrunndata::Response
46
+ def hent_kontaktdata(orgnr:)
47
+ call :hent_kontaktdata, orgnr: orgnr
48
+ end
49
+
50
+ # Search for an organization
51
+ #
52
+ # Makes it possible to find an organization from name
53
+ #
54
+ # NOTE
55
+ # ==================
56
+ # The response.message you get back seems to be a document of it's own,
57
+ # with header, some info about the search, and the hits.
58
+ #
59
+ # We may choose to do something with this message response in the
60
+ # future, or make this easier to use on the service level.
61
+ #
62
+ # Currently its structure is something like:
63
+ #
64
+ # <BrAixXmlResponse ResponseType="BrErfrTreffliste">
65
+ # <BrAixResponseHeader>
66
+ # <ReturStatus>0</ReturStatus>
67
+ # <TimeStamp>2016-12-04T20:03:09</TimeStamp>
68
+ # <ElapsedTime>168</ElapsedTime>
69
+ # <AntallTreff>61</AntallTreff>
70
+ # </BrAixResponseHeader>
71
+ # <Sokeverdier>
72
+ # <BrSokeStreng>STATOIL ASA</BrSokeStreng>
73
+ # <OrgForm>ALLE</OrgForm>
74
+ # <Fylke Fylkesnr="0">ALLE</Fylke>
75
+ # <Kommune Kommunenr="0">ALLE</Kommune>
76
+ # <Slettet>N</Slettet>
77
+ # <MedUnderenheter>true</MedUnderenheter>
78
+ # <MaxTreffReturneres>100</MaxTreffReturneres>
79
+ # <ReturnerIngenHvisMax>true</ReturnerIngenHvisMax>
80
+ # </Sokeverdier>
81
+ # <BrErfrTreffliste Antall="61">
82
+ # <BrErfrTrefflisteElement>
83
+ # <Orgnr>873152362</Orgnr>
84
+ # <OrgNavn>STATOIL ASA AVD KONTOR BERGEN</OrgNavn>
85
+ # <Sted>5254 SANDSLI</Sted>
86
+ # <Score>100</Score>
87
+ # <OrgForm>BEDR</OrgForm>
88
+ # </BrErfrTrefflisteElement>
89
+ #
90
+ # ...
91
+ #
92
+ #
93
+ # Arguments
94
+ # query - Your search string / query goes here
95
+ # first - How many do you want to get in return? (the limit)
96
+ # include_no_if_max - Do you want zero results if your search yields more
97
+ # results than the first X you asked for? I don't know
98
+ # why you would want that.
99
+ # with_subdivision - Do you want to include organization form BEDR og AAFY
100
+ # when you search?
101
+ # @return BrregGrunndata::Response
102
+ def sok_enhet(query:, first: 100, include_no_if_max: false, with_subdivision: true)
103
+ call :sok_enhet, search_request!: SokEnhetQueryToXml.new(
104
+ query,
105
+ first: first,
106
+ include_no_if_max: include_no_if_max,
107
+ with_subdivision: with_subdivision
108
+ ).cdata
109
+ end
110
+
111
+ # Gets extended information about an organization
112
+ #
113
+ # This returns information regarding organization is
114
+ # "Foretaksregisteret", "Merverdiavgiftsregisteret", or "konkursbehandling"
115
+ #
116
+ # Arguments
117
+ # orgnr - The orgnr you are searching for
118
+ #
119
+ # @return BrregGrunndata::Response
120
+ def hent_saerlige_opplysninger(orgnr:)
121
+ call :hent_saerlige_opplysninger, orgnr: orgnr
122
+ end
123
+
124
+ # Gets information from "Tilknyttet registeret"
125
+ #
126
+ # Contains information about "naeringskode", "sektorkode", "ansvarskapital"
127
+ # and number of employees
128
+ #
129
+ # Arguments
130
+ # orgnr - The orgnr you are searching for
131
+ #
132
+ # @return BrregGrunndata::Response
133
+ def hent_opplysninger_tilknyttet_register(orgnr:)
134
+ call :hent_oppl_tilkn_register, orgnr: orgnr
135
+ end
136
+
137
+ private
138
+
139
+ attr_reader :configuration, :service
140
+
141
+ # Calls the operation in soap service. Message will include credentials
142
+ #
143
+ # Arguments
144
+ # operation - The name of the operation, ex :hent_basisdata_mini
145
+ # message - The message to be sent to the operation.
146
+ # Credentials will be merged before message is sent.
147
+ def call(operation, message)
148
+ savon_response = service.call operation, message: message.merge(credentials)
149
+
150
+ ResponseValidator.new(
151
+ Response.new(savon_response)
152
+ ).raise_error_or_return_response!
153
+ rescue ::Net::ReadTimeout
154
+ raise TimeoutError
155
+ end
156
+
157
+ # rubocop:disable Metrics/MethodLength
158
+ def build_savon_service
159
+ options = {
160
+ open_timeout: configuration.open_timeout,
161
+ read_timeout: configuration.read_timeout,
162
+ wsdl: configuration.wsdl
163
+ }
164
+
165
+ if configuration.logger
166
+ options[:logger] = configuration.logger
167
+ options[:log_level] = configuration.log_level
168
+ options[:log] = true
169
+ end
170
+
171
+ Savon.client options
172
+ end
173
+ # rubocop:enable Metrics/MethodLength
174
+
175
+ def credentials
176
+ {
177
+ userid: configuration.userid,
178
+ password: configuration.password
179
+ }
180
+ end
181
+ end
182
+ end