elong 0.2.1

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,45 @@
1
+ module Elong
2
+ module API
3
+ module Hotel
4
+ # Elong Hotel Data API Class
5
+ class Data < Elong::API::Base
6
+
7
+ # The Hotel Data RP API
8
+ # ** Adadvanced API Level **
9
+ #
10
+ # @params [Hash] params api params
11
+ # @return [Elong::Response]
12
+ def rp(params={})
13
+ self.request(__method__, params)
14
+ end
15
+
16
+ # The Hotel Data Inventory API
17
+ # ** Adadvanced API Level **
18
+ #
19
+ # @params [Hash] params api params
20
+ # @return [Elong::Response]
21
+ def inventory(params={})
22
+ self.request(__method__, params)
23
+ end
24
+
25
+ # The Hotel Data Rate API
26
+ # ** Adadvanced API Level **
27
+ #
28
+ # @params [Hash] params api params
29
+ # @return [Elong::Response]
30
+ def rate(params={})
31
+ self.request(__method__, params)
32
+ end
33
+
34
+ # The Hotel Data Validate API
35
+ # ** Adadvanced API Level **
36
+ #
37
+ # @params [Hash] params api params
38
+ # @return [Elong::Response]
39
+ def validate(params={})
40
+ self.request(__method__, params)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,57 @@
1
+ module Elong
2
+ module API
3
+ module Hotel
4
+ # Elong Hotel Incr API Class
5
+ class Incr < Elong::API::Base
6
+
7
+ # The Hotel Incr Id API
8
+ # ** Adadvanced API Level **
9
+ #
10
+ # @params [Hash] params api params
11
+ # @return [Elong::Response]
12
+ def id(params={})
13
+ self.request(__method__, params)
14
+ end
15
+
16
+ # The Hotel Incr Inv API
17
+ # ** Adadvanced API Level **
18
+ #
19
+ # @params [Hash] params api params
20
+ # @return [Elong::Response]
21
+ def inv(params={})
22
+ self.request(__method__, params)
23
+ end
24
+
25
+ # The Hotel Incr Rate API
26
+ # ** Adadvanced API Level **
27
+ #
28
+ # @params [Hash] params api params
29
+ # @return [Elong::Response]
30
+ def rate(params={})
31
+ self.request(__method__, params)
32
+ end
33
+
34
+ # The Hotel Incr Order API
35
+ #
36
+ # `https` request
37
+ #
38
+ # @params [Hash] params api params
39
+ # @return [Elong::Response]
40
+ def order(params={})
41
+ self.request(__method__, params, https=true)
42
+ end
43
+
44
+ # The Hotel Incr Feedback API
45
+ #
46
+ # `https` request
47
+ #
48
+ # @params [Hash] params api params
49
+ # @return [Elong::Response]
50
+ def feedback(params={})
51
+ self.request(__method__, params, https=true)
52
+ end
53
+
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,86 @@
1
+ module Elong
2
+ module API
3
+ module Hotel
4
+ # Elong Hotel Order API Class
5
+ class Order < Elong::API::Base
6
+
7
+ # The Hotel List Order API
8
+ #
9
+ # `https` request
10
+ #
11
+ # @params [Hash] params api params
12
+ # @return [Elong::Response]
13
+ def list(params={})
14
+ self.request(__method__, params, https=true)
15
+ end
16
+
17
+ # The Hotel Order Detail API
18
+ #
19
+ # `https` request
20
+ #
21
+ # @params [Hash] params api params
22
+ # @return [Elong::Response]
23
+ def detail(params={})
24
+ self.request(__method__, params, https=true)
25
+ end
26
+
27
+ # The Hotel Order Instant API
28
+ #
29
+ # `https` request
30
+ #
31
+ # @params [Hash] params api params
32
+ # @return [Elong::Response]
33
+ def instant(params={})
34
+ self.request(__method__, params, https=true)
35
+ end
36
+
37
+ # The Hotel Create Order API
38
+ #
39
+ # `https` request
40
+ #
41
+ # @params [Hash] params api params
42
+ # @return [Elong::Response]
43
+ def create(params={})
44
+ self.request(__method__, params, https=true)
45
+ end
46
+
47
+ # The Hotel Cancel Order API
48
+ #
49
+ # @params [Hash] params api params
50
+ # @return [Elong::Response]
51
+ def cancel(params={})
52
+ self.request(__method__, params)
53
+ end
54
+
55
+ # The Hotel Update Order API
56
+ #
57
+ # @params [Hash] params api params
58
+ # @return [Elong::Response]
59
+ def update(params={})
60
+ self.request(__method__, params)
61
+ end
62
+
63
+ # The Hotel Related Orders API
64
+ #
65
+ # `https` request
66
+ #
67
+ # @params [Hash] params api params
68
+ # @return [Elong::Response]
69
+ def related(params={})
70
+ self.request(__method__, params, https=true)
71
+ end
72
+
73
+ # The Hotel Recent Orders API
74
+ #
75
+ # `https` request
76
+ #
77
+ # @params [Hash] params api params
78
+ # @return [Elong::Response]
79
+ def recent(params={})
80
+ self.request(__method__, params, https=true)
81
+ end
82
+
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,44 @@
1
+ module Elong
2
+ # Elong Client Class
3
+ class Client
4
+ attr_reader :request
5
+
6
+ # Instantiate a new Elong API client using the
7
+ # user, appKey and secretKey registered to your
8
+ # application
9
+ #
10
+ # @param [String] user the user value
11
+ # @param [String] appKey the appKey value
12
+ # @param [String] secretKey the secretKey value
13
+ # @param opts [String] :domain elong api url (default: 'http://api.elong.com/rest')
14
+ # @param opts [String] :version the version of elong api version (default: '1.0')
15
+ # @param opts [String] :local the data return language (ONLY 'en_US' and 'zn_CN', default: 'zh_CN')
16
+ # @param opts [String] :format the data return foramat (ONLY 'json' and 'xml', default: 'json')
17
+ # @return [Elong::Client]
18
+ def initialize(user, appKey, secretKey, opts={})
19
+ @request = Elong::Request.new(user, appKey, secretKey, opts)
20
+ end
21
+
22
+ # The Hotel section
23
+ #
24
+ # @return [Elong::API::Hotel::Core]
25
+ def hotel
26
+ @hotel ||= Elong::API::Hotel::Core.new(self)
27
+ end
28
+
29
+ # The Globel Hotel section
30
+ #
31
+ # @return [Elong::API::GHotel::Core]
32
+ def ghotel
33
+ @ghotel ||= Elong::API::GHotel::Core.new(self)
34
+ end
35
+
36
+ # The Common section
37
+ #
38
+ # @return [Elong::API::Common::Core]
39
+ def common
40
+ @common ||= Elong::API::Common::Core.new(self)
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,94 @@
1
+ require "rest-client"
2
+ require 'multi_json'
3
+ require 'uri'
4
+
5
+
6
+ module Elong
7
+ # Elong Http Request Class
8
+ class Request
9
+ attr_reader :user, :appKey, :secretKey
10
+ attr_accessor :data, :timestamp, :signature, :format, :domain, :version, :local, :https
11
+
12
+ # Initializes a Request instance
13
+ #
14
+ # @param [String] user the user value
15
+ # @param [String] appKey the appKey value
16
+ # @param [String] secretKey the secretKey value
17
+ # @param opts [String] :domain elong api url (default: 'http://api.elong.com/rest')
18
+ # @param opts [String] :version the version of elong api version (default: '1.0')
19
+ # @param opts [String] :local the data return language (ONLY 'en_US' and 'zn_CN', default: 'zh_CN')
20
+ # @param opts [String] :format the data return foramat (ONLY 'json' and 'xml', default: 'json')
21
+ # @return [Elong::Request]
22
+ def initialize(user, appKey, secretKey, opts={})
23
+ @user = user
24
+ @appKey = appKey
25
+ @secretKey = secretKey
26
+
27
+ @https = false
28
+ @domain = opts[:version] ? opts[:version] : 'http://api.elong.com/rest'
29
+ @version = opts[:version] ? opts[:version] : '1.0'
30
+ @local = opts[:local] ? opts[:local] : 'zh_CN'
31
+ @format = opts[:format] ? opts[:format] : 'json'
32
+ end
33
+
34
+ # Create a http request to call api
35
+ #
36
+ # @param [String] api call section api(eg, hotel.list, hotel.detail)
37
+ # @param [Hash] data the data request for api
38
+ # @param [Boolean] https request url if https or http (default: nil)
39
+ # @return [Elong::Response]
40
+ def execute(api, data, https=nil)
41
+ self.generateTimestamp
42
+ self.buildData(data)
43
+ self.generateSignature
44
+
45
+ params = self.buildQueryParams(api)
46
+ uri = URI.parse(@domain)
47
+ https = @https if ![TrueClass, FalseClass].include?(https.class)
48
+ scheme = https ? 'https' : 'http'
49
+ url = "#{scheme}://#{uri.host}/#{uri.path}?#{params}"
50
+
51
+ response = Elong::Response.new(RestClient.get(url))
52
+ end
53
+
54
+ # Build and format query params for url request
55
+ #
56
+ # @return [String]
57
+ def buildQueryParams(api)
58
+ URI.encode_www_form([
59
+ ["method", api],
60
+ ["user", @user],
61
+ ["timestamp", @timestamp],
62
+ ["data", @data],
63
+ ["signature", @signature],
64
+ ["format", @format],
65
+ ]).to_s
66
+ end
67
+
68
+ # Build request params
69
+ #
70
+ # @return [Hash]
71
+ def buildData(params)
72
+ @data = MultiJson.dump({
73
+ 'Version' => @version,
74
+ 'Local' => @local,
75
+ 'Request' => params
76
+ })
77
+ end
78
+
79
+ # Generate a new timestamp
80
+ #
81
+ # @return [String]
82
+ def generateTimestamp
83
+ @timestamp = Time.now.to_i.to_s
84
+ end
85
+
86
+ # Generate a new signature
87
+ #
88
+ # @return [String]
89
+ def generateSignature
90
+ @timestamp ||= self.generateTimestamps
91
+ @signature = Digest::MD5.hexdigest(@timestamp + Digest::MD5.hexdigest(@data + @appKey).downcase + @secretKey).downcase
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,74 @@
1
+ require 'multi_json'
2
+ require 'multi_xml'
3
+
4
+
5
+ module Elong
6
+ # Elong Http Response Class
7
+ class Response
8
+ attr_reader :status, :body, :headers, :cookies, :code, :error, :dataset
9
+
10
+ # Initializes a Request instance
11
+ #
12
+ # @param [RestClient::Response]
13
+ # @return [Elong::Response]
14
+ def initialize(response)
15
+ @status = response.code
16
+ @body = response.to_str
17
+ @headers = response.headers
18
+ @cookies = response.cookies
19
+
20
+ # mark status
21
+ if @status == 200
22
+ if self.json?
23
+ content = self.to_json
24
+ @code = content['Code']
25
+ @dataset = content['Request']
26
+ elsif self.xml?
27
+ content = self.to_xml
28
+ @code = content['ApiResult']['Code']
29
+ @dataset = content['ApiResult']['Result']
30
+ end
31
+
32
+ @code, @error = @code.split('|') if @code and @code.include?'|'
33
+ @code = @code
34
+ end
35
+ end
36
+
37
+ # Whether or not the content is json content type
38
+ #
39
+ # @return [Boolean]
40
+ def json?
41
+ (@headers[:content_type].include?'json') ? true : false
42
+ end
43
+
44
+ # Whether or not the content is xml content type
45
+ #
46
+ # @return [Boolean]
47
+ def xml?
48
+ (['xml', 'html'].any? { |word| @headers[:content_type].include?(word) }) ? true : false
49
+ end
50
+
51
+ # Output response content
52
+ #
53
+ # @return [String]
54
+ def to_s
55
+ @body
56
+ end
57
+
58
+ # Convert response content to JSON format
59
+ #
60
+ # @return [Hash]
61
+ def to_json
62
+ MultiJson.load(@body)
63
+ end
64
+
65
+ # Convert response content to XML format
66
+ #
67
+ # @return [Hash]
68
+ def to_xml(parser=nil)
69
+ MultiXml.parser = p if parser and [:ox, :libxml, :nokogiri, :rexml].include?parser.to_sym
70
+ MultiXml.parse(@body)
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ module Elong
2
+ VERSION = "0.2.1"
3
+ end
@@ -0,0 +1,101 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe Elong::API::Hotel do
5
+ subject { @client.hotel }
6
+
7
+ describe 'GET #detail' do
8
+ it '可以获取正确的房源信息' do
9
+ @client.request.format = 'json'
10
+ res = subject.detail(HotelIds:@hotelId,
11
+ ArrivalDate:@tomorrow,
12
+ DepartureDate:@theDayAfterTomorrow)
13
+
14
+ res.status.should eq(200)
15
+ res.code.should eq('0')
16
+ res.error.should be_nil
17
+
18
+ res.should be_json
19
+ #res.to_json['Result']['']
20
+ end
21
+
22
+ it '允许获取的结果为空' do
23
+ @client.request.format = 'json'
24
+ res = subject.detail(HotelIds:'999999999999999999',
25
+ ArrivalDate:@tomorrow,
26
+ DepartureDate:@theDayAfterTomorrow)
27
+
28
+ res.status.should eq(200)
29
+ res.code.should eq('0')
30
+ res.error.should be_nil
31
+
32
+ res.should be_json
33
+ res.to_json['Result']['Count'].should eq(0)
34
+ end
35
+
36
+ it '允许获取不存在的房源信息' do
37
+ @client.request.format = 'json'
38
+ res = subject.detail(HotelIds:'ddddd',
39
+ ArrivalDate:@tomorrow,
40
+ DepartureDate:@theDayAfterTomorrow)
41
+
42
+ res.status.should eq(200)
43
+ res.code.should eq('H000008')
44
+ res.error.should match('Ids不符合数字逗号格式')
45
+ end
46
+
47
+ it '必须保证时间格式正确无误' do
48
+ @client.request.format = 'json'
49
+ res = subject.detail(HotelIds:'000000',
50
+ ArrivalDate:'13-10-1',
51
+ DepartureDate:'13/10/2')
52
+
53
+ res.status.should eq(200)
54
+ res.code.should eq('H000998')
55
+ res.error.should match('请求参数异常')
56
+ end
57
+ end
58
+
59
+ describe 'GET #list' do
60
+ it '可以设置返回的房源数量' do
61
+ @client.request.format = 'json'
62
+ pageSize = 1
63
+ res = subject.list(CityId: @cityId,
64
+ ArrivalDate: @tomorrow,
65
+ DepartureDate: @theDayAfterTomorrow,
66
+ PageSize: pageSize)
67
+
68
+
69
+ res.status.should eq(200)
70
+ res.code.should eq('0')
71
+ res.error.should be_nil
72
+
73
+ res.should be_json
74
+
75
+ #@todo: 貌似是bug,返回总是 20 个
76
+ res.to_json['Result']['Hotels'].size.should eq(pageSize)
77
+ end
78
+
79
+ it '获取不存在的房源信息' do
80
+ pending
81
+ end
82
+ end
83
+
84
+ describe '#order' do
85
+ describe 'GET order.list' do
86
+ it '获取正常数据' do
87
+ @client.request.format = 'json'
88
+ res = subject.order.list(PageIndex: 1)
89
+
90
+ res.status.should eq(200)
91
+ res.code.should eq('0')
92
+ res.error.should be_nil
93
+
94
+ res.should be_json
95
+
96
+ #@todo: 貌似是bug,返回总是 20 个
97
+ res.to_json['Result']['Hotels'].size.should eq(pageSize)
98
+ end
99
+ end
100
+ end
101
+ end