Class: Elong::Request
- Inherits:
-
Object
- Object
- Elong::Request
- Defined in:
- lib/elong/request.rb
Overview
Elong Http Request Class
Instance Attribute Summary collapse
-
#appKey ⇒ Object
readonly
Returns the value of attribute appKey.
-
#data ⇒ Object
Returns the value of attribute data.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#format ⇒ Object
Returns the value of attribute format.
-
#https ⇒ Object
Returns the value of attribute https.
-
#local ⇒ Object
Returns the value of attribute local.
-
#secretKey ⇒ Object
readonly
Returns the value of attribute secretKey.
-
#signature ⇒ Object
Returns the value of attribute signature.
-
#timestamp ⇒ Object
Returns the value of attribute timestamp.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
-
#buildData(params) ⇒ Hash
Build request params.
-
#buildQueryParams(api) ⇒ String
Build and format query params for url request.
-
#execute(api, data, https = nil) ⇒ Elong::Response
Create a http request to call api.
-
#generateSignature ⇒ String
Generate a new signature.
-
#generateTimestamp ⇒ String
Generate a new timestamp.
-
#initialize(user, appKey, secretKey, opts = {}) ⇒ Elong::Request
constructor
Initializes a Request instance.
Constructor Details
#initialize(user, appKey, secretKey, opts = {}) ⇒ Elong::Request
Initializes a Request instance
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
#appKey ⇒ Object (readonly)
Returns the value of attribute appKey.
9 10 11 |
# File 'lib/elong/request.rb', line 9 def appKey @appKey end |
#data ⇒ Object
Returns the value of attribute data.
10 11 12 |
# File 'lib/elong/request.rb', line 10 def data @data end |
#domain ⇒ Object
Returns the value of attribute domain.
10 11 12 |
# File 'lib/elong/request.rb', line 10 def domain @domain end |
#format ⇒ Object
Returns the value of attribute format.
10 11 12 |
# File 'lib/elong/request.rb', line 10 def format @format end |
#https ⇒ Object
Returns the value of attribute https.
10 11 12 |
# File 'lib/elong/request.rb', line 10 def https @https end |
#local ⇒ Object
Returns the value of attribute local.
10 11 12 |
# File 'lib/elong/request.rb', line 10 def local @local end |
#secretKey ⇒ Object (readonly)
Returns the value of attribute secretKey.
9 10 11 |
# File 'lib/elong/request.rb', line 9 def secretKey @secretKey end |
#signature ⇒ Object
Returns the value of attribute signature.
10 11 12 |
# File 'lib/elong/request.rb', line 10 def signature @signature end |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
10 11 12 |
# File 'lib/elong/request.rb', line 10 def @timestamp end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
9 10 11 |
# File 'lib/elong/request.rb', line 9 def user @user end |
#version ⇒ Object
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
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
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
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 |
#generateSignature ⇒ String
Generate a new signature
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 |
#generateTimestamp ⇒ String
Generate a new timestamp
82 83 84 |
# File 'lib/elong/request.rb', line 82 def generateTimestamp @timestamp = Time.now.to_i.to_s end |