companies_house_hub 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -29,4 +29,12 @@ module CompaniesHouseHub
29
29
  def configure
30
30
  yield configuration
31
31
  end
32
+
33
+ def root
34
+ File.expand_path('../', __dir__)
35
+ end
36
+
37
+ def load_yml(name)
38
+ YAML.load_file(File.join(root, 'data', "#{name}.yml"))
39
+ end
32
40
  end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ module CompaniesHouseHub
6
+ class FilingHistoryFormatter
7
+ def initialize(filing_history)
8
+ @filing_history = filing_history
9
+ end
10
+
11
+ def formatted(format = 'pdf')
12
+ name = descriptions[@filing_history.description]
13
+ name = name.downcase
14
+
15
+ # If "made_up_date" exists then replace it with the filing date and don't include filing.date
16
+ # in the file name (made_up_date will be used).
17
+ if name.match?(/{.*}/)
18
+ name = replace_placeholders(name)
19
+ date = nil
20
+ end
21
+
22
+ cleanup(name)
23
+
24
+ [name, date].compact.join('-') << ".#{format}"
25
+ end
26
+
27
+ private
28
+
29
+ def descriptions
30
+ CompaniesHouseHub.load_yml('filing_history_descriptions')['description']
31
+ end
32
+
33
+ def cleanup(name)
34
+ name.tr!('*', '')
35
+ name.tr!("\s", '-')
36
+ end
37
+
38
+ # Replaces the placeholders in yaml descriptions with what we get from Companies House (and is
39
+ # stored in FilingHistory#description_values).
40
+ def replace_placeholders(text)
41
+ matches = text.scan(/\{([a-z_]+)\}/).flatten
42
+
43
+ return text unless matches.any?
44
+
45
+ replaced = text.dup
46
+
47
+ matches.each do |match|
48
+ value = @filing_history.description_values[match.to_sym]
49
+ replaced.sub!(/{#{match}}/, value)
50
+ end
51
+
52
+ replaced
53
+ end
54
+ end
55
+ end
@@ -1,12 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'companies_house_hub/filing_history_formatter'
4
+
3
5
  module CompaniesHouseHub
4
6
  class FilingHistory < BaseModel
5
7
  DOCUMENT_URL = 'https://beta.companieshouse.gov.uk'
6
8
  FIND_PATH = '/company/:company_number/filing-history'
7
9
  DEFAULT_PER_PAGE = 100
8
10
 
9
- attr_reader :description, :action_date, :date, :type, :barcode, :links
11
+ attr_reader :description, :action_date, :date, :type, :barcode, :links, :description_values
10
12
 
11
13
  def self.all(options = {})
12
14
  options[:items_per_page] ||= DEFAULT_PER_PAGE
@@ -25,6 +27,11 @@ module CompaniesHouseHub
25
27
  @type = json.dig(:type)
26
28
  @barcode = json.dig(:barcode)
27
29
  @links = json.dig(:links)
30
+ @description_values = json.dig(:description_values)
31
+ end
32
+
33
+ def formatted_name
34
+ FilingHistoryFormatter.new(self).formatted
28
35
  end
29
36
 
30
37
  def url(format = 'pdf')
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CompaniesHouseHub
4
- VERSION = '0.0.2'
4
+ VERSION = '0.0.3'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: companies_house_hub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ricardo Otero
@@ -123,9 +123,11 @@ files:
123
123
  - Rakefile
124
124
  - bin/console
125
125
  - companies_house_hub.gemspec
126
+ - data/filing_history_descriptions.yml
126
127
  - lib/companies_house_hub.rb
127
128
  - lib/companies_house_hub/base_model.rb
128
129
  - lib/companies_house_hub/configuration.rb
130
+ - lib/companies_house_hub/filing_history_formatter.rb
129
131
  - lib/companies_house_hub/models/address.rb
130
132
  - lib/companies_house_hub/models/company.rb
131
133
  - lib/companies_house_hub/models/filing_history.rb