html_from_pdf 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4fc86936ba0f26860a8a57506c955b8859f5f87ce1a38b22eff1ebc07ff5ad03
4
- data.tar.gz: b41423a9ede0a7b934d73c012f6461703dad62a5ec3f248e8a8716e279b04fde
3
+ metadata.gz: 05efcbae774d4b590ae4638028a3746414a8fa67036471eee913a1a346c4412d
4
+ data.tar.gz: e61a1af451bf0c72b6d86eca07c0ca10e3d7e7b39a6aa5feefd7ba6d38dcd910
5
5
  SHA512:
6
- metadata.gz: fab8a2e4e5bf204c0e47141cec78f8701a1155ec17d56564173297891c0b77c00fda6cd949e2dc1675bef39ffcb2f8e968d760034223aaa1d9a2893adf52ec54
7
- data.tar.gz: 20f7a839d126b16a675e5567a66c13362cf9c0c427d53cc14e5b84b4cba741033809aaa9a7d90082110827b1d1a1293a2b13e5503f9109ecfc8d922a06e2ba67
6
+ metadata.gz: 48ca0cba23e73b52a50f0fbf7637d14c7d78bd16b41def184e7845dbbdc9acc3daebfc288a4481433d42a8814e5c3eec69cf29219bbcaa715f0ed3ff95eb78cf
7
+ data.tar.gz: 3cb4b4159b5ac0091e4d70576eb59a369a930072fbc558aa8c8332953f1c741a0b3e762abed15f359798fda0f2077d3bff6644dfda6537c8502cd81a7e2ecfe4
data/README.md CHANGED
@@ -24,12 +24,17 @@ Or install it yourself as:
24
24
 
25
25
  ```ruby
26
26
  HtmlFromPdf.convert('sample.pdf', 'path/to/html.html', options: { zoom: 1.5 })
27
+ HtmlFromPdf.convert('https://url_path_for_pdf', options: { zoom: 1.5 })
27
28
  ```
28
29
  Path to html is optional
29
30
  ```ruby
30
31
  HtmlFromPdf.convert('sample.pdf', options: { zoom: 1.5 })
31
32
  ```
32
33
 
34
+ **HtmlFromPdf** has a two methods: **convert** and **convert!**.
35
+ * The first doesn't raise exceptions and returns *nil* if something went wrong
36
+ * The second raises exceptions if something went wrong
37
+
33
38
  #### Allowed options:
34
39
 
