Class: RTop::RTop

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/RTop/rtop.rb

Constant Summary collapse

API_URL =
'http://gw.api.taobao.com/router/rest'
API_URL_SANDBOX =
'http://gw.api.tbsandbox.com/router/rest'
API_OPTION =
{ "format" => "json", "v" => "2.0", "sign_method" => "md5" }

Instance Method Summary collapse

Constructor Details

#initialize(config_file = nil) ⇒ RTop

Returns a new instance of RTop.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/RTop/rtop.rb', line 18

def initialize(config_file = nil)
  unless config_file
    if defined? Rails
      config_file = "#{Rails.root}/config/taobao.yml"
    else
      config_file = File.join(Dir.pwd, 'config/taobao.yml')
      unless File.file?(config_file)
        config_file = File.join(Dir.pwd, 'taobao.yml')
      end
    end
    raise "taobao.yml not found, generate one with 'rtop generate [path]'." unless File.file?(config_file)
  end
  
  @settings = YAML.load_file(config_file)
  @settings = (defined? Rails) ? @settings[Rails.env] : @settings["defaults"]
end

Instance Method Details

#delete(url, options) ⇒ Object



59
60
61
62
# File 'lib/RTop/rtop.rb', line 59

def delete(url, options)
  options = prepare_options(method, options)
  self.class.delete(api_url, :query => options)
end

#get(method, options) ⇒ Object



44
45
46
47
# File 'lib/RTop/rtop.rb', line 44

def get(method, options)
  options = prepare_options(method, options)
  self.class.get(api_url, :query => options)
end

#get_item(item_id) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/RTop/rtop.rb', line 35

def get_item(item_id)                  
  response = self.get("taobao.item.get", { "num_iid" => item_id,
                              "fields" => "title,price,ems_fee,express_fee,detail_url,item_imgs,pic_url,location" })
  
  if response and response.parsed_response and response.parsed_response["item_get_response"] and response.parsed_response["item_get_response"]["item"]
    return response.parsed_response["item_get_response"]["item"]
  end
end

#post(url, options) ⇒ Object



49
50
51
52
# File 'lib/RTop/rtop.rb', line 49

def post(url, options)
  options = prepare_options(method, options)
  self.class.post(api_url, :query => options)
end

#put(url, options) ⇒ Object



54
55
56
57
# File 'lib/RTop/rtop.rb', line 54

def put(url, options)
  options = prepare_options(method, options)
  self.class.put(api_url, :query => options)
end