Class: Shiny
- Inherits:
-
Object
- Object
- Shiny
- Defined in:
- lib/shiny-sdk.rb
Instance Method Summary collapse
-
#add(spider_name, level, data = nil, hash = false) ⇒ Object
添加数据项.
-
#initialize(api_key, api_secret_key, api_host = 'https://shiny.kotori.moe') ⇒ Shiny
constructor
A new instance of Shiny.
-
#recent(page = 1) ⇒ Object
获取最新项目.
Constructor Details
#initialize(api_key, api_secret_key, api_host = 'https://shiny.kotori.moe') ⇒ Shiny
Returns a new instance of Shiny.
14 15 16 17 18 |
# File 'lib/shiny-sdk.rb', line 14 def initialize(api_key, api_secret_key, api_host='https://shiny.kotori.moe') @API_KEY = api_key @API_SECRET_KEY = api_secret_key @API_HOST = api_host end |
Instance Method Details
#add(spider_name, level, data = nil, hash = false) ⇒ Object
添加数据项
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/shiny-sdk.rb', line 21 def add(spider_name, level, data=nil, hash=false) if data.nil? data = {} end url = @API_HOST + '/Data/add' payload = {'api_key' => @API_KEY} event = {"level": level, "spiderName": spider_name} # 如果没有手动指定Hash,将会把data做一次md5生成hash begin if hash event['hash'] = hash else event['hash'] = Digest::MD5.hexdigest(data.to_json) end rescue raise ShinyError.new('Fail to generate hash.') end event['data'] = data payload['sign'] = Digest::SHA1.hexdigest(@API_KEY + @API_SECRET_KEY + event.to_json) payload["event"] = event.to_json response = Net::HTTP.post_form(URI.parse(url), payload) if response.code == '200' return JSON.parse(response.body) else raise ShinyError.new('Network error:' + response.code) end end |
#recent(page = 1) ⇒ Object
获取最新项目
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/shiny-sdk.rb', line 58 def recent(page=1) url = @API_HOST + "/Data/recent?page=#{page}" uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if uri.scheme == 'https' response = http.start {|http| http.request Net::HTTP::Get.new(uri.request_uri) } if response.code == '200' return JSON.parse(response.body) else raise ShinyError.new("HTTP Code: #{response.code}, Body: #{JSON.parse(response.body)}.") end end |