35
40
  ```ruby
@@ -96,7 +101,3 @@ Bug reports and pull requests are welcome on GitHub at [html_from_pdf](https://g
96
101
  ## License
97
102
 
98
103
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
99
-
100
- ## Code of Conduct
101
-
102
- Everyone interacting in the HtmlFromPdf project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/oleksiivykhor/html_from_pdf/blob/master/CODE_OF_CONDUCT.md).
@@ -1,14 +1,32 @@
1
1
  require 'html_from_pdf/version'
2
2
  require 'byebug'
3
3
  require 'yaml'
4
+ require 'open-uri'
5
+ require 'fileutils'
6
+ require 'html_from_pdf/helpers'
4
7
  require 'html_from_pdf/exceptions'
5
8
  require 'html_from_pdf/converter'
9
+ require 'html_from_pdf/downloader'
6
10
 
7
11
  module HtmlFromPdf
8
12
  def self.convert(pdf_path, html_path = nil, options: {})
9
- raise HtmlFromPdf::PdfNotFound unless File.exist? pdf_path
13
+ convert_pdf_to_html(pdf_path, html_path, options)
14
+ rescue
15
+ end
16
+
17
+ def self.convert!(pdf_path, html_path = nil, options: {})
18
+ convert_pdf_to_html(pdf_path, html_path, options)
19
+ end
20
+
21
+ class << self
22
+ private
23
+
24
+ def convert_pdf_to_html(pdf_path, html_path, options)
25
+ pdf_path = HtmlFromPdf::Downloader.download(pdf_path) || pdf_path
26
+ raise HtmlFromPdf::PdfNotFound unless File.exist? pdf_path
10
27
 
11
- converter = HtmlFromPdf::Converter.new(pdf_path, html_path, options)
12
- converter.convert
28
+ converter = HtmlFromPdf::Converter.new(pdf_path, html_path, options)
29
+ converter.convert
30
+ end
13
31
  end
14
32
  end
@@ -1,5 +1,7 @@
1
1
  module HtmlFromPdf
2
2
  class Converter
3
+ include HtmlFromPdf::Helpers
4
+
3
5
  def initialize(pdf_path, html_path, options = {})
4
6
  @pdf_path = pdf_path
5
7
  @html_path = html_path
@@ -28,7 +30,7 @@ module HtmlFromPdf
28
30
  when !@html_path && @options[:dest_dir]
29
31
  file_name = nil
30
32
  else
31
- @options[:dest_dir], file_name = File.split(default_html_path)
33
+ @options[:dest_dir], file_name = File.split(default_file_path)
32
34
  end
33
35
  file_name
34
36
  end
@@ -44,10 +46,5 @@ module HtmlFromPdf
44
46
 
45
47
  result
46
48
  end
47
-
48
- def default_html_path
49
- file_name = "#{Time.now.strftime('%m_%d_%Y__%H_%M_%S')}.html"
50
- File.join('/tmp/html_from_pdf', file_name)
51
- end
52
49
  end
53
50
  end
@@ -0,0 +1,31 @@
1
+ module HtmlFromPdf
2
+ class Downloader
3
+ extend HtmlFromPdf::Helpers
4
+
5
+ def self.download(url)
6
+ return unless valid_url? url
7
+
8
+ file_path = default_file_path(:pdf)
9
+ dir_path = File.split(file_path).first
10
+ create_dir dir_path
11
+ File.open(file_path, 'wb') do |file|
12
+ file.write open(url).read
13
+ end
14
+ file_path
15
+ end
16
+
17
+ class << self
18
+ private
19
+
20
+ def valid_url?(url)
21
+ regex = /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}
22
+ (([0-9]{1,5})?\/.*)?$/ix
23
+ return false unless url[regex]
24
+
25
+ open(url).status.include?('200')
26
+ rescue SocketError
27
+ false
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,12 @@
1
+ module HtmlFromPdf
2
+ module Helpers
3
+ def default_file_path(format = :html)
4
+ file_name = "#{Time.now.strftime('%m_%d_%Y__%H_%M_%S')}.#{format}"
5
+ File.join('/tmp/html_from_pdf', file_name)
6
+ end
7
+
8
+ def create_dir(dir_path)
9
+ FileUtils.mkdir_p dir_path
10
+ end
11
+ end
12
+ end
@@ -1,3 +1,3 @@
1
1
  module HtmlFromPdf
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html_from_pdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - o.vykhor
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-10-21 00:00:00.000000000 Z
11
+ date: 2018-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -62,7 +62,6 @@ files:
62
62
  - ".gitignore"
63
63
  - ".rspec"
64
64
  - ".travis.yml"
65
- - CODE_OF_CONDUCT.md
66
65
  - Gemfile
67
66
  - LICENSE.txt
68
67
  - README.md
@@ -72,7 +71,9 @@ files:
72
71
  - html_from_pdf.gemspec
73
72
  - lib/html_from_pdf.rb
74
73
  - lib/html_from_pdf/converter.rb
74
+ - lib/html_from_pdf/downloader.rb
75
75
  - lib/html_from_pdf/exceptions.rb
76
+ - lib/html_from_pdf/helpers.rb
76
77
  - lib/html_from_pdf/options.yml
77
78
  - lib/html_from_pdf/version.rb
78
79
  homepage: https://github.com/oleksiivykhor/html_from_pdf
@@ -1,74 +0,0 @@
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 o.vykhor@ukr.net. 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/