Class: Top4R::Client

Inherits:
Object
  • Object
show all
Includes:
ClassUtilMixin
Defined in:
lib/top4r/config.rb,
lib/top4r/client.rb,
lib/top4r/console.rb,
lib/top4r/client/base.rb,
lib/top4r/client/user.rb,
lib/top4r/client/suite.rb,
lib/top4r/client/trade.rb,
lib/top4r/client/shipping.rb

Overview

Config class

Constant Summary collapse

@@no_login_required_methods =
{
  :user => {
    :info => 'taobao.user.get',
    :multi_info => 'taobao.users.get'
  },
  :trade => {},
  :area => {
    :list => 'taobao.areas.get'
  },
  :logistic_company => {
    :list => 'taobao.logisticcompanies.get'
  }
}
@@defaults =
{
  :env => :test,
  :host => 'gw.api.taobao.com',
  :rest_uri => '/router/rest',
  :port => 80,
  :protocol => :http,
  :test_host => 'gw.sandbox.taobao.com',
  :test_rest_uri => '/router/rest',
  :test_port => 80,
  :test_protocol => :http,
  # :staging_host => '192.168.208.110',
  #       :staging_rest_uri => '/top/private/services/rest',
  #       :staging_port => '8080',
  #       :staging_protocol => :http,
  :proxy_host => nil,
  :proxy_port => nil,
  :format => :json,
  :application_name => 'Top4R',
  :application_key => '12000224',
  :application_secret => '2f26cb1a99570aa72daee12a1db88e63',
  :application_version => Top4R::Version.to_version,
  :application_url => 'http://top4r.nowa.me',
  :user_agent => 'default',
  :source => 'top4r',
  :logger => nil,
  :trace => false
}
@@config =
Top4R::Config.new(@@defaults)
@@logger =
Top4R::Logger.new(@@config.logger)
@@USER_METHODS =
{
  :info => 'taobao.user.get',
  :multi_info => 'taobao.users.get'
}
@@SUITE_METHODS =
{
  :list => 'taobao.suites.get',
}
@@TRADE_METHODS =
{
  :bought_list => 'taobao.trades.bought.get',
  :sold_list => 'taobao.trades.sold.get',
  :increments_list => 'taobao.trades.sold.increment.get',
  :info => 'taobao.trade.get',
  :fullinfo => 'taobao.trade.fullinfo.get',
  :close => 'taobao.trade.close',
  :add_memo => 'taobao.trade.memo.add',
  :update_memo => 'taobao.trade.memo.update',
  :confirmfee => 'taobao.trade.confirmfee.get'
}
@@AREA_METHODS =
{
  :list => 'taobao.areas.get'
}
@@LOGISTIC_COMPANY_METHODS =
{
  :list => 'taobao.logisticcompanies.get'
}
@@DELIVERY_METHODS =
{
  :send => 'taobao.delivery.send'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassUtilMixin

included

Instance Attribute Details

#loginObject (readonly)

Returns the value of attribute login.



4
5
6
# File 'lib/top4r/client/base.rb', line 4

def 
  @login
end

#total_resultsObject

Returns the value of attribute total_results.



3
4
5
# File 'lib/top4r/client/base.rb', line 3

def total_results
  @total_results
end

Class Method Details

.configure {|@@config| ... } ⇒ Object

Yields to given block to configure the Twitter4R API.

Yields:

  • (@@config)

Raises:

  • (ArgumentError)


81
82
83
84
# File 'lib/top4r/config.rb', line 81

def configure(&block)
  raise ArgumentError, "Block must be provided to configure" unless block_given?
  yield @@config
end

.from_config(config_file, env = 'test') ⇒ Object

Helper method mostly for irb shell prototyping.

Reads in app_key/app_secret from YAML file found at the location given by config_file that has the following format: envname: app_key: application key app_secret: application secret

Where envname is the name of the environment like ‘test’, ‘dev’ or ‘prod’. The env argument defaults to ‘test’.

To use this in the shell you would do something like the following examples: top = Top4R::Client.from_config(‘config/top.yml’, ‘dev’) top = Top4R::Client.from_config(‘config/top.yml’)



23
24
25
26
# File 'lib/top4r/console.rb', line 23

def from_config(config_file, env = 'test')
  yaml_hash = YAML.load(File.read(config_file))
  self.new yaml_hash[env]
end

Instance Method Details

#areas(method = :list, options = {}, &block) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/top4r/client/shipping.rb', line 14

def areas(method = :list, options = {}, &block)
  valid_method(method, @@AREA_METHODS, :area)
  params = {:fields => Top4R::Area.fields}.merge(options)
  response = http_connect {|conn| create_http_get_request(@@AREA_METHODS[method], params)}
  areas = Top4R::Area.unmarshal(JSON.parse(response.body)["rsp"]["areas"])
  areas.each {|area| bless_model(area); yield area if block_given?}
  areas
end

#deliver_trade(t, method = :send, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/top4r/client/shipping.rb', line 32

def deliver_trade(t, method = :send, options = {})
  valid_method(method, @@DELIVERY_METHODS, :delivery)
  params = {}
  if t.is_a?(Top4R::Delivery)
    params = t.to_hash
  else
    t = t.tid if t.is_a?(Top4R::Trade)
    params = options.merge(:tid => t)
  end
  response = http_connect {|conn| create_http_get_request(@@DELIVERY_METHODS[method], params)}
  json = JSON.parse(response.body)
  json.is_a?(Hash) ? json["rsp"]["is_success"] : false
end

#initObject



11
12
13
14
15
16
17
18
19
# File 'lib/top4r/client/base.rb', line 11

def init
  total_results = 0
  @@logger = Top4R::Logger.new(@@config.logger, @@config.trace)
  if @parameters and @session
    @parameters = Base64.decode64(@parameters).split('&').inject({}) do |hsh, i| kv = i.split('='); hsh[kv[0]] = kv[1]; hsh end
    @login = user(@parameters['visitor_nick'].to_utf8)
    # puts "login: #{@login.inspect}"
  end
end

#inspectObject



6
7
8
9
# File 'lib/top4r/client/base.rb', line 6

def inspect
  s = old_inspect
  s.gsub!(/@app_secret=".*?"/, '@app_secret="XXXX"')
end

#logged_in?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/top4r/client/user.rb', line 7

def logged_in?
  @login.is_a?(Top4R::User) ? true : false
end

#logistic_companies(method = :list, options = {}, &block) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/top4r/client/shipping.rb', line 23

def logistic_companies(method = :list, options = {}, &block)
  valid_method(method, @@LOGISTIC_COMPANY_METHODS, :logistic_company)
  params = {:fields => Top4R::LogisticCompany.fields}.merge(options)
  response = http_connect {|conn| create_http_get_request(@@LOGISTIC_COMPANY_METHODS[method], params)}
  logistic_companies = Top4R::LogisticCompany.unmarshal(JSON.parse(response.body)["rsp"]["logistic_companies"])
  logistic_companies.each {|logistic_company| bless_model(logistic_company); yield logistic_company if block_given?}
  logistic_companies
end

#my(method, options = {}) ⇒ Object



18
19
20
21
22
# File 'lib/top4r/client/user.rb', line 18

def my(method, options = {})
  valid_method(method, @@USER_METHODS, :user, true)
  users = user_request(@login.nick, method, options)
  method == :info ? bless_model(users.first) : bless_models(users)
end

#old_inspectObject



2
# File 'lib/top4r/client/base.rb', line 2

alias :old_inspect :inspect

#suites(u, service_code, method = :list, options = {}, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/top4r/client/suite.rb', line 6

def suites(u, service_code, method = :list, options = {}, &block)
  valid_method(method, @@SUITE_METHODS, :suite)
  u = u.nick if u.is_a?(Top4R::User)
  params = {:service_code => service_code}.merge(options).merge(:nick => u)
  response = http_connect {|conn| create_http_get_request(@@SUITE_METHODS[method], params)}
  suites = Top4R::Suite.unmarshal(JSON.parse(response.body)["rsp"]["suites"])
  suites.each {|suite| bless_model(suite); yield suite if block_given?}
  # puts "\nsuites: #{suites.inspect}"
  @total_results = JSON.parse(response.body)["rsp"]["totalResults"].to_i
  suites
end

#trade(t, method = :info, options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/top4r/client/trade.rb', line 29

def trade(t, method = :info, options = {})
  valid_method(method, @@TRADE_METHODS, :trade)
  t = t.tid if t.is_a?(Top4R::Trade)
  params = {:fields => Top4R::Trade.fields}.merge(options).merge(:tid => t)
  response = http_connect {|conn| create_http_get_request(@@TRADE_METHODS[method], params)}
  parsed_body = JSON.parse(response.body)
  
  if [:info, :fullinfo].member?(method)
    trades = Top4R::Trade.unmarshal(parsed_body["rsp"]["trades"])
    bless_model(trades.first)
  elsif method == :confirmfee
    confirmfees = Top4R::TradeConfirmFee.unmarshal(parsed_body["rsp"]["confirmFees"])
    bless_models(confirmfees)
  elsif [:close, :update_memo].member?(method)
    parsed_body["rsp"]["modified"]
  elsif method == :add_memo
    parsed_body["rsp"]["created"]
  end
end

#trades_for(method = :bought_list, options = {}, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/top4r/client/trade.rb', line 14

def trades_for(method = :bought_list, options = {}, &block)
  valid_method(method, @@TRADE_METHODS, :trade)
  params = {:fields => Top4R::Trade.fields}.merge(options)
  if method == :increments_list
    now = Time.now
    params = {:start_modified => (now - 24.hours).strftime("%Y-%m-%d %H:%M:%S"), :end_modified => now.strftime("%Y-%m-%d %H:%M:%S")}.merge(params)
  end
  response = http_connect {|conn| create_http_get_request(@@TRADE_METHODS[method], params)}
  trades = Top4R::Trade.unmarshal(JSON.parse(response.body)["rsp"]["trades"])
  trades.each {|trade| bless_model(trade); yield trade if block_given?}
  # puts "\ntrades: #{trades.inspect}"
  @total_results = JSON.parse(response.body)["rsp"]["totalResults"].to_i
  trades
end

#user(u, method = :info, options = {}) ⇒ Object



11
12
13
14
15
16
# File 'lib/top4r/client/user.rb', line 11

def user(u, method = :info, options = {})
  valid_method(method, @@USER_METHODS, :user)
  u = u.nick if u.is_a?(Top4R::User)
  users = user_request(u, method, options)
  method == :info ? bless_model(users.first) : bless_models(users)
end