newegg 1.0.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: 5e478d191ea6919320a5ec5f94f2d3e4a2cc2683
4
+ data.tar.gz: b966fc79c729baf1066447bc95c8aafe15a50685
5
+ SHA512:
6
+ metadata.gz: d88d9d5e08c0612f7ee8a9f270a8b931d43fb6b335b3e60fdda348917871d4ab7e46d00da16940cd5db9a50c21d0d1ecf060436b0edca446d3ce8597edd0901b
7
+ data.tar.gz: 7cbc0cff6c21ecc00ae5ab241181489c9fdd9f5101b958354f41061b41f385791b3c9a9ad0d097290d54f87f615e7072d8da9523e7f6c835f73bef38f0d4566f
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
@@ -0,0 +1,7 @@
1
+ Metrics/LineLength:
2
+ AllowURI: true
3
+ Enabled: false
4
+
5
+ Style/Documentation:
6
+ Enabled: false
7
+
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source 'http://ruby.taobao.org'
2
+
3
+ gem 'yard'
4
+
5
+ group :development do
6
+ gem 'pry'
7
+ gem 'pry-rescue'
8
+ end
9
+
10
+ group :test do
11
+ gem 'rubocop', require: false
12
+ gem 'simplecov', require: false
13
+ gem 'webmock'
14
+ end
15
+
16
+ # Specify your gem's dependencies in newegg.gemspec
17
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 ryancheung
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # Newegg
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'newegg'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install newegg
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/newegg/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,6 @@
1
+ require 'newegg/version'
2
+ require 'newegg/client'
3
+
4
+ module Newegg
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,7 @@
1
+ require 'newegg/orders'
2
+
3
+ module Newegg
4
+ module API
5
+ include Newegg::Orders
6
+ end
7
+ end
@@ -0,0 +1,96 @@
1
+ require 'newegg/version'
2
+ require 'faraday'
3
+ require 'newegg/response/parse_json'
4
+ require 'newegg/api'
5
+
6
+ module Newegg
7
+ # API Client
8
+ class Client
9
+ include Newegg::API
10
+
11
+ DEFAULT_ENDPOINT = 'https://api.newegg.com.cn'
12
+
13
+ attr_reader :app_key, :access_token
14
+
15
+ # Initializes a new Client object
16
+ # @param options [Hash]
17
+ # @option options [String] :app_key Required.
18
+ # @option options [String] :access_token Required.
19
+ def initialize(options)
20
+ @app_key = options.fetch(:app_key)
21
+ @access_token = options.fetch(:access_token)
22
+ end
23
+
24
+ # @return [String]
25
+ def endpoint
26
+ @endpoint ||= DEFAULT_ENDPOINT
27
+ end
28
+
29
+ # @return [String]
30
+ def user_agent
31
+ "Newegg Ruby Gem #{Newegg::VERSION}"
32
+ end
33
+
34
+ # Connection options for faraday
35
+ #
36
+ # @return [Hash]
37
+ def connection_options
38
+ {
39
+ builder: middleware,
40
+ headers: {
41
+ accept: 'application/json',
42
+ user_agent: user_agent,
43
+ Authorization: "#{app_key}&#{access_token}"
44
+ },
45
+ request: {
46
+ open_timeout: 10,
47
+ timeout: 30
48
+ }
49
+ }
50
+ end
51
+
52
+ # @return [Faraday::RackBuilder]
53
+ def middleware
54
+ Faraday::RackBuilder.new do |faraday|
55
+ # Checks for files in the payload, otherwise leaves everything untouched
56
+ faraday.request :multipart
57
+ # Encodes as "application/x-www-form-urlencoded" if not already encoded
58
+ faraday.request :url_encoded
59
+ # Parse JSON response bodies
60
+ faraday.response :parse_json
61
+ # Set default HTTP adapter
62
+ faraday.adapter :net_http
63
+ end
64
+ end
65
+
66
+ # Perform an HTTP GET request
67
+ def get(path, params = {})
68
+ request(:get, path, params)
69
+ end
70
+
71
+ # Perform an HTTP POST request
72
+ def post(path, params = {})
73
+ request(:post, path, params)
74
+ end
75
+
76
+ # Perform an HTTP PUT request
77
+ def put(path, params = {})
78
+ request(:put, path, params)
79
+ end
80
+
81
+ # Perform an HTTP DELETE request
82
+ def delete(path, params = {})
83
+ request(:delete, path, params)
84
+ end
85
+
86
+ private
87
+
88
+ def connection
89
+ @connection ||= Faraday.new(endpoint, connection_options)
90
+ end
91
+
92
+ def request(method, path, params = {}, headers = {})
93
+ connection.send(method.to_sym, path, params) { |request| request.headers.update(headers) }.env
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,63 @@
1
+ module Newegg
2
+ module Orders
3
+ # Fetch order list
4
+ # @param options [Hash]
5
+ # @option options [String] :status Required. 0:待审核 1:待出库 4:已出库 -1:作废 -5:BackOrder
6
+ # @option options [Time,String] :IndateFrom Optional. 下单时间开始
7
+ # @option options [Time,String] :IndateTo Optional. 下单时间结束
8
+ # @option options [Time,String] :endTime Required.
9
+ # @option options [Integer] :pageIndex Optional, defaults to 1. Current page
10
+ # @option options [Integer] :pageSize Optional, Order count per page
11
+ def fetch_orders(options)
12
+ if Time === options[:IndateFrom]
13
+ options[:IndateFrom] = options[:IndateFrom].strftime('%Y-%m-%d %T')
14
+ end
15
+
16
+ if Time === options[:IndateTo]
17
+ options[:IndateTo] = options[:IndateTo].strftime('%Y-%m-%d %T')
18
+ end
19
+
20
+ get('/v1/order', options)
21
+ end
22
+
23
+ # Get order details
24
+ # @param order_number [String] Required.
25
+ #
26
+ # @return [Hash]
27
+ def order_details(order_number)
28
+ get("/v1/order/#{order_number}")
29
+ end
30
+
31
+ # Ship order
32
+ # @param order_number [String] Required.
33
+ # @param options [Hash]
34
+ # @option options [String] :ShippingService SEF --- 商家自送 SF --- 顺风 YTO --- 圆通 STO --- 申通 YUNDA --- 韵达 ZTO --- 中通 TTK --- 天天 HT --- 汇通 EMS --- EMS ZJS --- 宅急送 OTHER --- 其它
35
+ # @option options [String] :PackageNumber Tracking number
36
+ def ship_order(order_number, options)
37
+ params = {
38
+ OperationType: 'UpdateOrder',
39
+ Shipment: {
40
+ ShippingService: options.fetch(:ShippingService),
41
+ PackageNumber: options.fetch(:PackageNumber)
42
+ }
43
+ }
44
+
45
+ post("/v1/order/#{order_number}", params)
46
+ end
47
+
48
+ # Cancel order
49
+ # @param order_number [String] Required.
50
+ # @param options [Hash]
51
+ # @option options [String] :Reason Optional.
52
+ def cancel_order(order_number, options)
53
+ params = {
54
+ OperationType: 'UpdateOrder',
55
+ OrderNumber: order_number,
56
+ Action: 4,
57
+ Reason: options.fetch(:Reason)
58
+ }
59
+
60
+ post("/v1/order/#{order_number}", params)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,30 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module Newegg
5
+ module Response
6
+ class ParseJson < Faraday::Response::Middleware
7
+ WHITESPACE_REGEX = /\A^\s*$\z/
8
+
9
+ def parse(body)
10
+ case body
11
+ when WHITESPACE_REGEX, nil
12
+ nil
13
+ else
14
+ JSON.parse(body, symbolize_names: true)
15
+ end
16
+ end
17
+
18
+ def on_complete(response)
19
+ response.body = parse(response.body) if respond_to?(:parse) && !unparsable_status_codes.include?(response.status)
20
+ end
21
+
22
+ def unparsable_status_codes
23
+ # [204, 301, 302, 304]
24
+ []
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ Faraday::Response.register_middleware parse_json: Newegg::Response::ParseJson
@@ -0,0 +1,3 @@
1
+ module Newegg
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'newegg/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'newegg'
8
+ spec.version = Newegg::VERSION
9
+ spec.authors = ['ryancheung']
10
+ spec.email = ['ryancheung.go@gmail.com']
11
+ spec.summary = 'Newegg Marketplace API wrapper'
12
+ spec.description = ''
13
+ spec.homepage = 'https://github.com/ryancheung/newegg'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(/^(test|spec|features)\//)
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+
24
+ spec.add_dependency 'faraday'
25
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: newegg
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - ryancheung
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-15 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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: faraday
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
+ - ryancheung.go@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rubocop.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/newegg.rb
69
+ - lib/newegg/api.rb
70
+ - lib/newegg/client.rb
71
+ - lib/newegg/orders.rb
72
+ - lib/newegg/response/parse_json.rb
73
+ - lib/newegg/version.rb
74
+ - newegg.gemspec
75
+ homepage: https://github.com/ryancheung/newegg
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.2.2
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Newegg Marketplace API wrapper
99
+ test_files: []
100
+ has_rdoc: