paperplane-rb 0.1.6 → 0.1.8

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: 8650245780e13bcc390fcc98d5628c523077cf443a8776563bfa95c528429397
4
- data.tar.gz: 000b39399abe1e6909f46f28f23d99bdfca7bdc7f2722ac5750311a901c96212
3
+ metadata.gz: 7bac605d7028446810334ec5ce9f549ce2a40884aa766dc82d30384145b85bf8
4
+ data.tar.gz: 1f6055b1cf36e3562861598e7d839a8086ddd03a9cb537bc9954e98532320e7a
5
5
  SHA512:
6
- metadata.gz: a92b8cf658f491043a937359d0494e811219965fef14fcf9521d536f05e3b64f831b8ae5915f9a11611ff48a23f772c9696e7e3b8923914306a9a563739d12e5
7
- data.tar.gz: 9886f0011b89563d4b9ab7970e424006124cc033408f115b1ac1eaab822aeece9e682fc768a2070fbdc6a1b30f32e7a85967ad5644770cd2d512e10ecd7990e1
6
+ metadata.gz: e5880a18a09467a0a9cd48146b10977d842ceb0acd22deca2491dc9466d160d1126a7d140c7daa8d7b70620f1c6adf6da20f38318e8e718bb37934fd1e728f0e
7
+ data.tar.gz: ed739ace7d040964123f9898115fda612b4c780df711c8d21f396a5f050f4ede22ac2c0a7ee0edb83dd264763eec51c0105445efeafd40ad90e5853668a818d7
data/Gemfile CHANGED
@@ -8,5 +8,3 @@ gemspec
8
8
  gem "rake", "~> 13.0"
9
9
 
10
10
  gem "rspec", "~> 3.0"
11
-
12
- gem 'http'
data/Gemfile.lock CHANGED
@@ -1,14 +1,17 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- paperplane-rb (0.1.5)
4
+ paperplane-rb (0.1.8)
5
5
  http
6
+ webmock
6
7
 
7
8
  GEM
8
9
  remote: https://rubygems.org/
9
10
  specs:
10
11
  addressable (2.8.1)
11
12
  public_suffix (>= 2.0.2, < 6.0)
13
+ crack (0.4.5)
14
+ rexml
12
15
  diff-lcs (1.5.0)
13
16
  domain_name (0.5.20190701)
14
17
  unf (>= 0.0.5, < 1.0.0)
@@ -16,6 +19,7 @@ GEM
16
19
  ffi-compiler (1.0.1)
17
20
  ffi (>= 1.0.0)
18
21
  rake
22
+ hashdiff (1.0.1)
19
23
  http (5.1.1)
20
24
  addressable (~> 2.8)
21
25
  http-cookie (~> 1.0)
@@ -29,6 +33,7 @@ GEM
29
33
  rake (~> 13.0)
30
34
  public_suffix (5.0.1)
31
35
  rake (13.0.6)
36
+ rexml (3.2.5)
32
37
  rspec (3.12.0)
33
38
  rspec-core (~> 3.12.0)
34
39
  rspec-expectations (~> 3.12.0)
@@ -45,12 +50,16 @@ GEM
45
50
  unf (0.1.4)
46
51
  unf_ext
47
52
  unf_ext (0.0.8.2)
53
+ webmock (3.18.1)
54
+ addressable (>= 2.8.0)
55
+ crack (>= 0.3.2)
56
+ hashdiff (>= 0.4.0, < 2.0.0)
48
57
 
49
58
  PLATFORMS
50
59
  arm64-darwin-22
60
+ x86_64-linux
51
61
 
52
62
  DEPENDENCIES
53
- http
54
63
  paperplane-rb!
55
64
  rake (~> 13.0)
