tindie_api 0.1.4

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
+ SHA256:
3
+ metadata.gz: 112bc88a7cd40d984989ef332c81e0c4c1dfd827e02e5247632955a2c9613678
4
+ data.tar.gz: 18e04b2f25f5f385cff25c997a9ee3fa7e7aadf5cc8149bc5378642f7d18e0dc
5
+ SHA512:
6
+ metadata.gz: acac5e238775d9444c3c4789fdb81946733093bc49550397fb662cba0b28cccc5dbd7ee6c813ff7aa44ae60281358245fc126cfe90dcc3453ac6f38d7f3763fc
7
+ data.tar.gz: be6790c2699cdaf08a104dd1147b0cf93a8ce9c762d2212a1c727dd5920ad60ff3c6d5624dcca29c7a879ef1ab856fca9d63bb4078a0ed66bf5b62d0703683b6
@@ -0,0 +1,53 @@
1
+ name: Ruby Gem
2
+
3
+ on:
4
+ push:
5
+ branches: [ "main" ]
6
+ pull_request:
7
+ branches: [ "main" ]
8
+
9
+ # on:
10
+ # push:
11
+ # branches: [ "main" ]
12
+ # paths:
13
+ # - 'lib/tindie_api/version.rb'
14
+ # pull_request:
15
+ # branches: [ "main" ]
16
+ # paths:
17
+ # - 'lib/tindie_api/version.rb'
18
+
19
+ jobs:
20
+ build:
21
+ name: Build + Publish
22
+ runs-on: ubuntu-latest
23
+ permissions:
24
+ contents: read
25
+ packages: write
26
+
27
+ steps:
28
+ - uses: actions/checkout@v4
29
+ - name: Set up Ruby
30
+ uses: ruby/setup-ruby@v1
31
+
32
+ - name: Publish to GPR
33
+ run: |
34
+ mkdir -p $HOME/.gem
35
+ touch $HOME/.gem/credentials
36
+ chmod 0600 $HOME/.gem/credentials
37
+ printf -- "---\n:github: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
38
+ gem build *.gemspec
39
+ gem push --KEY github --host https://rubygems.pkg.github.com/${OWNER} *.gem
40
+ env:
41
+ GEM_HOST_API_KEY: "Bearer ${{secrets.GITHUB_TOKEN}}"
42
+ OWNER: ${{ github.repository_owner }}
43
+
44
+ - name: Publish to RubyGems
45
+ run: |
46
+ mkdir -p $HOME/.gem
47
+ touch $HOME/.gem/credentials
48
+ chmod 0600 $HOME/.gem/credentials
49
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
50
+ gem build *.gemspec
51
+ gem push *.gem
52
+ env:
53
+ GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}"
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.2.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in tindie_api.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Spencer Owen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # TindieApi
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/tindie_api`. 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
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'tindie_api'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install tindie_api
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tindie_api.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tindie_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__)
data/bin/setup ADDED
@@ -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,3 @@
1
+ module TindieApi
2
+ VERSION = "0.1.4"
3
+ end
data/lib/tindie_api.rb ADDED
@@ -0,0 +1,115 @@
1
+ require "tindie_api/version"
2
+ require 'net/http'
3
+ require 'json'
4
+ require 'date'
5
+
6
+ module TindieApi
7
+ class Error < StandardError; end
8
+
9
+ # Constants
10
+ EXPIRES_TIME = 3600 # 3600s = 1h
11
+
12
+ class TindieProduct
13
+ attr_reader :json_parsed, :model, :options, :qty, :sku, :unit_price, :price, :name
14
+
15
+ def initialize(data)
16
+ @json_parsed = data
17
+ @model = data['model_number']
18
+ @options = data['options']
19
+ @qty = data['quantity']
20
+ @sku = data['sku']
21
+ @unit_price = data['price_unit']
22
+ @price = data['price_total']
23
+ @name = data['product']
24
+ end
25
+ end
26
+
27
+ class TindieOrder
28
+ attr_reader :json_parsed, :date, :date_shipped, :products, :shipped, :refunded, :order_number,
29
+ :recipient_email, :recipient_phone, :address_dict, :address_str, :seller_payout,
30
+ :shipping_cost, :subtotal, :tindie_fee, :cc_fee, :tracking_code, :tracking_url
31
+
32
+ def initialize(data)
33
+ @json_parsed = data
34
+ @date = DateTime.parse(data['date'])
35
+ @date_shipped = DateTime.parse(data['date_shipped'])
36
+ @products = data['items'].map { |i| TindieProduct.new(i) }
37
+ @shipped = data['shipped']
38
+ @refunded = data['refunded']
39
+ @order_number = data['number']
40
+ @recipient_email = data['email']
41
+ @recipient_phone = data['phone']
42
+ @address_dict = {
43
+ city: data['shipping_city'],
44
+ country: data['shipping_country'],
45
+ recipient_name: data['shipping_name'],
46
+ instructions: data['shipping_instructions'],
47
+ postcode: data['shipping_postcode'],
48
+ service: data['shipping_service'],
49
+ state: data['shipping_state'],
50
+ street: data['shipping_street']
51
+ }
52
+ @address_str = "#{data['shipping_name']}\n#{data['shipping_street']}\n" \
53
+ "#{data['shipping_city']} #{data['shipping_state']} #{data['shipping_postcode']}\n" \
54
+ "#{data['shipping_country']}"
55
+ @seller_payout = data['total_seller']
56
+ @shipping_cost = data['total_shipping']
57
+ @subtotal = data['total_subtotal']
58
+ @tindie_fee = data['total_tindiefee']
59
+ @cc_fee = data['total_ccfee']
60
+ if @shipped
61
+ @tracking_code = data['tracking_code']
62
+ @tracking_url = data['tracking_url']
63
+ end
64
+ end
65
+ end
66
+
67
+ class TindieOrdersAPI
68
+ def initialize(username, api_key)
69
+ @usr = username
70
+ @api = api_key
71
+ @cache = { false => nil, true => nil, nil => nil }
72
+ end
73
+
74
+ def get_orders_json(shipped = nil)
75
+ url = "https://www.tindie.com/api/v1/order/?format=json&api_key=#{@api}&username=#{@usr}"
76
+ url += "&shipped=#{shipped}" unless shipped.nil?
77
+ uri = URI(url)
78
+ response = Net::HTTP.get(uri)
79
+ JSON.parse(response)
80
+ end
81
+
82
+ def get_orders(shipped = nil)
83
+ raise ArgumentError, "shipped must be true, false, or nil" if !shipped.nil? && ![true, false].include?(shipped)
84
+ result = get_orders_json(shipped)['orders'].map { |i| TindieOrder.new(i) }
85
+ @cache[shipped] = [Time.now + EXPIRES_TIME, result]
86
+ result
87
+ end
88
+
89
+ def _get_cache_(shipped = nil)
90
+ elem = @cache[shipped]
91
+ return get_orders(shipped) if elem.nil? || elem[0] < Time.now
92
+ elem[1]
93
+ end
94
+
95
+ def get_last_order
96
+ _get_cache_.first
97
+ end
98
+
99
+ def average_order_revenue(limit = 20)
100
+ orders = _get_cache_.take(limit)
101
+ orders.sum(&:seller_payout) / orders.size.to_f
102
+ end
103
+
104
+ def average_order_shipping(limit = 20)
105
+ orders = _get_cache_.take(limit)
106
+ orders.sum(&:shipping_cost) / orders.size.to_f
107
+ end
108
+
109
+ def average_order_timedelta(limit = 20)
110
+ orders = _get_cache_.take(limit + 1)
111
+ total_time = orders.each_cons(2).sum { |a, b| a.date - b.date }
112
+ total_time / [limit, orders.size - 1].min
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,41 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "tindie_api/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tindie_api"
8
+ spec.version = TindieApi::VERSION
9
+ spec.authors = ["Spencer Owen"]
10
+ spec.email = ["owenspencer@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby Library to interact with tindie.com api}
13
+ spec.description = %q{Ruby Library to interact with tindie.com api}
14
+ spec.homepage = "https://github.com/spuder/tindie_api"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/spuder/tindie_api"
23
+ spec.metadata["changelog_uri"] = "https://github.com/spuder/tindie_api"
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against " \
26
+ "public gem pushes."
27
+ end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_development_dependency "bundler", "~> 1.17"
39
+ spec.add_development_dependency "rake", "~> 10.0"
40
+ spec.add_development_dependency "minitest", "~> 5.0"
41
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tindie_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - Spencer Owen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-12-17 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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: Ruby Library to interact with tindie.com api
56
+ email:
57
+ - owenspencer@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".github/workflows/gem-push.yml"
63
+ - ".gitignore"
64
+ - ".ruby-version"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/tindie_api.rb
72
+ - lib/tindie_api/version.rb
73
+ - tindie_api.gemspec
74
+ homepage: https://github.com/spuder/tindie_api
75
+ licenses:
76
+ - MIT
77
+ metadata:
78
+ homepage_uri: https://github.com/spuder/tindie_api
79
+ source_code_uri: https://github.com/spuder/tindie_api
80
+ changelog_uri: https://github.com/spuder/tindie_api
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.4.10
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Ruby Library to interact with tindie.com api
100
+ test_files: []