Class: Speedup::Weibo

Inherits:
Object
  • Object
show all
Defined in:
lib/speedup.rb

Instance Method Summary collapse

Instance Method Details

#get_info_from_yaml(key) ⇒ Object



47
48
49
50
# File 'lib/speedup.rb', line 47

def get_info_from_yaml key
	file=File.expand_path("./speedup.yaml",File.dirname(__FILE__))
	YAML::load_file(file)[key]
end

#weibo(text) ⇒ Object



52
53
54
55
56
57
# File 'lib/speedup.rb', line 52

def weibo(text)
	write_access_token
	token = get_info_from_yaml(:access_token)
	conn = Faraday.new(:url => 'https://api.weibo.com')
	conn.post '/2/statuses/update.json',:access_token => token,:status => text
end

#write_access_tokenObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/speedup.rb', line 30

def write_access_token
	config = YAML.load_file(File.expand_path("./speedup.yaml",File.dirname(__FILE__)))
	if config[:code].nil?
		puts "\ncode missing\n\n"
		exit
	end
	if config[:access_token].nil?
		code = config[:code]
		conn = Faraday.new(:url => "https://api.weibo.com/")

		result = conn.post '/oauth2/access_token',:client_id => '44721943',:client_secret => '96e57fd158c96e1b4d961124dca55e56',:grant_type => 'authorization_code',:code => code,:redirect_uri => 'https://github.com/jinbin/speedup'

		config[:access_token] = JSON.parse(result.env[:body])["access_token"]
		File.open(File.expand_path("./speedup.yaml",File.dirname(__FILE__)),"w") {|f| YAML.dump(config,f)}	
	end
end

#write_code(code, isforce = false) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/speedup.rb', line 18

def write_code code, isforce=false
	config = YAML.load_file(File.expand_path("./speedup.yaml",File.dirname(__FILE__)))
	if config[:code].nil?
		config[:code] = code	
	elsif !config[:code].nil? and !isforce 
		
	elsif !config[:code].nil? and isforce
		config[:code] = code
	end	
	File.open(File.expand_path("./speedup.yaml",File.dirname(__FILE__)),"w") {|f| YAML.dump(config,f)}
end