Class: Elong::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/elong/request.rb

Overview

Elong Http Request Class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, appKey, secretKey, opts = {}) ⇒ Elong::Request

Initializes a Request instance

Parameters:

  • user (String)

    the user value

  • appKey (String)

    the appKey value

  • secretKey (String)

    the secretKey value

  • opts (String) (defaults to: {})

    :domain elong api url (default: ‘api.elong.com/rest’)

  • opts (String) (defaults to: {})

    :version the version of elong api version (default: ‘1.0’)

  • opts (String) (defaults to: {})

    :local the data return language (ONLY ‘en_US’ and ‘zn_CN’, default: ‘zh_CN’)

  • opts (String) (defaults to: {})

    :format the data return foramat (ONLY ‘json’ and ‘xml’, default: ‘json’)



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/elong/request.rb', line 22

def initialize(user, appKey, secretKey, opts={})
  @user = user
  @appKey = appKey
  @secretKey = secretKey

  @https = false
  @domain = opts[:version] ? opts[:version] : 'http://api.elong.com/rest'
  @version = opts[:version] ? opts[:version] : '1.0'
  @local = opts[:local] ? opts[:local] : 'zh_CN'
  @format = opts[:format] ? opts[:format] : 'json'
end

Instance Attribute Details

#appKeyObject (readonly)

Returns the value of attribute appKey.



9
10
11
# File 'lib/elong/request.rb', line 9

def appKey
  @appKey
end

#dataObject

Returns the value of attribute data.



10
11
12
# File 'lib/elong/request.rb', line 10

def data
  @data
end

#domainObject

Returns the value of attribute domain.



10
11
12
# File 'lib/elong/request.rb', line 10

def domain
  @domain
end

#formatObject

Returns the value of attribute format.



10
11
12
# File 'lib/elong/request.rb', line 10

def format
  @format
end

#httpsObject

Returns the value of attribute https.



10
11
12
# File 'lib/elong/request.rb', line 10

def https
  @https
end

#localObject

Returns the value of attribute local.



10
11
12
# File 'lib/elong/request.rb', line 10

def local
  @local
end

#secretKeyObject (readonly)

Returns the value of attribute secretKey.



9
10
11
# File 'lib/elong/request.rb', line 9

def secretKey
  @secretKey
end

#signatureObject

Returns the value of attribute signature.



10
11
12
# File 'lib/elong/request.rb', line 10

def signature
  @signature
end

#timestampObject

Returns the value of attribute timestamp.



10
11
12
# File 'lib/elong/request.rb', line 10

def timestamp
  @timestamp
end

#userObject (readonly)

Returns the value of attribute user.



9
10
11
# File 'lib/elong/request.rb', line 9

def user
  @user
end

#versionObject

Returns the value of attribute version.



10
11
12
# File 'lib/elong/request.rb', line 10

def version
  @version
end

Instance Method Details

#buildData(params) ⇒ Hash

Build request params

Returns:

  • (Hash)


71
72
73
74
75
76
77
# File 'lib/elong/request.rb', line 71

def buildData(params)
  @data = MultiJson.dump({
    'Version' => @version,
    'Local'   => @local,
    'Request' => params
  })
end

#buildQueryParams(api) ⇒ String

Build and format query params for url request

Returns:

  • (String)


57
58
59
60
61
62
63
64
65
66
# File 'lib/elong/request.rb', line 57

def buildQueryParams(api)
  URI.encode_www_form([
    ["method", api],
    ["user", @user],
    ["timestamp", @timestamp],
    ["data", @data],
    ["signature", @signature],
    ["format", @format],
 ]).to_s
end

#execute(api, data, https = nil) ⇒ Elong::Response

Create a http request to call api

Parameters:

  • api (String)

    call section api(eg, hotel.list, hotel.detail)

  • data (Hash)

    the data request for api

  • https (Boolean) (defaults to: nil)

    request url if https or http (default: nil)

Returns:



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/elong/request.rb', line 40

def execute(api, data, https=nil)
  self.generateTimestamp
  self.buildData(data)
  self.generateSignature

  params = self.buildQueryParams(api)
  uri = URI.parse(@domain)
  https = @https if ![TrueClass, FalseClass].include?(https.class)
  scheme = https ? 'https' : 'http'
  url = "#{scheme}://#{uri.host}/#{uri.path}?#{params}"

  response = Elong::Response.new(RestClient.get(url))
end

#generateSignatureString

Generate a new signature

Returns:

  • (String)


89
90
91
92
# File 'lib/elong/request.rb', line 89

def generateSignature
  @timestamp ||= self.generateTimestamps
  @signature = Digest::MD5.hexdigest(@timestamp + Digest::MD5.hexdigest(@data + @appKey).downcase + @secretKey).downcase
end

#generateTimestampString

Generate a new timestamp

Returns:

  • (String)


82
83
84
# File 'lib/elong/request.rb', line 82

def generateTimestamp
  @timestamp = Time.now.to_i.to_s
end