select_pdf_api 0.0.1

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: 098930437dd6e75013830c8ceb62783b408309d6
4
+ data.tar.gz: 99170880bbe8759d518cebee3ded5e3dd8c80489
5
+ SHA512:
6
+ metadata.gz: 1184de9dde9c70d531d001abd0c922abe91af93bb92f578e90e8cf5d18c188d52aba7b4f038933056b4df76785f07c51bcf185f83fe32c8715880078d19b2be5
7
+ data.tar.gz: db5fdabb37f34e977ecfdacc071848f6691973c20381e718d9a30ed1aa90caaf4526d37dbdebe5f55b598279d528bad215f143697833185b49506238cc92ccf4
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in select-pdf-api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 arianamador
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # select_pdf_api
2
+
3
+ A wrapper for the [selectpdf.org](http://selectpdf.com/) public API.
4
+
5
+ The [selectpdf.org](http://selectpdf.com/) online API allows you to create PDFs from web pages and raw HTML code in your applications.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'select_pdf_api'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install select_pdf_api
19
+
20
+ You'll need to have a config folder and at least the default select-pdf-config.yml file. Inside your config folder you'll be able to have as many config.yml files as necessary.
21
+
22
+ mkdir config
23
+ touch select-pdf-config.yml
24
+
25
+ Inside your select-pdf-config.yml:
26
+ ``` ruby
27
+ key: 'service api key'
28
+ ```
29
+ See the [API Documentation](https://github.com/arian-amador/select_pdf_api/) for all the options.
30
+
31
+ ## Usage
32
+
33
+ ##### Default
34
+ ``` ruby
35
+ pdf_doc = SelectPdfApi.new({url: "http://www.google.com"}) # Load the default select-pdf-config.yml and setup the url to capture.
36
+ pdf_doc.download # Save result to the default ./document.pdf
37
+
38
+ ```
39
+ ##### Load a specific config per download
40
+ ``` ruby
41
+
42
+ sites = [
43
+ {url: "http://www.google.com", save_to: 'google_com.pdf'},
44
+ {url: "http://mail.yahoo.com", save_to: 'mail_yahoo.pdf',
45
+ config_file: 'password-protected-yahoo.yml'}]
46
+
47
+ sites.each do |options|
48
+ pdf = SelectPdfApi.new options
49
+ pdf.download
50
+ end
51
+ ```
52
+
53
+ ##### Changing config options
54
+ ``` ruby
55
+ pdf = SelectPdfApi.new # Loads default config file.
56
+ pdf.config.load_config('wide_margins.yml') # Load wide margin options.
57
+ pdf.download
58
+ ```
59
+
60
+ ## Documentation
61
+ * API - http://selectpdf.com/html-to-pdf-api/
62
+ * Rubydoc - http://www.rubydoc.info/github/arian-amador/select_pdf_api/master
63
+ * GitHub - https://github.com/arian-amador/select_pdf_api/
64
+
65
+ ## Contributing
66
+ 1. Fork it ( https://github.com/arian-amador/select_pdf_api/fork )
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = "test/test_select_pdf_api/test_*.rb"
6
+ end
@@ -0,0 +1,4 @@
1
+ class SelectPdfApi
2
+ class DownloadError < StandardError; end
3
+ class ConfigError < StandardError; end
4
+ end
@@ -0,0 +1,3 @@
1
+ class SelectPdfApi
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ class SelectPdfApi
2
+ class YamlFileConfig
3
+
4
+ def initialize(filename)
5
+ @data = {}
6
+ load_config filename
7
+ end
8
+
9
+ def load_config(filename)
10
+ config_file = File.join(File.expand_path(File.join('config')), filename)
11
+
12
+ raise SelectPdfApi::ConfigError, "Config file #{config_file} does not exist." unless
13
+ File.exist? config_file
14
+
15
+ @data = YAML::load_file(config_file)
16
+
17
+ raise SelectPdfApi::ConfigError, "Error loading values from #{config_file}" unless @data
18
+ end
19
+
20
+ def options
21
+ @data
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,52 @@
1
+ require 'yaml'
2
+ require 'httparty'
3
+
4
+ require 'select_pdf_api/yaml_file_config'
5
+ require 'select_pdf_api/exceptions'
6
+
7
+ class SelectPdfApi
8
+ attr_accessor :config, :save_to, :url
9
+
10
+ API_URL = 'http://selectpdf.com/api'
11
+
12
+ # TODO: Refactor this...
13
+ DEFAULT_OPTIONS = {
14
+ config_file: "select-pdf-config.yml",
15
+ save_to: "document.pdf"
16
+ }
17
+
18
+ def initialize(user_opts={})
19
+ opts = DEFAULT_OPTIONS.merge user_opts
20
+
21
+ @url = opts[:url]
22
+ @config = opts[:config] || SelectPdfApi::YamlFileConfig.new(opts[:config_file])
23
+ @save_to = opts[:save_to]
24
+ @success = false
25
+ end
26
+
27
+ def download
28
+ raise SelectPdfApi::DownloadError, "A URL must be specified." if @url.nil?
29
+
30
+ request = "#{API_URL}/?#{params}"
31
+ response = HTTParty.get request
32
+
33
+ if response.success?
34
+ File.open(@save_to, "wb") {|f| f.write response.parsed_response}
35
+ @success = true
36
+ else
37
+ raise SelectPdfApi::DownloadError, "There was an error with the following request #{request}"
38
+ end
39
+ end
40
+
41
+ def params
42
+ result = []
43
+ @config.options.sort.map { |name, value| result << "#{name}=#{value}" }
44
+ result << "url=#{@url}"
45
+ result.join('&')
46
+ end
47
+
48
+ def success?
49
+ @success
50
+ end
51
+
52
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require 'select_pdf_api/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "select_pdf_api"
7
+ spec.version = SelectPdfApi::VERSION
8
+ spec.authors = ["Arian Amador"]
9
+ spec.email = ["arian@arianamador.com"]
10
+ spec.summary = %q{Wrapper library for the Select PDF service.}
11
+ spec.description = %q{Wrapper library for the Select PDF service. SelectPdf offers a REST API that can be used to convert html to pdf in any language with our dedicated web service.}
12
+ spec.homepage = "https://www.github.com/arian-amador/select-pdf-api"
13
+ spec.licenses = ["MIT"]
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.post_install_message = "PDF all the web of things!"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+
25
+ spec.add_development_dependency "vcr", '~> 2.9', '>= 2.9.3'
26
+ spec.add_development_dependency "webmock", '~> 1.20', '>= 1.20.4'
27
+ spec.add_development_dependency 'minitest', '~> 5.5', '>= 5.5.1'
28
+
29
+ spec.add_dependency "httparty", "~> 0.13.3"
30
+ end
@@ -0,0 +1,9 @@
1
+ key: "valid-key-123-67ad"
2
+ page_size: "Letter"
3
+ page_orientation: "Landscape"
4
+ margin_right: "2pt"
5
+ margin_bottom: "2pt"
6
+ margin_left: "1.25pt"
7
+ margin_left: "1.25pt"
8
+ user_password: "user123"
9
+ owner_password: "owner567"
File without changes
@@ -0,0 +1 @@
1
+ key: 'random-valid-api-123abc-345dbc'
@@ -0,0 +1,13 @@
1
+ $:.unshift File.expand_path('../../lib', __FILE__)
2
+ $:.unshift File.expand_path('../../config', __FILE__)
3
+ require 'select_pdf_api'
4
+
5
+ require 'minitest/autorun'
6
+ require 'webmock/minitest'
7
+ require 'vcr'
8
+
9
+ VCR.configure do |c|
10
+ c.allow_http_connections_when_no_cassette = false
11
+ c.cassette_library_dir = "test/fixtures/vcr"
12
+ c.hook_into :webmock
13
+ end
@@ -0,0 +1,73 @@
1
+ require './test/minitest_helper'
2
+
3
+ # API test
4
+ describe SelectPdfApi do
5
+ let(:valid_url) {'http://www.google.com'}
6
+ let(:invalid_url) { SelectPdfApi.new({url: 'invalid_url'}) }
7
+
8
+ let(:default_config_api) { SelectPdfApi.new({url: valid_url})}
9
+ let(:minimum_config_api) { SelectPdfApi.new({url: valid_url,
10
+ config_file: '../test/fixtures/minimum-config.yml'})}
11
+ let(:maximum_config_api) { SelectPdfApi.new({url: valid_url,
12
+ config_file: '../test/fixtures/all-config.yml'})}
13
+
14
+ def test_class_exists
15
+ assert default_config_api
16
+ default_config_api.must_be_instance_of SelectPdfApi
17
+ end
18
+
19
+ def test_save_to_should_be_changable
20
+ original_filename = "/tmp/test_file_1.tmp"
21
+ modified_filename = "/tmp/test_file_9.tmp"
22
+
23
+ new_select = SelectPdfApi.new({url: valid_url, save_to: original_filename})
24
+ new_select.save_to.must_equal original_filename
25
+ new_select.save_to = modified_filename
26
+ new_select.save_to.must_equal modified_filename
27
+ end
28
+
29
+ def test_url_should_be_changable
30
+ modified_url = "http://mail.yahoo.com"
31
+
32
+ default_config_api.url.must_equal valid_url
33
+ default_config_api.url = modified_url
34
+ default_config_api.url.must_equal modified_url
35
+ end
36
+
37
+ describe "#params" do
38
+ def test_params_exists
39
+ default_config_api.must_respond_to 'params'
40
+ end
41
+
42
+ def test_it_should_build_params_for_all_options
43
+ maximum_config_api.params.must_equal "key=valid-key-123-67ad&margin_bottom=2pt&margin_left=1.25pt&margin_right=2pt&owner_password=owner567&page_orientation=Landscape&page_size=Letter&user_password=user123&url=http://www.google.com"
44
+ end
45
+
46
+ def test_it_should_build_params_for_minimum_options
47
+ minimum_config_api.params.must_equal "key=random-valid-api-123abc-345dbc&url=http://www.google.com"
48
+ end
49
+ end
50
+
51
+ describe "#download" do
52
+ def test_download_exists
53
+ default_config_api.must_respond_to 'download'
54
+ end
55
+
56
+ def test_it_fails_without_a_url
57
+ -> {SelectPdfApi.new.download}.must_raise SelectPdfApi::DownloadError
58
+ end
59
+
60
+ def test_it_fails_with_an_invalid_url
61
+ VCR.use_cassette('download_with_invalid_url', :record => :new_episodes) do
62
+ -> {invalid_url.download}.must_raise SelectPdfApi::DownloadError
63
+ end
64
+ end
65
+
66
+ def test_it_downloads_a_pdf
67
+ VCR.use_cassette('download', :record => :new_episodes) do
68
+ default_config_api.download
69
+ default_config_api.success?
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,39 @@
1
+ require './test/minitest_helper'
2
+
3
+ # Config test
4
+ describe SelectPdfApi::YamlFileConfig do
5
+ let(:fixtures) {"../test/fixtures"}
6
+
7
+ let(:blank_config) {SelectPdfApi::YamlFileConfig.new("#{fixtures}/blank-config.yml")}
8
+ let(:minimum_config) {SelectPdfApi::YamlFileConfig.new("#{fixtures}/minimum-config.yml")}
9
+ let(:maximum_config) {SelectPdfApi::YamlFileConfig.new("#{fixtures}/all-config.yml")}
10
+ let(:invalid_config) {SelectPdfApi::YamlFileConfig.new('invalid_config')}
11
+
12
+ def test_it_should_fail_with_an_invalid_config
13
+ -> {invalid_config}.must_raise SelectPdfApi::ConfigError
14
+ end
15
+
16
+ def test_it_should_fail_with_a_blank_config
17
+ -> {blank_config}.must_raise SelectPdfApi::ConfigError
18
+ end
19
+
20
+ def test_valid_options_for_minimium_config
21
+ result = {"key"=>"random-valid-api-123abc-345dbc"}
22
+ minimum_config.options.must_equal result
23
+ end
24
+
25
+ def test_valid_options_for_all_options_config
26
+ result = {"key"=>"valid-key-123-67ad", "page_size"=>"Letter", "page_orientation"=>"Landscape", "margin_right"=>"2pt", "margin_bottom"=>"2pt", "margin_left"=>"1.25pt", "user_password"=>"user123", "owner_password"=>"owner567"}
27
+ maximum_config.options.must_equal result
28
+ end
29
+
30
+ def test_it_loads_a_new_config
31
+ original = {"key"=>"random-valid-api-123abc-345dbc"}
32
+ modified = {"key"=>"valid-key-123-67ad", "page_size"=>"Letter", "page_orientation"=>"Landscape", "margin_right"=>"2pt", "margin_bottom"=>"2pt", "margin_left"=>"1.25pt", "user_password"=>"user123", "owner_password"=>"owner567"}
33
+
34
+ config = SelectPdfApi::YamlFileConfig.new("#{fixtures}/minimum-config.yml")
35
+ config.options.must_equal original
36
+ config.load_config("#{fixtures}/all-config.yml")
37
+ config.options.must_equal modified
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: select_pdf_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Arian Amador
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-15 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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: vcr
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.9'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 2.9.3
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '2.9'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.9.3
61
+ - !ruby/object:Gem::Dependency
62
+ name: webmock
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.20'
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 1.20.4
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '1.20'
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 1.20.4
81
+ - !ruby/object:Gem::Dependency
82
+ name: minitest
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '5.5'
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 5.5.1
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '5.5'
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: 5.5.1
101
+ - !ruby/object:Gem::Dependency
102
+ name: httparty
103
+ requirement: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - "~>"
106
+ - !ruby/object:Gem::Version
107
+ version: 0.13.3
108
+ type: :runtime
109
+ prerelease: false
110
+ version_requirements: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - "~>"
113
+ - !ruby/object:Gem::Version
114
+ version: 0.13.3
115
+ description: Wrapper library for the Select PDF service. SelectPdf offers a REST API
116
+ that can be used to convert html to pdf in any language with our dedicated web service.
117
+ email:
118
+ - arian@arianamador.com
119
+ executables: []
120
+ extensions: []
121
+ extra_rdoc_files: []
122
+ files:
123
+ - ".gitignore"
124
+ - Gemfile
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - lib/select_pdf_api.rb
129
+ - lib/select_pdf_api/exceptions.rb
130
+ - lib/select_pdf_api/version.rb
131
+ - lib/select_pdf_api/yaml_file_config.rb
132
+ - select_pdf_api.gemspec
133
+ - test/fixtures/all-config.yml
134
+ - test/fixtures/blank-config.yml
135
+ - test/fixtures/minimum-config.yml
136
+ - test/minitest_helper.rb
137
+ - test/test_select_pdf_api/test_api.rb
138
+ - test/test_select_pdf_api/test_yaml_file_config.rb
139
+ homepage: https://www.github.com/arian-amador/select-pdf-api
140
+ licenses:
141
+ - MIT
142
+ metadata: {}
143
+ post_install_message: PDF all the web of things!
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.4.1
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: Wrapper library for the Select PDF service.
163
+ test_files:
164
+ - test/fixtures/all-config.yml
165
+ - test/fixtures/blank-config.yml
166
+ - test/fixtures/minimum-config.yml
167
+ - test/minitest_helper.rb
168
+ - test/test_select_pdf_api/test_api.rb
169
+ - test/test_select_pdf_api/test_yaml_file_config.rb