shopify-report 0.1.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
+ SHA1:
3
+ metadata.gz: 05e8c968d672dc3bdde08d88dbfe6514bdb4403d
4
+ data.tar.gz: e6bd122d8b63b14f2fd807e32ff4eb168a3d6287
5
+ SHA512:
6
+ metadata.gz: a6bee474aada73eeed761842a18c293f817c82dd71591dc42fadc4f38e877072f9a471f35cb87c0bedf08c95e88bf469504946c4af12f27632a6b1e1fabb16cf
7
+ data.tar.gz: 256867539f30390e76d03fe0038832048dd9aa17772eceb193231f5637bfcaac3bb0ce9683c6d832da3759f1fcabf0ebf032c78e5c5d510cd3020f85eafe91af
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Dhurba Baral
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,42 @@
1
+ # Shopify
2
+ Fetch data from shopify which are not available through API call
3
+
4
+ ## Features
5
+ * Read any data from Shopify. It doesn't use shopify API.
6
+ * You can connect to multiple shops in the same application.
7
+ * Requires `domain`, `login` and `password` to fetch data.
8
+
9
+ ## Examples
10
+ ```
11
+ shop = ShopifyScrape.new('domain', 'username', 'password')
12
+ date_min = 1.month.ago; date_max = Time.zone.now
13
+ payout_report = shop.payout_report(date_min, date_max) # get payout reports
14
+ sales_report = shop.sales_report(date_min, date_max) # get sales report
15
+ shopify_url = shop.shopify_url # shopify url
16
+ ```
17
+
18
+
19
+ ## Installation
20
+ Add this line to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem 'shopify', :git => 'git://github.com/dhurba87/shopify.git'
24
+ ```
25
+
26
+ And then execute:
27
+ ```bash
28
+ $ bundle
29
+ ```
30
+
31
+ ## Contributing
32
+ Guidelines to follow:
33
+
34
+ * Fork the repo
35
+ * Create your feature branch (git checkout -b my-new-feature)
36
+ * Make sure you have some tests, and they pass!(bundle exec rake)
37
+ * Commit your changes(git commit -am 'Add some feature')
38
+ * Push to the branch( git push origin my-new-feature)
39
+ * Create new Pull Request
40
+
41
+ ## License
42
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,33 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Shopify'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+
33
+ task default: :test
@@ -0,0 +1,5 @@
1
+ require 'shopify/captcha_exception'
2
+ require 'shopify/shopify_scrape'
3
+
4
+ module Shopify
5
+ end
@@ -0,0 +1,5 @@
1
+ class CaptchaException < StandardError
2
+ def initialize(msg = 'Captcha mismatch')
3
+ super
4
+ end
5
+ end
@@ -0,0 +1,116 @@
1
+ require 'net/http'
2
+ require 'csv'
3
+
4
+ class ShopifyScrape
5
+ def initialize(domain, username, password)
6
+ @domain = domain
7
+ @username = username
8
+ @password = password
9
+
10
+ @cookie = login
11
+ end
12
+
13
+ def shopify_url
14
+ "https://#{@domain}.myshopify.com"
15
+ end
16
+
17
+ def payout_report(date_min=Time.zone.now.beginning_of_month, date_max=Time.zone.now.end_of_month)
18
+ url = URI("#{shopify_url}/admin/payments/payouts.json?date_min=#{date_min}&date_max=#{date_max}")
19
+
20
+ http = Net::HTTP.new(url.host, url.port)
21
+ http.use_ssl = true
22
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
23
+
24
+ request1 = Net::HTTP::Get.new(url)
25
+ request1["content-type"] = 'application/x-www-form-urlencoded'
26
+ request1["cache-control"] = 'no-cache'
27
+ request1["cookie"] = @cookie
28
+
29
+ response = http.request(request1)
30
+
31
+ raise Exception, parse_error(response.body) if response.code.to_i >= 400
32
+ JSON.parse(response.body)
33
+ end
34
+
35
+ def sales_report(date_min=Time.zone.now.beginning_of_month, date_max=Time.zone.now.end_of_month)
36
+ url = URI("https://analytics.shopify.com/query?q=SHOW%20orders%20AS%20%22orders%22%2C%20gross_sales%20AS%20%22gross_sales%22%2C%20discounts%20AS%20%22discounts%22%2C%20returns%20AS%20%22returns%22%2C%20net_sales%20AS%20%22net_sales%22%2C%20shipping%20AS%20%22shipping%22%2C%20taxes%20AS%20%22taxes%22%2C%20total_sales%20AS%20%22total_sales%22%20OVER%20month%20AS%20%22month%22%20FROM%20sales%20SINCE%20-11m%20UNTIL%20today%20ORDER%20BY%20%22month%22%20ASC&format=csv&source=shopify-summary&token=#{get_token}&beta=true&handle=sales_#{date_min}_#{date_max}")
37
+
38
+ http = Net::HTTP.new(url.host, url.port)
39
+ http.use_ssl = true
40
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
41
+
42
+ request = Net::HTTP::Get.new(url)
43
+ request["cookie"] = @cookie
44
+ request["cache-control"] = 'no-cache'
45
+
46
+ response = http.request(request)
47
+
48
+ raise Exception, parse_error(response.body) if response.code.to_i >= 400
49
+
50
+ csv_arr = CSV.parse(response.read_body)
51
+ keys = csv_arr.shift
52
+ arr_of_object = csv_arr.map {|a| Hash[keys.zip(a)]}
53
+
54
+ total_shipping = 0
55
+ total_taxes = 0
56
+ arr_of_object.each do |obj|
57
+ total_shipping = total_shipping + obj['shipping'].to_f
58
+ total_taxes = total_taxes + obj['taxes'].to_f
59
+ end
60
+
61
+ {shipping: total_shipping, taxes: total_taxes}
62
+ end
63
+
64
+ private
65
+
66
+ def login
67
+ login_url = "#{shopify_url}/admin/auth/login"
68
+
69
+ url = URI(login_url)
70
+ http = Net::HTTP.new(url.host, url.port)
71
+ http.use_ssl = true
72
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
73
+
74
+ request = Net::HTTP::Post.new(url)
75
+ request["content-type"] = 'application/x-www-form-urlencoded'
76
+ request["cache-control"] = 'no-cache'
77
+ request.body = "login=#{@username}&password=#{@password}"
78
+
79
+ response = http.request(request)
80
+
81
+ # Check for invalid username/password
82
+ validation_error_message = Nokogiri::HTML(response.body).css('span.validation-error__message')
83
+
84
+ raise validation_error_message.text if validation_error_message.length > 0
85
+ raise 'Domain not found' if response.code.to_i == 404
86
+ raise CaptchaException, 'Captcha displayed while logging in' unless response.code.to_i == 302
87
+
88
+ # Return cookie
89
+ response['set-cookie']
90
+ end
91
+
92
+ def get_token
93
+ url = URI("#{shopify_url}/admin/reportify/token.json")
94
+
95
+ http = Net::HTTP.new(url.host, url.port)
96
+ http.use_ssl = true
97
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
98
+
99
+ request = Net::HTTP::Get.new(url)
100
+ request["cookie"] = @cookie
101
+ request["cache-control"] = 'no-cache'
102
+
103
+ response = http.request(request)
104
+ JSON.parse(response.body)['token']
105
+ end
106
+
107
+ def parse_error(response_body)
108
+ begin
109
+ JSON.parse(response_body)['message']
110
+ rescue JSON::ParserError
111
+ # todo::Use nokogiri to parse error from html page
112
+ 'Error occurred.'
113
+ end
114
+ end
115
+
116
+ end
@@ -0,0 +1,3 @@
1
+ module Shopify
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :shopify do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shopify-report
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dhurba Baral
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.1'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 5.1.4
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '5.1'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 5.1.4
33
+ description: Shopify reports which are not available through API
34
+ email:
35
+ - dhurba87@gmail.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - MIT-LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - lib/shopify.rb
44
+ - lib/shopify/captcha_exception.rb
45
+ - lib/shopify/shopify_scrape.rb
46
+ - lib/shopify/version.rb
47
+ - lib/tasks/shopify_tasks.rake
48
+ homepage: https://github.com/dhurba87/shopify
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.6.14
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: fetch shopify payout report and sales report
72
+ test_files: []