fucking-great-advice 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/advice_me.rb +61 -0
  2. metadata +45 -0
@@ -0,0 +1,61 @@
1
+ # coding: utf-8
2
+ require 'net/http'
3
+ require 'json/pure'
4
+ require 'cgi'
5
+ class AdviceMe
6
+ BASE_URL = "http://fucking-great-advice.ru/api/"
7
+ attr_reader :text
8
+
9
+ def initialize(attr={})
10
+ @id = attr['id'].to_i
11
+ @text = attr['text']
12
+ @sound = attr['sound']
13
+ @stat = attr['stat'].to_i
14
+ end
15
+
16
+ # @advice = AdviceMe.random
17
+ # returning object
18
+ def self.random
19
+ uri = 'random'
20
+ new(get(uri))
21
+ end
22
+
23
+ # @advice = AdviceMe.something
24
+ # returning just html-text
25
+ def self.something
26
+ random.text
27
+ end
28
+
29
+ # @advice = AdviceMe.latest
30
+ # returning collection of objects of the last one
31
+ # or
32
+ # @advices = AdviceMe.latest(n)
33
+ # returning collection of n objects
34
+ def self.latest(n=0)
35
+ uri = (!n)? 'latest': 'latest/'+n.to_s
36
+ coll = []
37
+ get(uri).each do |advice|
38
+ coll << new(advice)
39
+ end
40
+ coll
41
+ end
42
+
43
+ # @adivice = AdviceMe.fresh
44
+ # return the html-text of fresh advice
45
+ def self.fresh
46
+ latest.first.text
47
+ end
48
+
49
+ private
50
+ def self.get(uri)
51
+ url = "#{BASE_URL}"+uri
52
+ resp = Net::HTTP.get_response(URI.parse(url))
53
+ data = resp.body
54
+ begin
55
+ object = JSON.parse(data)
56
+ rescue JSON::ParserError
57
+ raise "parse_error on #{raw_url}"
58
+ end
59
+ end
60
+
61
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fucking-great-advice
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - wi11son
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-25 00:00:00.000000000Z
13
+ dependencies: []
14
+ description:
15
+ email:
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/advice_me.rb
21
+ homepage:
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.6
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: That gem is to make easy access to great advices from fucking-great-advice.ru
45
+ test_files: []