dianping 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +7 -2
- data/Gemfile +1 -0
- data/Guardfile +24 -0
- data/README.md +1 -0
- data/config/dianping.yml +2 -0
- data/dianping.gemspec +7 -1
- data/lib/dianping.rb +1 -0
- data/lib/dianping/api/auth.rb +18 -0
- data/lib/dianping/api/business.rb +17 -0
- data/lib/dianping/api/utils.rb +44 -0
- data/lib/dianping/client.rb +59 -0
- data/lib/dianping/error.rb +29 -0
- data/lib/dianping/error/client_error.rb +35 -0
- data/lib/dianping/error/not_found.rb +10 -0
- data/lib/dianping/version.rb +1 -1
- data/test/dianping/test_dianping.rb +1 -1
- data/test/dianping/test_find_businesses.rb +23 -0
- metadata +63 -4
data/CHANGELOG.rdoc
CHANGED
data/Gemfile
CHANGED
data/Guardfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'minitest' do
|
5
|
+
# with Minitest::Unit
|
6
|
+
watch(%r|^test/(.*)\/?test_(.*)\.rb|)
|
7
|
+
watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
|
8
|
+
watch(%r|^test/test_helper\.rb|) { "test" }
|
9
|
+
|
10
|
+
# with Minitest::Spec
|
11
|
+
# watch(%r|^spec/(.*)_spec\.rb|)
|
12
|
+
# watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
|
13
|
+
# watch(%r|^spec/spec_helper\.rb|) { "spec" }
|
14
|
+
|
15
|
+
# Rails 3.2
|
16
|
+
# watch(%r|^app/controllers/(.*)\.rb|) { |m| "test/controllers/#{m[1]}_test.rb" }
|
17
|
+
# watch(%r|^app/helpers/(.*)\.rb|) { |m| "test/helpers/#{m[1]}_test.rb" }
|
18
|
+
# watch(%r|^app/models/(.*)\.rb|) { |m| "test/unit/#{m[1]}_test.rb" }
|
19
|
+
|
20
|
+
# Rails
|
21
|
+
# watch(%r|^app/controllers/(.*)\.rb|) { |m| "test/functional/#{m[1]}_test.rb" }
|
22
|
+
# watch(%r|^app/helpers/(.*)\.rb|) { |m| "test/helpers/#{m[1]}_test.rb" }
|
23
|
+
# watch(%r|^app/models/(.*)\.rb|) { |m| "test/unit/#{m[1]}_test.rb" }
|
24
|
+
end
|
data/README.md
CHANGED
data/config/dianping.yml
ADDED
data/dianping.gemspec
CHANGED
@@ -10,10 +10,16 @@ Gem::Specification.new do |gem|
|
|
10
10
|
gem.email = ["as181920@hotmail.com"]
|
11
11
|
gem.description = %q{dianping.com api wrapper}
|
12
12
|
gem.summary = %q{dianping.com api wrapper}
|
13
|
-
gem.homepage = ""
|
13
|
+
gem.homepage = "https://github.com/as181920/dianping"
|
14
14
|
|
15
15
|
gem.files = `git ls-files`.split($/)
|
16
16
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
18
|
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency "httparty"
|
21
|
+
|
22
|
+
gem.add_development_dependency "guard-minitest"
|
23
|
+
gem.add_development_dependency "rb-inotify"
|
24
|
+
|
19
25
|
end
|
data/lib/dianping.rb
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "digest/md5"
|
4
|
+
|
5
|
+
module DianPing
|
6
|
+
module API
|
7
|
+
module Auth
|
8
|
+
|
9
|
+
def generate_signature(params,app_key,app_secret)
|
10
|
+
params_str = params.keys.sort.map{|key| [key,params[key]] }.flatten.join
|
11
|
+
sign = app_key+params_str+app_secret
|
12
|
+
Digest::SHA1.hexdigest(sign).upcase
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "dianping/api/utils"
|
4
|
+
|
5
|
+
module DianPing
|
6
|
+
module API
|
7
|
+
module Business
|
8
|
+
include DianPing::API::Utils
|
9
|
+
|
10
|
+
def find_businesses(options={})
|
11
|
+
res = send :get, "/v1/business/find_businesses", options
|
12
|
+
res["businesses"]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "yaml"
|
4
|
+
require "httparty"
|
5
|
+
require "dianping/api/auth"
|
6
|
+
|
7
|
+
module DianPing
|
8
|
+
module API
|
9
|
+
module Utils
|
10
|
+
include DianPing::API::Auth
|
11
|
+
|
12
|
+
def send(request_method, path, options={})
|
13
|
+
sign = generate_signature(options,app_key,app_secret)
|
14
|
+
query = URI.encode_www_form options
|
15
|
+
response = ::HTTParty.send request_method, "http://api.dianping.com#{path}?appkey=#{app_key}&sign=#{sign}&#{query}"
|
16
|
+
response.parsed_response
|
17
|
+
end
|
18
|
+
|
19
|
+
=begin
|
20
|
+
def objects_from_response(klass, request_method, path, options={})
|
21
|
+
response = send(request_method.to_sym, path, options)[:body]
|
22
|
+
objects_from_array(klass, response)
|
23
|
+
end
|
24
|
+
=end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def app_key
|
29
|
+
@app_key ||= config["app_key"]
|
30
|
+
end
|
31
|
+
|
32
|
+
def app_secret
|
33
|
+
@app_secret ||= config["app_secret"]
|
34
|
+
end
|
35
|
+
|
36
|
+
def config
|
37
|
+
YAML.load_file(File.join(File.dirname(__FILE__),"../../../config/dianping.yml"))
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require "dianping/api/business"
|
2
|
+
|
3
|
+
module DianPing
|
4
|
+
class Client
|
5
|
+
include DianPing::API::Business
|
6
|
+
|
7
|
+
# Initializes a new Client object
|
8
|
+
#
|
9
|
+
# @param options [Hash]
|
10
|
+
# @return [DianPing::Client]
|
11
|
+
def initialize(options={})
|
12
|
+
end
|
13
|
+
|
14
|
+
# Perform an HTTP DELETE request
|
15
|
+
def delete(path, params={})
|
16
|
+
request(:delete, path, params)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Perform an HTTP GET request
|
20
|
+
def get(path, params={})
|
21
|
+
request(:get, path, params)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Perform an HTTP POST request
|
25
|
+
def post(path, params={})
|
26
|
+
signature_params = params.values.any?{|value| value.respond_to?(:to_io)} ? {} : params
|
27
|
+
request(:post, path, params, signature_params)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Perform an HTTP PUT request
|
31
|
+
def put(path, params={})
|
32
|
+
request(:put, path, params)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def request(method, path, params={}, signature_params=params)
|
38
|
+
Httparty.send method,
|
39
|
+
connection.send(method.to_sym, path, params) do |request|
|
40
|
+
request.headers[:authorization] = auth_header(method.to_sym, path, signature_params).to_s
|
41
|
+
end.env
|
42
|
+
#rescue Faraday::Error::ClientError
|
43
|
+
# raise Twitter::Error::ClientError
|
44
|
+
#rescue MultiJson::DecodeError
|
45
|
+
# raise Twitter::Error::DecodeError
|
46
|
+
#rescue
|
47
|
+
# raise DianPing::Error::RequestError
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
=begin
|
55
|
+
puts sign
|
56
|
+
|
57
|
+
response = HTTParty.get "http://api.dianping.com/v1/business/find_businesses?city=%E4%B8%8A%E6%B5%B7&appkey=#{key}&sign=#{sign}"
|
58
|
+
p response
|
59
|
+
=end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module DianPing
|
2
|
+
# Custom error class for rescuing from all DianPing errors
|
3
|
+
class Error < StandardError
|
4
|
+
|
5
|
+
# @return [Hash]
|
6
|
+
def self.errors
|
7
|
+
@errors ||= Hash[descendants.map{|klass| [klass.const_get(:HTTP_STATUS_CODE)]}]
|
8
|
+
end
|
9
|
+
|
10
|
+
# @return [Array]
|
11
|
+
def self.descendants
|
12
|
+
ObjectSpace.each_object(::Class).select{|klass| klass < self}
|
13
|
+
end
|
14
|
+
|
15
|
+
# Initializes a new Error object
|
16
|
+
#
|
17
|
+
# @param exception [Exception, String]
|
18
|
+
# @param response_headers [Hash]
|
19
|
+
# @return [DianPing::Error]
|
20
|
+
def initialize(exception=$!, response_headers={})
|
21
|
+
exception.respond_to?(:backtrace) ? super(exception.message) : super(exception.to_s)
|
22
|
+
end
|
23
|
+
|
24
|
+
def backtrace
|
25
|
+
@wrapped_exception.respond_to?(:backtrace) ? @wrapped_exception.backtrace : super
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "dianping/error"
|
2
|
+
|
3
|
+
module DianPing
|
4
|
+
class Error
|
5
|
+
# Raise when DianPing returns a 4xx HTTP status code or there's an error in Httparty
|
6
|
+
class ClientError < DianPing::Error
|
7
|
+
# Create a new error from an HTTP environment
|
8
|
+
#
|
9
|
+
# @param response [Hash]
|
10
|
+
# @return [DianPing::Error]
|
11
|
+
def self.from_response(response={})
|
12
|
+
new(parse_error(response[:body]), response[:response_headers])
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def self.parse_error(body)
|
18
|
+
if body.nil?
|
19
|
+
''
|
20
|
+
elsif body[:error]
|
21
|
+
body[:error]
|
22
|
+
elsif body[errors]
|
23
|
+
first = Array(body[errors]).first
|
24
|
+
if first.kind_of?(Hash)
|
25
|
+
first[:message].chomp
|
26
|
+
else
|
27
|
+
first.chomp
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
data/lib/dianping/version.rb
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'dianping'
|
4
|
+
|
5
|
+
describe DianPing do
|
6
|
+
before do
|
7
|
+
@client = DianPing::Client.new
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "find by city" do
|
11
|
+
it "can find some businesses in shanghai" do
|
12
|
+
businesses = @client.find_businesses({city: '上海'})
|
13
|
+
businesses.must_be_kind_of Array
|
14
|
+
businesses.wont_be_empty
|
15
|
+
end
|
16
|
+
|
17
|
+
it "can find businesses in shanghai with limited count" do
|
18
|
+
businesses = @client.find_businesses({city: '上海', limit: 10})
|
19
|
+
businesses.must_be_kind_of Array
|
20
|
+
businesses.length.must_equal 10
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dianping
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,56 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
13
|
-
dependencies:
|
12
|
+
date: 2013-02-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: httparty
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: guard-minitest
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rb-inotify
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
14
62
|
description: dianping.com api wrapper
|
15
63
|
email:
|
16
64
|
- as181920@hotmail.com
|
@@ -21,14 +69,24 @@ files:
|
|
21
69
|
- .gitignore
|
22
70
|
- CHANGELOG.rdoc
|
23
71
|
- Gemfile
|
72
|
+
- Guardfile
|
24
73
|
- LICENSE.txt
|
25
74
|
- README.md
|
26
75
|
- Rakefile
|
76
|
+
- config/dianping.yml
|
27
77
|
- dianping.gemspec
|
28
78
|
- lib/dianping.rb
|
79
|
+
- lib/dianping/api/auth.rb
|
80
|
+
- lib/dianping/api/business.rb
|
81
|
+
- lib/dianping/api/utils.rb
|
82
|
+
- lib/dianping/client.rb
|
83
|
+
- lib/dianping/error.rb
|
84
|
+
- lib/dianping/error/client_error.rb
|
85
|
+
- lib/dianping/error/not_found.rb
|
29
86
|
- lib/dianping/version.rb
|
30
87
|
- test/dianping/test_dianping.rb
|
31
|
-
|
88
|
+
- test/dianping/test_find_businesses.rb
|
89
|
+
homepage: https://github.com/as181920/dianping
|
32
90
|
licenses: []
|
33
91
|
post_install_message:
|
34
92
|
rdoc_options: []
|
@@ -54,3 +112,4 @@ specification_version: 3
|
|
54
112
|
summary: dianping.com api wrapper
|
55
113
|
test_files:
|
56
114
|
- test/dianping/test_dianping.rb
|
115
|
+
- test/dianping/test_find_businesses.rb
|