whiplash-batches-api 1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/Gemfile +3 -0
- data/README.md +100 -0
- data/Rakefile +11 -0
- data/lib/whiplash-batches-api.rb +17 -0
- data/lib/whiplash/batches/api.rb +80 -0
- data/lib/whiplash/batches/version.rb +5 -0
- data/whiplash-batches-api.gemspec +25 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f41cb18432683142210262aa20c381ac90dcaecd78cad6d4dc2956782854aa29
|
4
|
+
data.tar.gz: 91380399218da67b887f547467dbb67eb9921dd53afb2019fba6b750ef2b87de
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 957010d5543621a1377f1a8ddf51c260b278443a3bd31cd3e0b7becc5658a5f0615b36abdcf788e8e0d6fb3974f42db2964c12cd9c4ce9a104352c9a1fc9e0df
|
7
|
+
data.tar.gz: 668b5b8dfb4608922f2a476b91468a59879a2e7d9a2c0508d55b688653d616f94f42aaacf6443ddaa84087d00b35ac4fdc5b131134a9050ac8b1f3f87048c03d
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
# Whiplash Batches Api
|
2
|
+
|
3
|
+
Whiplash Batches Api
|
4
|
+
## Get Started
|
5
|
+
```ruby
|
6
|
+
gem 'whiplash-batches-api', github: 'wqsaali/whiplash-batches-api'
|
7
|
+
```
|
8
|
+
create `config/initializer/whiplash_initializer.rb` file
|
9
|
+
```ruby
|
10
|
+
Whiplash.configure do |config|
|
11
|
+
config.whiplash_email = WHIPLASH_EMAIL
|
12
|
+
config.whiplash_password = WHIPLASH_PASSWORD
|
13
|
+
config.whiplash_base_url = 'https://www.getwhiplash.com'
|
14
|
+
end
|
15
|
+
```
|
16
|
+
## Usage
|
17
|
+
``` ruby
|
18
|
+
handler = Whiplash::Batches::Api.new
|
19
|
+
```
|
20
|
+
### GET BATCHES
|
21
|
+
``` ruby
|
22
|
+
handler.batches
|
23
|
+
```
|
24
|
+
response is
|
25
|
+
``` json
|
26
|
+
[{"batch": "437737",
|
27
|
+
"to_pack": "2",
|
28
|
+
"to_label": "2",
|
29
|
+
"total_items": "51",
|
30
|
+
"printed": "Mar 01, 2019 2:20PM",
|
31
|
+
"pre_pack_buy": "",
|
32
|
+
"assigned": "Start",
|
33
|
+
"started": "",
|
34
|
+
"stopped": "",
|
35
|
+
"min": "",
|
36
|
+
"incidents": "",
|
37
|
+
"order_ids": ["7511027", "7515574"],
|
38
|
+
"page": 1
|
39
|
+
},
|
40
|
+
{"batch": "437732",
|
41
|
+
"to_pack": "2",
|
42
|
+
"to_label": "2",
|
43
|
+
"total_items": "51",
|
44
|
+
"printed": "Mar 01, 2019 2:20PM",
|
45
|
+
"pre_pack_buy": "",
|
46
|
+
"assigned": "Start",
|
47
|
+
"started": "",
|
48
|
+
"stopped": "",
|
49
|
+
"min": "",
|
50
|
+
"incidents": "",
|
51
|
+
"order_ids": ["7511021", "7515575"],
|
52
|
+
"page": 1
|
53
|
+
}
|
54
|
+
]
|
55
|
+
```
|
56
|
+
|
57
|
+
You can also add page number and ignore_list
|
58
|
+
1. if page number is given we fetch from given page number to end (ie if total pages we 10 and given page number is 7 then we return all batches from 7 to 10 pages)
|
59
|
+
2. If you want to ignore some batches add array of batch ids
|
60
|
+
``` ruby
|
61
|
+
handler.batches(1, ["437737"])
|
62
|
+
```
|
63
|
+
It returns
|
64
|
+
``` json
|
65
|
+
[
|
66
|
+
{"batch": "437732",
|
67
|
+
"to_pack": "2",
|
68
|
+
"to_label": "2",
|
69
|
+
"total_items": "51",
|
70
|
+
"printed": "Mar 01, 2019 2:20PM",
|
71
|
+
"pre_pack_buy": "",
|
72
|
+
"assigned": "Start",
|
73
|
+
"started": "",
|
74
|
+
"stopped": "",
|
75
|
+
"min": "",
|
76
|
+
"incidents": "",
|
77
|
+
"order_ids": ["7511021", "7515575"],
|
78
|
+
"page": 1
|
79
|
+
}
|
80
|
+
]
|
81
|
+
```
|
82
|
+
|
83
|
+
### GET ORDERS
|
84
|
+
``` ruby
|
85
|
+
handler.orders(437732)
|
86
|
+
```
|
87
|
+
it returns
|
88
|
+
``` json
|
89
|
+
["7511021", "7515575"]
|
90
|
+
```
|
91
|
+
|
92
|
+
# License
|
93
|
+
|
94
|
+
Copyright (c) 2019 Waqas Ali
|
95
|
+
|
96
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
97
|
+
|
98
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
99
|
+
|
100
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
|
3
|
+
desc "Build the gem"
|
4
|
+
task :build do
|
5
|
+
system "gem build whiplash-batches-api.gemspec"
|
6
|
+
end
|
7
|
+
|
8
|
+
desc "Build and release the gem"
|
9
|
+
task :release => :build do
|
10
|
+
system "gem push whiplash-batches-api-#{Whiplash::Batches::VERSION}.gem"
|
11
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "whiplash/batches/version"
|
2
|
+
require "whiplash/batches/api"
|
3
|
+
|
4
|
+
module Whiplash
|
5
|
+
class << self
|
6
|
+
attr_accessor :configuration
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.configure
|
10
|
+
self.configuration ||= Configuration.new
|
11
|
+
yield(configuration)
|
12
|
+
end
|
13
|
+
|
14
|
+
class Configuration
|
15
|
+
attr_accessor :whiplash_email, :whiplash_password, :whiplash_base_url
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'mechanize'
|
2
|
+
class Whiplash::Batches::Api
|
3
|
+
attr_reader :agent, :current_page, :whiplash_base_url, :order_current_page
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
if Whiplash.configuration.present?
|
7
|
+
@username = Whiplash.configuration.whiplash_email
|
8
|
+
@password = Whiplash.configuration.whiplash_password
|
9
|
+
@whiplash_base_url = Whiplash.configuration.whiplash_base_url
|
10
|
+
end
|
11
|
+
check_confuration_variables
|
12
|
+
@agent = Mechanize.new
|
13
|
+
login
|
14
|
+
end
|
15
|
+
|
16
|
+
def batches(page = 1, ignore_ids = [])
|
17
|
+
results = []
|
18
|
+
ignore_ids.map!(&:to_s) # convert to ids of string
|
19
|
+
@current_page = agent.get("#{@whiplash_base_url}/order_batches?page=#{page}")
|
20
|
+
results << build_batch_hash(current_page, ignore_ids, page)
|
21
|
+
page += 1 # page is scrapped already
|
22
|
+
loop do
|
23
|
+
next_page = current_page.links.find{ |l| l.href.to_s.include?("/order_batches?page=#{page}") }
|
24
|
+
if next_page.present?
|
25
|
+
batch_page = next_page.click
|
26
|
+
results << build_batch_hash(batch_page, ignore_ids, page)
|
27
|
+
page += 1
|
28
|
+
else
|
29
|
+
break
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
results.flatten
|
34
|
+
end
|
35
|
+
|
36
|
+
def orders(batch_id = nil)
|
37
|
+
return [] if batch_id.nil?
|
38
|
+
@order_current_page = agent.get("#{@whiplash_base_url}/order_batches/#{batch_id}")
|
39
|
+
orders = order_current_page.links.select { |l| l.href.to_s.match(/\/orders\/.[0-9]+/) }
|
40
|
+
orders.map{ |l| l.text.split('-').first }
|
41
|
+
end
|
42
|
+
|
43
|
+
def login
|
44
|
+
page = agent.get("#{@whiplash_base_url}/login")
|
45
|
+
@current_page = page.form_with(:id => 'new_user') do |form|
|
46
|
+
form.field_with(name: 'user[email]').value = @username
|
47
|
+
form.field_with(name: 'user[password]').value = @password
|
48
|
+
end.submit
|
49
|
+
if current_page.title == "Log In"
|
50
|
+
raise "Credentials: Login credentials invalid!"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def check_confuration_variables
|
57
|
+
raise 'whiplash_email is not configured in initializer' if @username.nil?
|
58
|
+
raise 'whiplash_password is not configured in initializer' if @password.nil?
|
59
|
+
raise 'whiplash_base_url is not configured in initializer' if @whiplash_base_url.nil?
|
60
|
+
end
|
61
|
+
|
62
|
+
def build_batch_hash(current_page, ignore_ids, page)
|
63
|
+
records = []
|
64
|
+
table = current_page.search('table#order-batches')[0]
|
65
|
+
if table.present?
|
66
|
+
headers = table.search('thead tr th').map{ |th| th.text.downcase.parameterize.underscore }
|
67
|
+
table.search('tbody tr').each do |tr|
|
68
|
+
next if ignore_ids.include?(tr.at('td a').text.strip)
|
69
|
+
hash = {}
|
70
|
+
tr.elements.each_with_index do |td, index|
|
71
|
+
hash[headers[index]] = td.at('a').present? ? td.at('a').text.strip : td.text.strip
|
72
|
+
end
|
73
|
+
hash['order_ids'] = orders(hash['batch'])
|
74
|
+
hash['page'] = page
|
75
|
+
records << hash
|
76
|
+
end
|
77
|
+
records
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/whiplash/batches/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.add_runtime_dependency 'mechanize', '~> 2.7'
|
6
|
+
|
7
|
+
|
8
|
+
# gem.add_development_dependency 'rspec', '~> 2.7'
|
9
|
+
# gem.add_development_dependency 'rack-test'
|
10
|
+
# gem.add_development_dependency 'webmock'
|
11
|
+
# gem.add_development_dependency 'simplecov'
|
12
|
+
|
13
|
+
gem.authors = ["Waqas Ali"]
|
14
|
+
gem.email = ["wqsaali@gmail.com"]
|
15
|
+
gem.description = %q{Enable Whiplash batches api with email/password}
|
16
|
+
gem.summary = %q{Enable Whiplash batches api with emai/password}
|
17
|
+
gem.homepage = "https://github.com/wqsaali/whiplash-batches-api"
|
18
|
+
gem.license = "GPL-3.0"
|
19
|
+
|
20
|
+
gem.files = `git ls-files`.split("\n")
|
21
|
+
# gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
gem.name = "whiplash-batches-api"
|
23
|
+
gem.require_paths = ["lib"]
|
24
|
+
gem.version = Whiplash::Batches::VERSION
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: whiplash-batches-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Waqas Ali
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-03-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: mechanize
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.7'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.7'
|
27
|
+
description: Enable Whiplash batches api with email/password
|
28
|
+
email:
|
29
|
+
- wqsaali@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- Gemfile
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- lib/whiplash-batches-api.rb
|
39
|
+
- lib/whiplash/batches/api.rb
|
40
|
+
- lib/whiplash/batches/version.rb
|
41
|
+
- whiplash-batches-api.gemspec
|
42
|
+
homepage: https://github.com/wqsaali/whiplash-batches-api
|
43
|
+
licenses:
|
44
|
+
- GPL-3.0
|
45
|
+
metadata: {}
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 2.7.7
|
63
|
+
signing_key:
|
64
|
+
specification_version: 4
|
65
|
+
summary: Enable Whiplash batches api with emai/password
|
66
|
+
test_files: []
|