ytj_client 0.2.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: d8eae097f8325e466a4593e4960afff09fc8db03
4
+ data.tar.gz: af32f6468f790c419c4cc04816575198de4f77d1
5
+ SHA512:
6
+ metadata.gz: fe5c1dbcdb5dff384d998fb0d7454e7c6b9214d884746aeace174ce37dfd3e2f65533d8ac1acdfe11a1d89587baaee915e9a75764f247050d319f0256bef9b07
7
+ data.tar.gz: 0605aa009b316bd33ad408e727723caceced144b99fb689229508e026d22801b1741151b278fb109d90d14ac488cbf71021546891bea179aafcd94317de5241a
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.log
11
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ytj_client.gemspec
4
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,9 @@
1
+ # MIT License
2
+
3
+ Copyright (c) 2016 Janne Warén
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # ytj_client
2
+
3
+ A really small gem to fetch and parse data from the Finnish Patent and Registration Office's (PRH) YTJ-tiedot (business information system) API at http://avoindata.prh.fi/ytj.html
4
+
5
+ Makes the API call for you and parses the relevant (in my opinion) information to a convenient format (Ruby hash).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ytj_client'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle intall
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ytj_client
22
+
23
+ ## Usage
24
+
25
+ Currently only supports getting company information with a business_id:
26
+
27
+ ```ruby
28
+ require 'ytj_client'
29
+
30
+ YtjClient.fetch_company('2331972-7')
31
+ # => #Hash {
32
+ # :business_id => "2331972-7",
33
+ # :name => "Verso Food Oy",
34
+ # :registration_date => "2010-04-20",
35
+ # :company_form => "OY",
36
+ # :phones => {
37
+ # :mobile_phone => "+358400770697"
38
+ # },
39
+ # :website => "www.versofood.fi",
40
+ # :addresses => {
41
+ # :visiting_address => "Loisteputki 4, 00750, HELSINKI",
42
+ # :postal_address => "Loisteputki 4, 00750, HELSINKI"
43
+ # }
44
+ # }
45
+ ```
46
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ytj_client"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.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,3 @@
1
+ module YtjClient
2
+ VERSION = "0.2.0"
3
+ end
data/lib/ytj_client.rb ADDED
@@ -0,0 +1,85 @@
1
+ require "ytj_client/version"
2
+ require 'logger'
3
+
4
+ module YtjClient
5
+
6
+ YTJ_API_URL = 'http://avoindata.prh.fi:80/bis/v1/'.freeze
7
+
8
+ class << self
9
+
10
+ # Public method to be called
11
+ # for now returns a set of data that I think is generally useful
12
+ def fetch_company(business_id)
13
+ ytj_data = api_call(business_id)
14
+ {
15
+ business_id: ytj_data["businessId"],
16
+ name: ytj_data["name"],
17
+ registration_date: ytj_data["registrationDate"],
18
+ company_form: ytj_data["companyForm"],
19
+ phones: parse_phones(ytj_data),
20
+ website: parse_website(ytj_data),
21
+ addresses: parse_addresses(ytj_data)
22
+ }
23
+ rescue RestClient::NotFound
24
+ logger.warn "Company #{business_id} not found"
25
+ nil
26
+ rescue
27
+ logger.error "Error fetching data from YTJ: #{$!.message} - #{$!.backtrace}"
28
+ end
29
+
30
+ private
31
+
32
+ # Returns a hash with all company data from YTJ
33
+ def api_call(business_id)
34
+ url = "#{YTJ_API_URL}#{business_id}"
35
+ response = RestClient.get url
36
+ JSON.parse(response.body)["results"][0]
37
+ end
38
+
39
+ def parse_phones(ytj_data)
40
+ phones = {}
41
+ mobile_phone = ytj_data["contactDetails"].select { |contact| contact["type"] =="Mobile phone" && contact["endDate"] == nil }.first
42
+ phone = ytj_data["contactDetails"].select { |contact| contact["type"] =="Telephone" && contact["endDate"] == nil }.first
43
+ phones[:phone] = phone["value"].delete(' ') if phone
44
+ phones[:mobile_phone] = mobile_phone["value"].delete(' ') if mobile_phone
45
+ return phones
46
+ end
47
+
48
+ def parse_website(ytj_data)
49
+ website = ytj_data["contactDetails"].select { |contact| contact["type"] =="Website address" && contact["endDate"] == nil }.first
50
+ website["value"] if website
51
+ end
52
+
53
+ def parse_addresses(ytj_data)
54
+ addresses = {}
55
+ visiting_address =ytj_data["addresses"].select { |address| address["type"] == 1 && address["endDate"] == nil }.first
56
+ postal_address = ytj_data["addresses"].select { |address| address["type"] == 2 && address["endDate"] == nil }.first
57
+ addresses[:visiting_address] = parse_address(visiting_address)
58
+ addresses[:postal_address] = parse_address(postal_address)
59
+ return addresses
60
+ end
61
+
62
+ def parse_address(address)
63
+ result = []
64
+ result << address['careOf']
65
+ result << address['street']
66
+ result << address['postCode']
67
+ result << address['city']
68
+ result << address['country']
69
+ result.compact!
70
+ result.join(', ')
71
+ end
72
+
73
+ def parse_business_lines(ytj_data)
74
+ business_lines = []
75
+ ytj_data["businessLines"].select{|line| line["language"] == "FI"}.each do |business_line|
76
+ business_lines << business_line["name"]
77
+ end
78
+ business_lines
79
+ end
80
+
81
+ def logger
82
+ @logger ||= Logger.new('log/ytj_client.log')
83
+ end
84
+ end
85
+ end
data/log/.keep ADDED
File without changes
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ytj_client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ytj_client"
8
+ spec.version = YtjClient::VERSION
9
+ spec.authors = ["Janne Warén"]
10
+ spec.email = ["janne.waren@iki.fi"]
11
+ spec.licenses = ['MIT']
12
+
13
+ spec.summary = 'Client for communicating with Finnish Patent and Registration Office (PRH) YTJ-tiedot API'
14
+ spec.description = 'Fetches data with business_id from the YTJ API at http://avoindata.prh.fi/ytj.html'
15
+ spec.homepage = 'https://github.com/jannewaren/ytj_client'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency 'vcr', '~> 3.0', '>= 3.0.3'
28
+ spec.add_development_dependency 'webmock', '~> 2.3', '>= 2.3.1'
29
+ spec.add_development_dependency "pry", "~> 0.10.4"
30
+ spec.add_development_dependency 'awesome_print', '~> 1.7', '>= 1.7.0'
31
+
32
+ spec.add_runtime_dependency 'rest-client', '~> 2.0', '>= 2.0.0'
33
+ end
metadata ADDED
@@ -0,0 +1,194 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ytj_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Janne Warén
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: vcr
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 3.0.3
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '3.0'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 3.0.3
75
+ - !ruby/object:Gem::Dependency
76
+ name: webmock
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '2.3'
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 2.3.1
85
+ type: :development
86
+ prerelease: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '2.3'
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 2.3.1
95
+ - !ruby/object:Gem::Dependency
96
+ name: pry
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: 0.10.4
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: 0.10.4
109
+ - !ruby/object:Gem::Dependency
110
+ name: awesome_print
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: '1.7'
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: 1.7.0
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: '1.7'
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: 1.7.0
129
+ - !ruby/object:Gem::Dependency
130
+ name: rest-client
131
+ requirement: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - "~>"
134
+ - !ruby/object:Gem::Version
135
+ version: '2.0'
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: 2.0.0
139
+ type: :runtime
140
+ prerelease: false
141
+ version_requirements: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '2.0'
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: 2.0.0
149
+ description: Fetches data with business_id from the YTJ API at http://avoindata.prh.fi/ytj.html
150
+ email:
151
+ - janne.waren@iki.fi
152
+ executables: []
153
+ extensions: []
154
+ extra_rdoc_files: []
155
+ files:
156
+ - ".gitignore"
157
+ - ".rspec"
158
+ - ".travis.yml"
159
+ - Gemfile
160
+ - LICENSE.md
161
+ - README.md
162
+ - Rakefile
163
+ - bin/console
164
+ - bin/setup
165
+ - lib/ytj_client.rb
166
+ - lib/ytj_client/version.rb
167
+ - log/.keep
168
+ - ytj_client.gemspec
169
+ homepage: https://github.com/jannewaren/ytj_client
170
+ licenses:
171
+ - MIT
172
+ metadata: {}
173
+ post_install_message:
174
+ rdoc_options: []
175
+ require_paths:
176
+ - lib
177
+ required_ruby_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ required_rubygems_version: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ requirements: []
188
+ rubyforge_project:
189
+ rubygems_version: 2.5.1
190
+ signing_key:
191
+ specification_version: 4
192
+ summary: Client for communicating with Finnish Patent and Registration Office (PRH)
193
+ YTJ-tiedot API
194
+ test_files: []