habr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -0,0 +1,5 @@
1
+ require 'rake'
2
+ require 'rspec/core/rake_task'
3
+ require File.expand_path(File.dirname(__FILE__) + '/lib/habr.rb')
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,66 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+ require 'cgi'
4
+
5
+ module Habr
6
+
7
+ HABR_API_PROFILE_URL = 'http://habrahabr.ru/api/profile/%%username%%/'
8
+
9
+ class Loader
10
+
11
+ class << self
12
+
13
+ def load_user_data(user_name)
14
+ url = Habr::HABR_API_PROFILE_URL.gsub('%%username%%', CGI.escape(user_name)).to_s
15
+ doc = Nokogiri::XML(open(url,
16
+ 'Content-type' => 'text/xml',
17
+ 'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_7) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.57 Safari/534.24'
18
+ ))
19
+ data = {}
20
+ error = nil
21
+ doc.css('habrauser error').each do |e|
22
+ error = e.content.to_s
23
+ end
24
+ raise "Data loading error: #{error}" if !error.nil?
25
+ Habr::User::FIELDS.each do |f|
26
+ doc.css("habrauser #{f.to_s}").each do |field|
27
+ data[f] = field.content.to_s
28
+ end
29
+ end
30
+ data
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+
37
+ class User
38
+
39
+ FIELDS = [:login, :karma, :rating, :ratingPosition]
40
+
41
+ FIELDS.each do |f|
42
+ attr_accessor f
43
+ end
44
+
45
+ def initialize(data)
46
+ FIELDS.each do |f|
47
+ send("#{f.to_s}=".to_sym, data[f]) if !data[f].nil?
48
+ end
49
+ end
50
+
51
+ class << self
52
+
53
+ def find_by_name(name)
54
+ begin
55
+ data = Habr::Loader.load_user_data name
56
+ Habr::User.new(data)
57
+ rescue => e
58
+ nil
59
+ end
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Habr::Loader do
6
+
7
+ let(:username1) { '4pcbr' }
8
+ let(:username2) { '_4pcbr_' }
9
+
10
+ describe 'Load data for valid username = "4pcbr"' do
11
+
12
+ it 'should not raise error if data is valid' do
13
+ lambda { Habr::Loader.load_user_data(username1) }.should_not raise_error
14
+ end
15
+
16
+ it 'login data should eq "4pcbr"' do
17
+ data = Habr::Loader.load_user_data(username1)
18
+ data[:login].should eq username1.to_s
19
+ end
20
+
21
+ end
22
+
23
+ describe 'Load data with invalid username' do
24
+
25
+ it 'should raise error after loading' do
26
+ lambda { Habr::Loader.load_user_data(username2) }.should raise_error RuntimeError
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Habr::User do
6
+
7
+ let(:data) { {:login => login, :karma => karma, :rating => rating, :ratingPosition => ratingPosition} }
8
+ let(:login) { '4pcbr' }
9
+ let(:karma) { 49 }
10
+ let(:rating) { 46.25 }
11
+ let(:ratingPosition) { 871 }
12
+
13
+ describe 'instantiates from hash data and respodes to main getters' do
14
+
15
+ it 'should create new instance with parameters given' do
16
+ lambda { user = Habr::User.new(data) }.should_not raise_error
17
+ end
18
+
19
+ it 'should respond to main getters' do
20
+ user = Habr::User.new(data)
21
+ Habr::User::FIELDS.each do |field|
22
+ lambda { user.send(field) }.should_not raise_error
23
+ end
24
+ end
25
+
26
+ it 'user.login method should return "4pcbr"' do
27
+ user = Habr::User.new(data)
28
+ user.login.should eq login
29
+ end
30
+
31
+ it 'user.karma should return "49"' do
32
+ user = Habr::User.new(data)
33
+ user.karma.should eq karma
34
+ end
35
+
36
+ it 'user.rating should return "46.25"' do
37
+ user = Habr::User.new(data)
38
+ user.rating.should eq rating
39
+ end
40
+
41
+ it 'user rating position should return "871"' do
42
+ user = Habr::User.new(data)
43
+ user.ratingPosition.should eq ratingPosition
44
+ end
45
+ end
46
+
47
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'spork'
3
+
4
+ Dir[File.dirname(__FILE__) + '/../lib/*.rb'].each do |f|
5
+ require f
6
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: habr
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - 4pcbr
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-11 00:00:00 +04:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: nokogiri
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ description: Simple HTTP mapper for habrahabr.ru user data API
28
+ email: i4pcbr@gmail.com
29
+ executables: []
30
+
31
+ extensions: []
32
+
33
+ extra_rdoc_files:
34
+ - README.md
35
+ files:
36
+ - README.md
37
+ - lib/habr.rb
38
+ - Rakefile
39
+ - spec/spec_helper.rb
40
+ - spec/habr/loader_spec.rb
41
+ - spec/habr/user_spec.rb
42
+ has_rdoc: true
43
+ homepage: https://github.com/4pcbr/habr-gem
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.6.2
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Use it when you need to get habrahabr user data
70
+ test_files: []
71
+