56
65
  rspec (~> 3.0)
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
- # Paperplane
1
+ # paperplane-rb
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/paperplane`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ A simple gem to interact with the Paperplane PDF API. It just has very basic features that I needed quickly. I'll continue to improve this as I need. Feel free to contribute if you'd like!
6
4
 
7
5
  ## Installation
8
6
 
@@ -10,20 +8,41 @@ Install the gem and add to the application's Gemfile by executing:
10
8
 
11
9
  $ bundle add paperplane
12
10
 
13
- If bundler is not being used to manage dependencies, install the gem by executing:
11
+ Or add it to your `Gemfile` manually:
14
12
 
15
- $ gem install paperplane
13
+ ```ruby
14
+ gem 'paperplane'
15
+ ```
16
16
 
17
17
  ## Usage
18
18
 
19
- TODO: Write usage instructions here
20
-
21
- ## Development
22
-
23
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
-
25
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
19
+ Initialize the library and set your API key(required) and logger(optional).
20
+ ```ruby
21
+ require 'paperplane'
22
+
23
+ Paperplane.configure do |config|
24
+ config.api_key = 'your_paperplane_api_key'
25
+ config.logger = Appsignal::Logger.new("paperplane")
26
+ end
27
+ ```
28
+
29
+ You can now call the different methods:
30
+ ```ruby
31
+ # Download PDF
32
+ url = 'https://en.wikipedia.org/wiki/Airplane'
33
+ output_file = 'example.pdf'
34
+ PaperplaneAPI.download_pdf(url, output_file)
35
+
36
+ # Create Job
37
+ response = PaperplaneAPI.create_job(url)
38
+ puts response
39
+ job_id = response['id']
40
+
41
+ # Show Job
42
+ job = PaperplaneAPI.show_job(job_id)
43
+ puts job
44
+ ```
26
45
 
27
46
  ## Contributing
28
47
 
29
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/paperplane.
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nolyoi/paperplane-rb.
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+ require 'logger'
3
+
4
+ class Config
5
+ attr_accessor :api_key, :logger
6
+
7
+ def initialize
8
+ @api_key = ''
9
+ @logger = Logger.new(STDOUT)
10
+ end
11
+ end
@@ -1,6 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Paperplane
4
- class Error < StandardError
4
+ class Error < StandardError
5
+ def initialize(message)
6
+ Paperplane.config.logger.error(message)
7
+ super(message)
8
+ end
5
9
  end
6
10
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Paperplane
4
- VERSION = "0.1.6"
4
+ VERSION = "0.1.8"
5
5
  end
data/lib/paperplane.rb CHANGED
@@ -1,67 +1,66 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'http'
4
+ require 'paperplane/config'
4
5
  require 'paperplane/error'
5
6
  require 'paperplane/version'
6
7
 
7
8
  module Paperplane
8
9
  ENDPOINTS = {
9
- create_job: 'https://api.paperplane.app/v1/jobs',
10
- show_job: 'https://api.paperplane.app/v1/jobs/%{id}',
11
- download_pdf: 'https://download.paperplane.app/'
10
+ download_pdf: 'https://download.paperplane.app/',
11
+ jobs: 'https://api.paperplane.app/jobs'
12
12
  }.freeze
13
- PAGE_SIZES = %w[Letter Legal A3 A4 A5].freeze
14
13
 
15
14
  class << self
16
- attr_accessor :api_key
15
+ attr_writer :config
17
16
 
18
- def configure
19
- yield self
17
+ def config
18
+ @config ||= Config.new
20
19
  end
21
20
 
22
- def create_job(url, page_size: 'A4')
23
- validate_page_size!(page_size)
24
- perform_request(:post, :create_job, url: url, page_size: page_size)
21
+ def configure
22
+ yield(config) if block_given?
25
23
  end
26
24
 
27
- def show_job(id)
28
- perform_request(:get, :show_job, id)
25
+ def version
26
+ @version ||= Paperplane::VERSION
29
27
  end
30
28
 
31
- def download_pdf(url, page_size: 'A4')
32
- response = HTTP.basic_auth(user: self.api_key, pass: "").post(
33
- "https://download.paperplane.app",
34
- json: { url: url, page_size: page_size }
35
- )
36
- response.body.to_s
29
+ def client
30
+ @client ||= HTTP.basic_auth(user: config.api_key, pass: '')
37
31
  end
38
32
 
39
- private
40
-
41
- def http_client
42
- @http_client ||= HTTP.basic_auth(user: api_key, pass: '')
33
+ def prepare_params(url, page_size: 'A4')
34
+ params = { url: url, page_size: page_size }
43
35
  end
44
36
 
45
- def perform_request(method, endpoint_name, *args)
46
- url = build_url(endpoint_name, *args)
47
- puts url
48
- puts args
49
- response = http_client.request(method, url.to_s, json: args.to_json)
50
- puts response.parse
51
- validate_response!(response)
37
+ # Download PDF
38
+ def download_pdf(url, page_size: 'A4')
39
+ params = prepare_params(url, page_size: page_size)
40
+ response = client.post(ENDPOINTS[:download_pdf], form: params)
41
+ raise Paperplane::Error, "Failed to download PDF: #{response.status}" unless response.status.success?
42
+
43
+ config.logger.info("Downloaded PDF: #{url}")
44
+ response.body.to_s
52
45
  end
53
46
 
54
- def build_url(endpoint_name, *args)
55
- format(ENDPOINTS[endpoint_name], args)
56
- end
47
+ # Create Job
48
+ def create_job(url, page_size: 'A4', options: {})
49
+ params = prepare_params(url, page_size: page_size)
50
+ response = client.post(ENDPOINTS[:jobs], form: params.merge(options))
51
+ raise Paperplane::Error, "Failed to create job: #{response.status}" unless response.status.success?
57
52
 
58
- def validate_response!(response)
59
- # raise Paperplane::Error, response.parse['message'] if response.status >= 400
53
+ config.logger.info("Created job for URL: #{url}")
60
54
  response.parse
61
55
  end
62
56
 
63
- def validate_page_size!(page_size)
64
- raise ArgumentError, "Invalid page size '#{page_size}'" unless PAGE_SIZES.include?(page_size)
57
+ # Show Job
58
+ def show_job(job_id)
59
+ response = client.get("#{ENDPOINTS[:jobs]}/#{job_id}")
60
+ raise Paperplane::Error, "Failed to show job: #{response.status}" unless response.status.success?
61
+
62
+ config.logger.info("Fetched job: #{job_id}")
63
+ response.parse
65
64
  end
66
65
  end
67
66
  end
data/paperplane.gemspec CHANGED
@@ -31,6 +31,7 @@ Gem::Specification.new do |spec|
31
31
 
32
32
  # Uncomment to register a new dependency of your gem
33
33
  spec.add_dependency "http"
34
+ spec.add_dependency "webmock"
34
35
 
35
36
  # For more information and examples about making a new gem, check out our
36
37
  # guide at: https://bundler.io/guides/creating_gem.html
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paperplane-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - nolyoi
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: webmock
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  description: A gem to interact with the Paperplane PDF API.
28
42
  email:
29
43
  - nolan@syslogica.io
@@ -38,10 +52,10 @@ files:
38
52
  - README.md
39
53
  - Rakefile
40
54
  - lib/paperplane.rb
55
+ - lib/paperplane/config.rb
41
56
  - lib/paperplane/error.rb
42
57
  - lib/paperplane/version.rb
43
58
  - paperplane.gemspec
44
- - sig/paperplane.rbs
45
59
  homepage: https://github.com/nolyoi
46
60
  licenses: []
47
61
  metadata:
data/sig/paperplane.rbs DELETED
@@ -1,4 +0,0 @@
1
- module Paperplane
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
- end