lulu_api 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d24ec37cde205a303675449206ec5f5fa9727af2ae89cc0dc800f649ac68596f
4
+ data.tar.gz: 3f63ec4e40f8fc72ca21c57fef5aac263eba6c017ccdda4c66cd89f401e4ebc3
5
+ SHA512:
6
+ metadata.gz: 9c94f54324b2b58280c53fe6905bb09dddc07611d9716f2e776e5a4cf3ff0494a5fe10f0552eec13b3c08994d57535a6aa92f908c8dbecbd2fbd96c5240e477f
7
+ data.tar.gz: e47ea430be31fa18eaad84e3dda647cb1a5cd1e16a51fb8bc956220251f09c0afaa555efc28d4405248a0634ff24813a0a3f67919945c2649508b66fb8eadf1d
@@ -0,0 +1,26 @@
1
+ name: Ruby Gem
2
+
3
+ on: [release]
4
+
5
+ jobs:
6
+ build:
7
+ name: Build + Publish
8
+ runs-on: ubuntu-latest
9
+
10
+ steps:
11
+ - uses: actions/checkout@v2
12
+ - name: Set up Ruby 2.6
13
+ uses: actions/setup-ruby@v1
14
+ with:
15
+ ruby-version: 2.6.x
16
+
17
+ - name: Publish to RubyGems
18
+ run: |
19
+ mkdir -p $HOME/.gem
20
+ touch $HOME/.gem/credentials
21
+ chmod 0600 $HOME/.gem/credentials
22
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
23
+ gem build *.gemspec
24
+ gem push *.gem
25
+ env:
26
+ GEM_HOST_API_KEY: ${{secrets.RUBYGEMS_AUTH_TOKEN}}
@@ -0,0 +1,20 @@
1
+ name: Ruby
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+
8
+ runs-on: ubuntu-latest
9
+
10
+ steps:
11
+ - uses: actions/checkout@v2
12
+ - name: Set up Ruby 2.6
13
+ uses: actions/setup-ruby@v1
14
+ with:
15
+ ruby-version: 2.6.x
16
+ - name: Build and test with RSpec
17
+ run: |
18
+ gem install bundler
19
+ bundle install --jobs 4 --retry 3
20
+ bundle exec rspec
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.idea
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <Settings><!--This file was automatically generated by Ruby plugin.
3
+ You are allowed to:
4
+ 1. Remove rake task
5
+ 2. Add existing rake tasks
6
+ To add existing rake tasks automatically delete this file and reload the project.
7
+ --><RakeGroup description="" fullCmd="" taksId="rake"><RakeTask description="Build lulu_api-0.1.0.gem into the pkg directory" fullCmd="build" taksId="build" /><RakeTask description="Remove any temporary products" fullCmd="clean" taksId="clean" /><RakeTask description="Remove any generated files" fullCmd="clobber" taksId="clobber" /><RakeTask description="Build and install lulu_api-0.1.0.gem into system gems" fullCmd="install" taksId="install" /><RakeGroup description="" fullCmd="" taksId="install"><RakeTask description="Build and install lulu_api-0.1.0.gem into system gems without network access" fullCmd="install:local" taksId="local" /></RakeGroup><RakeTask description="Create tag v0.1.0 and build and push lulu_api-0.1.0.gem to TODO: Set to 'http://mygemserver.com'" fullCmd="release[remote]" taksId="release[remote]" /><RakeTask description="" fullCmd="default" taksId="default" /><RakeTask description="" fullCmd="release" taksId="release" /><RakeGroup description="" fullCmd="" taksId="release"><RakeTask description="" fullCmd="release:guard_clean" taksId="guard_clean" /><RakeTask description="" fullCmd="release:rubygem_push" taksId="rubygem_push" /><RakeTask description="" fullCmd="release:source_control_push" taksId="source_control_push" /></RakeGroup></RakeGroup></Settings>
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ group :test do
4
+ gem 'pry'
5
+ gem 'rspec'
6
+ gem 'webmock'
7
+ end
8
+
9
+ # Specify your gem's dependencies in lulu_api.gemspec
10
+ gemspec
@@ -0,0 +1,61 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lulu_api (0.1.0)
5
+ httparty
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.7.0)
11
+ public_suffix (>= 2.0.2, < 5.0)
12
+ coderay (1.1.2)
13
+ crack (0.4.3)
14
+ safe_yaml (~> 1.0.0)
15
+ diff-lcs (1.3)
16
+ hashdiff (1.0.0)
17
+ httparty (0.18.0)
18
+ mime-types (~> 3.0)
19
+ multi_xml (>= 0.5.2)
20
+ method_source (0.9.2)
21
+ mime-types (3.3.1)
22
+ mime-types-data (~> 3.2015)
23
+ mime-types-data (3.2019.1009)
24
+ multi_xml (0.6.0)
25
+ pry (0.12.2)
26
+ coderay (~> 1.1.0)
27
+ method_source (~> 0.9.0)
28
+ public_suffix (4.0.3)
29
+ rake (13.0.1)
30
+ rspec (3.9.0)
31
+ rspec-core (~> 3.9.0)
32
+ rspec-expectations (~> 3.9.0)
33
+ rspec-mocks (~> 3.9.0)
34
+ rspec-core (3.9.1)
35
+ rspec-support (~> 3.9.1)
36
+ rspec-expectations (3.9.0)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.9.0)
39
+ rspec-mocks (3.9.1)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.9.0)
42
+ rspec-support (3.9.2)
43
+ safe_yaml (1.0.5)
44
+ webmock (3.8.2)
45
+ addressable (>= 2.3.6)
46
+ crack (>= 0.3.2)
47
+ hashdiff (>= 0.4.0, < 2.0.0)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ bundler (~> 2.0)
54
+ lulu_api!
55
+ pry
56
+ rake (~> 13.0)
57
+ rspec
58
+ webmock
59
+
60
+ BUNDLED WITH
61
+ 2.0.2
@@ -0,0 +1,36 @@
1
+ # LuluApi
2
+ ![Rspec](https://github.com/yatryan/lulu_api/workflows/Ruby/badge.svg) ![Ruby Gem](https://github.com/yatryan/lulu_api/workflows/Ruby%20Gem/badge.svg)
3
+
4
+ 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/lulu_api`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+
6
+ TODO: Delete this and the text above, and describe your gem
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'lulu_api'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install lulu_api
23
+
24
+ ## Usage
25
+
26
+ TODO: Write usage instructions here
27
+
28
+ ## Development
29
+
30
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
31
+
32
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/yatryan/lulu_api.
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "lulu_api"
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(__FILE__)
@@ -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,15 @@
1
+ require "httparty"
2
+ require "lulu_api/version"
3
+ require "lulu_api/client"
4
+
5
+ module LuluApi
6
+ class Error < StandardError; end
7
+
8
+ class << self
9
+ attr_writer :logger
10
+
11
+ def logger
12
+ @logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,84 @@
1
+ require 'json'
2
+ require "lulu_api/client/auth"
3
+ require "lulu_api/client/calculations"
4
+ require "lulu_api/client/print_jobs"
5
+
6
+ module LuluApi
7
+ class Client
8
+ include HTTParty
9
+ include LuluApi::Client::Auth
10
+ include LuluApi::Client::Calculations
11
+ include LuluApi::Client::PrintJobs
12
+
13
+ headers 'Content-Type' => 'application/json'
14
+ headers 'Cache-Control' => 'no-cache'
15
+
16
+ attr_accessor :token, :max_retries
17
+
18
+ def initialize(client_key = nil, client_secret = nil, auth_string = nil)
19
+ @client_key = client_key || ENV["LULU_CLIENT_KEY"]
20
+ @client_secret = client_secret || ENV["LULU_SECRET_KEY"]
21
+ @auth_string = auth_string || ENV["LULU_AUTH_STRING"]
22
+
23
+ @sandbox = ENV["RAILS_ENV"] == 'development' || ENV["LULU_USE_SANDBOX"]
24
+ self.class.base_uri @sandbox ? 'https://api.sandbox.lulu.com/' : 'https://api.lulu.com/'
25
+
26
+ @max_retries = 1
27
+ end
28
+
29
+ def base_url
30
+ self.class.base_uri
31
+ end
32
+
33
+ protected
34
+
35
+ ##
36
+ # Make API call to Lulu API. Handles if a 401 is retrieved.
37
+ #
38
+ # @param &block <Block> - Actual API Call
39
+ #
40
+ # @return response <Hash> - Response from Lulu
41
+ #
42
+ def make_api_call
43
+ response = nil
44
+ attempted_retries = 0
45
+ is_successful = false
46
+
47
+ while attempted_retries <= @max_retries && !is_successful
48
+ fetch_token if @token.nil?
49
+ response = yield
50
+
51
+ fetch_token if @token.nil?
52
+ response = yield
53
+
54
+ if response.code == 401
55
+ # Old Token, refresh and we will try again
56
+ @token = nil
57
+ else
58
+ # No 401, should be good!
59
+ is_successful = true
60
+ end
61
+
62
+ # Bump Attempts
63
+ attempted_retries = attempted_retries + 1
64
+ end
65
+
66
+ handle_lulu_response response
67
+ end
68
+
69
+ ##
70
+ # Handle response from Lulu safely.
71
+ #
72
+ def handle_lulu_response(response)
73
+ if !response.response.kind_of?(Net::HTTPSuccess)
74
+ LuluApi.logger.error("Error: Code: #{response.code}\nResponse:\n#{response.body}")
75
+ nil
76
+ elsif response.body && response.body != ''
77
+ response.parsed_response
78
+ else
79
+ LuluApi.logger.info("Error: Something went wrong, expected response not received.\n#{response.inspect}")
80
+ nil
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,17 @@
1
+ module LuluApi
2
+ class Client
3
+ module Auth
4
+
5
+ def fetch_token
6
+ body = "grant_type=client_credentials&client_key=#{@client_key}&client_secret=#{@client_secret}"
7
+ headers = {'Authorization' => @auth_string, 'Content-Type' => 'application/x-www-form-urlencoded' }
8
+ response = self.class.post("/auth/realms/glasstree/protocol/openid-connect/token", { headers: headers, body: body })
9
+ @token = response.parsed_response['access_token']
10
+
11
+ self.class.headers 'Authorization' => "Bearer #{@token}"
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ module LuluApi
2
+ class Client
3
+ module Calculations
4
+
5
+ def print_job_cost_calculations(body = {})
6
+ LuluApi.logger.debug 'Lulu: Requesting Print Job Estimates'
7
+
8
+ make_api_call do
9
+ self.class.post("/print-job-cost-calculations", { body: body.to_json })
10
+ end
11
+ end
12
+
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,44 @@
1
+ module LuluApi
2
+ class Client
3
+ module PrintJobs
4
+
5
+ def list_print_jobs(options = {})
6
+ LuluApi.logger.debug 'Lulu: Listing print jobs'
7
+
8
+ make_api_call do
9
+ self.class.get("/print-jobs", { query: options })
10
+ end
11
+ end
12
+
13
+ def get_print_job(id)
14
+ LuluApi.logger.debug 'Lulu: Getting print job'
15
+
16
+ make_api_call do
17
+ self.class.get("/print-jobs/#{id}")
18
+ end
19
+ end
20
+
21
+ def get_print_job_costs(id)
22
+ LuluApi.logger.debug 'Lulu: Getting print job cost'
23
+
24
+ make_api_call do
25
+ self.class.get("/print-jobs/#{id}/costs")
26
+ end
27
+ end
28
+
29
+ def get_print_job_status(id)
30
+ LuluApi.logger.debug 'Lulu: Getting print job status'
31
+ make_api_call do
32
+ self.class.get("/print-jobs/#{id}/status")
33
+ end
34
+ end
35
+
36
+ def create_print_job(job)
37
+ LuluApi.logger.debug 'Lulu: Creating print job'
38
+ make_api_call do
39
+ self.class.post("/print-jobs", { body: job.to_json })
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module LuluApi
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "lulu_api/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "lulu_api"
7
+ spec.version = LuluApi::VERSION
8
+ spec.authors = ["Taylor Ryan"]
9
+ spec.email = ["taylor@yatryan.com"]
10
+
11
+ spec.summary = "Client Library for Lulu Print on Demand API"
12
+ spec.homepage = "https://github.com/yatryan/lulu_api"
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/yatryan/lulu_api"
16
+ spec.metadata["changelog_uri"] = "https://github.com/yatryan/lulu_api/CHANGELOG.md"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_development_dependency "bundler", "~> 2.0"
28
+ spec.add_development_dependency "rake", "~> 13.0"
29
+
30
+ spec.add_dependency "httparty"
31
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lulu_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Taylor Ryan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-05-24 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - taylor@yatryan.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".github/workflows/gempush.yml"
63
+ - ".github/workflows/ruby.yml"
64
+ - ".gitignore"
65
+ - ".rakeTasks"
66
+ - ".rspec"
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/lulu_api.rb
74
+ - lib/lulu_api/client.rb
75
+ - lib/lulu_api/client/auth.rb
76
+ - lib/lulu_api/client/calculations.rb
77
+ - lib/lulu_api/client/print_jobs.rb
78
+ - lib/lulu_api/version.rb
79
+ - lulu_api.gemspec
80
+ homepage: https://github.com/yatryan/lulu_api
81
+ licenses: []
82
+ metadata:
83
+ homepage_uri: https://github.com/yatryan/lulu_api
84
+ source_code_uri: https://github.com/yatryan/lulu_api
85
+ changelog_uri: https://github.com/yatryan/lulu_api/CHANGELOG.md
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubygems_version: 3.0.3
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Client Library for Lulu Print on Demand API
105
+ test_files: []