jingdong_fu 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.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 NIO TEAM
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.
data/README.markdown ADDED
@@ -0,0 +1,3 @@
1
+ USAGE:
2
+
3
+ JingdongFu.get method: '360buy.order.search', param: {order_state:'WAIT_SELLER_STOCK_OUT', page:1, page_size:10}
@@ -0,0 +1,3 @@
1
+ To copy a JingdongFu initializer to your Rails App, with some configuration values, just do:
2
+
3
+ rails generate jingdong_fu:install
@@ -0,0 +1,13 @@
1
+ module JingdongFu
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ desc "Copy JingdongFu default files"
5
+ source_root File.expand_path('../templates', __FILE__)
6
+ class_option :template_engine
7
+
8
+ def copy_config
9
+ directory 'config'
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ require 'jingdong_fu'
2
+
3
+ require 'yaml'
4
+ YAML::ENGINE.yamler = 'syck'
5
+
6
+ config_file = File.join(Rails.root, "config", "jingdong.yml")
7
+ JingdongFu.load(config_file) if FileTest::exists?(config_file)
@@ -0,0 +1,19 @@
1
+ defaults: &defaults
2
+ app_key: # YOUR_APP_KEY
3
+ secret_key: # YOUR_APP_SECRET_TOKEN
4
+ access_token:
5
+
6
+ development:
7
+ <<: *defaults
8
+ is_sandbox: false # If true, it will work under the sandbox environment(tbsandbox.com, not taobao.com).
9
+ use_curl: false # If true, it will use gem "patron" as the REST client.
10
+
11
+ test:
12
+ <<: *defaults
13
+ is_sandbox: ture
14
+ use_curl: false
15
+
16
+ production:
17
+ <<: *defaults
18
+ is_sandbox: false
19
+ use_curl: true
@@ -0,0 +1,106 @@
1
+ begin
2
+ require "crack"
3
+ rescue LoadError
4
+ puts "The Crack gem is not available.\nIf you ran this command from a git checkout " \
5
+ "of Rails, please make sure crack is installed. \n "
6
+ exit
7
+ end
8
+ # require "patron"
9
+ require "digest/md5"
10
+ require "yaml"
11
+ require "uri"
12
+ require "rest"
13
+
14
+ module JingdongFu
15
+
16
+ SANDBOX = 'http://gw.api.sandbox.360buy.com/routerjson?'
17
+ PRODBOX = 'http://gw.api.360buy.com/routerjson?'
18
+ USER_AGENT = 'jingdong_fu/1.0.0.alpha'
19
+ REQUEST_TIMEOUT = 10
20
+ API_VERSION = 2.0
21
+ SIGN_ALGORITHM = 'md5'
22
+ OUTPUT_FORMAT = 'json'
23
+
24
+ class << self
25
+ def load(config_file)
26
+ @settings = YAML.load_file(config_file)
27
+ @settings = @settings[Rails.env] if defined? Rails.env
28
+ apply_settings
29
+ end
30
+
31
+ def apply_settings
32
+ ENV['TAOBAO_API_KEY'] = @settings['app_key'].to_s
33
+ ENV['TAOBAO_SECRET_KEY'] = @settings['secret_key']
34
+ ENV['TAOBAOKE_PID'] = @settings['taobaoke_pid']
35
+ @base_url = @settings['is_sandbox'] ? SANDBOX : PRODBOX
36
+
37
+ initialize_session if @settings['use_curl']
38
+ end
39
+
40
+ def initialize_session
41
+ @sess = Patron::Session.new
42
+ @sess.base_url = @base_url
43
+ @sess.headers['User-Agent'] = USER_AGENT
44
+ @sess.timeout = REQUEST_TIMEOUT
45
+ end
46
+
47
+ def switch_to(sandbox_or_prodbox)
48
+ @base_url = sandbox_or_prodbox
49
+ @sess.base_url = @base_url if @sess
50
+ end
51
+
52
+ def get(options = {})
53
+ if @sess
54
+ @response = @sess.get(generate_query_string(sorted_params(options))).body
55
+ else
56
+ @response = TaobaoFu::Rest.get(@base_url, generate_query_vars(sorted_params(options)))
57
+ end
58
+ parse_result @response
59
+ end
60
+
61
+ # http://toland.github.com/patron/
62
+ def post(options = {})
63
+ end
64
+
65
+ def update(options = {})
66
+ end
67
+
68
+ def delete(options = {})
69
+ end
70
+
71
+ def sorted_params(options)
72
+ param = options.delete(:param)
73
+ options['360buy_param_json'] = param.to_json
74
+ {
75
+ :app_key => @settings['app_key'],
76
+ :access_token => @settings['access_token'],
77
+ :format => OUTPUT_FORMAT,
78
+ :v => API_VERSION,
79
+ :sign_method => SIGN_ALGORITHM,
80
+ :timestamp => Time.now.strftime("%Y-%m-%d %H:%M:%S")
81
+ }.merge!(options)
82
+ end
83
+
84
+ def generate_query_vars(params)
85
+ params[:sign] = generate_sign(params.sort_by { |k,v| k.to_s }.flatten.join)
86
+ params
87
+ end
88
+
89
+ def generate_query_string(params)
90
+ sign_token = generate_sign(params_array.flatten.join)
91
+ total_param = params_array.map { |key, value| key.to_s+"="+value.to_s } + ["sign=#{sign_token}"]
92
+ URI.escape(total_param.join("&"))
93
+ end
94
+
95
+ def generate_sign(param_string)
96
+ puts param_string
97
+ Digest::MD5.hexdigest(@settings['secret_key'] + param_string + @settings['secret_key']).upcase
98
+ end
99
+
100
+ def parse_result(data)
101
+ Crack::JSON.parse(data)
102
+ end
103
+
104
+ end
105
+
106
+ end
data/lib/rest.rb ADDED
@@ -0,0 +1,95 @@
1
+ require "uri"
2
+ require "net/http"
3
+
4
+ module JingdongFu
5
+ module Rest
6
+ class << self
7
+ def get(url, hashed_vars)
8
+ res = request(url, 'GET', hashed_vars)
9
+ process_result(res, url)
10
+ end
11
+
12
+ def post(url, hashed_vars)
13
+ res = request(url, 'POST', hashed_vars)
14
+ process_result(res, url)
15
+ end
16
+
17
+ def put(url, hashed_vars)
18
+ res = request(url, 'PUT', hashed_vars)
19
+ process_result(res, url)
20
+ end
21
+
22
+ def delete(url, hashed_vars)
23
+ res = request(url, 'DELETE', hashed_vars)
24
+ process_result(res, url)
25
+ end
26
+
27
+ protected
28
+
29
+ def request(url, method=nil, params = {})
30
+ if !url || url.length < 1
31
+ raise ArgumentError, 'Invalid url parameter'
32
+ end
33
+ if method && !['GET', 'POST', 'DELETE', 'PUT'].include?(method)
34
+ raise NotImplementedError, 'HTTP %s not implemented' % method
35
+ end
36
+
37
+ if method && method == 'GET'
38
+ url = build_get_uri(url, params)
39
+ end
40
+ uri = URI.parse(url)
41
+
42
+ http = Net::HTTP.new(uri.host, uri.port)
43
+
44
+ if method && method == 'GET'
45
+ req = Net::HTTP::Get.new(uri.request_uri)
46
+ elsif method && method == 'DELETE'
47
+ req = Net::HTTP::Delete.new(uri.request_uri)
48
+ elsif method && method == 'PUT'
49
+ req = Net::HTTP::Put.new(uri.request_uri)
50
+ req.set_form_data(params)
51
+ else
52
+ req = Net::HTTP::Post.new(uri.request_uri)
53
+ req.set_form_data(params)
54
+ end
55
+
56
+ http.request(req)
57
+ end
58
+
59
+ def build_get_uri(uri, params)
60
+ if params && params.length > 0
61
+ uri += '?' unless uri.include?('?')
62
+ uri += urlencode(params)
63
+ end
64
+ URI.escape(uri)
65
+ end
66
+
67
+ def urlencode(params)
68
+ params.to_a.collect! { |k, v| "#{k.to_s}=#{v.to_s}" }.join("&")
69
+ end
70
+
71
+ def process_result(res, raw_url)
72
+ if res.code =~ /\A2\d{2}\z/
73
+ res.body
74
+ elsif %w(301 302 303).include? res.code
75
+ url = res.header['Location']
76
+ if url !~ /^http/
77
+ uri = URI.parse(raw_url)
78
+ uri.path = "/#{url}".squeeze('/')
79
+ url = uri.to_s
80
+ end
81
+ raise RuntimeError, "Redirect #{url}"
82
+ elsif res.code == "304"
83
+ raise RuntimeError, "NotModified #{res}"
84
+ elsif res.code == "401"
85
+ raise RuntimeError, "Unauthorized #{res}"
86
+ elsif res.code == "404"
87
+ raise RuntimeError, "Resource not found #{res}"
88
+ else
89
+ raise RuntimeError, "Maybe request timed out #{res}. HTTP status code #{res.code}"
90
+ end
91
+ end
92
+
93
+ end
94
+ end
95
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jingdong_fu
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - nioteam
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: crack
16
+ requirement: &70304697158500 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.1.7
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70304697158500
25
+ description: Ruby client for JOS platform.
26
+ email: info@networking.io
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files: []
30
+ files:
31
+ - MIT-LICENSE
32
+ - README.markdown
33
+ - lib/generators/jingdong_fu/install_generator.rb
34
+ - lib/generators/jingdong_fu/templates/config/initializers/jindong_fu.rb
35
+ - lib/generators/jingdong_fu/templates/config/jingdong.yml
36
+ - lib/generators/jingdong_fu/USAGE
37
+ - lib/jingdong_fu.rb
38
+ - lib/rest.rb
39
+ homepage: http://www.networking.io
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 1.8.15
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Ruby client for JOS platform.
63
+ test_files: []