Class: Shiny

Inherits:
Object
  • Object
show all
Defined in:
lib/shiny-sdk.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_secret_key, api_host) ⇒ Shiny

Returns a new instance of Shiny.



13
14
15
16
17
# File 'lib/shiny-sdk.rb', line 13

def initialize(api_key, api_secret_key, api_host)
  @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



19
20
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
# File 'lib/shiny-sdk.rb', line 19

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

  begin
    response = RestClient.post(url, payload)
    return JSON.parse(response.body)
  rescue => e
    raise ShinyError.new('Network error:' + e.to_s)
  end
end

#recentObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/shiny-sdk.rb', line 56

def recent
  # 获取最新项目
  url = @API_HOST + '/Data/recent'
  begin
    response = RestClient.get(url)
    return JSON.parse(response.body)
  rescue => e
    raise ShinyError.new('Network error:' + e.to_s)
  end
end