shelfari 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'mechanize'
4
+ gem 'json'
5
+ gem 'rake'
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ domain_name (0.5.2)
5
+ unf (~> 0.0.3)
6
+ json (1.6.5)
7
+ mechanize (2.3)
8
+ domain_name (~> 0.5, >= 0.5.1)
9
+ mime-types (~> 1.17, >= 1.17.2)
10
+ net-http-digest_auth (~> 1.1, >= 1.1.1)
11
+ net-http-persistent (~> 2.5, >= 2.5.2)
12
+ nokogiri (~> 1.4)
13
+ ntlm-http (~> 0.1, >= 0.1.1)
14
+ webrobots (~> 0.0, >= 0.0.9)
15
+ mime-types (1.18)
16
+ net-http-digest_auth (1.2)
17
+ net-http-persistent (2.5.2)
18
+ nokogiri (1.5.2)
19
+ nokogiri (1.5.2-x86-mingw32)
20
+ ntlm-http (0.1.1)
21
+ rake (0.9.2.2)
22
+ unf (0.0.5)
23
+ unf_ext
24
+ unf_ext (0.0.4)
25
+ unf_ext (0.0.4-x86-mingw32)
26
+ webrobots (0.0.13)
27
+
28
+ PLATFORMS
29
+ ruby
30
+ x86-mingw32
31
+
32
+ DEPENDENCIES
33
+ json
34
+ mechanize
35
+ rake
data/LICENSE.md ADDED
@@ -0,0 +1,7 @@
1
+ (The MIT License)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ [![Code Climate](https://codeclimate.com/github/robertboloc/shelfari.png)](https://codeclimate.com/github/robertboloc/shelfari)
2
+ [![Build Status](https://travis-ci.org/robertboloc/shelfari.png)](https://travis-ci.org/robertboloc/shelfari)
3
+
4
+ This gem uses [mechanize](https://github.com/tenderlove/mechanize) to scrape content from [shelfari.com](http://www.shelfari.com) and simulate an API, because currently there is no official one. Given the nature of obtaining the data, things might break often, so use at your own risk.
5
+
6
+ #### Methods
7
+ ```ruby
8
+ book_by_id(id) #returns information about a book based on id
9
+ book_by_isbn(isbn) #returns information about a book based on isbn
10
+ user(username) #returns user details
11
+ now_reading(username) #returns the books the user is currently reading
12
+ ```
13
+ #### Installation
14
+ ```ruby
15
+ gem install shelfari
16
+ ```
17
+ #### Usage
18
+ ```ruby
19
+ require 'shelfari'
20
+
21
+ library = Shelfari.new
22
+ ```
23
+ Get info about a book
24
+ ```ruby
25
+ myBook = library.book(5831441)
26
+ ```
27
+ This returns the following JSON
28
+ ```json
29
+ {
30
+ "bookid": "5831441",
31
+ "editionid": "7405802",
32
+ "title": "The Girl Who Kicked the Hornet's Nest",
33
+ "author": "Stieg Larsson",
34
+ "cover": "http://ecx.images-amazon.com/images/I/41BHhplwx0L._SL236.jpg",
35
+ "url": "http://www.shelfari.com/books/5831441/The-Girl-Who-Kicked-the-Hornets-Nest"
36
+ }
37
+ ```
38
+
39
+ #### License
40
+
41
+ This library is distributed under the MIT license. Please see the LICENSE file.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
data/lib/shelfari.rb ADDED
@@ -0,0 +1,11 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'mechanize'
4
+ require 'json'
5
+ require 'shelfari/api'
6
+
7
+ module Shelfari
8
+ def self.new
9
+ Shelfari::API.new
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ require 'shelfari/book'
2
+ require 'shelfari/user'
3
+ require 'shelfari/helper'
4
+
5
+ module Shelfari
6
+ class API
7
+ include Shelfari::Book
8
+ include Shelfari::User
9
+ include Shelfari::Helper
10
+
11
+ def initialize
12
+ @agent = Mechanize.new
13
+ end
14
+
15
+ def request(uri)
16
+ @agent.get('http://www.shelfari.com'+uri)
17
+ end
18
+
19
+ def response(response)
20
+ JSON.generate(response)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,27 @@
1
+ module Shelfari
2
+ module Book
3
+ def book_by_id(id)
4
+ page = request('/books/'+id.to_s)
5
+ raw_book = page.parser.xpath("//a[@bookid='"+id.to_s+"']").first
6
+ book = {
7
+ :bookid => id.to_s,
8
+ :editionid => raw_book['editionid'],
9
+ :title => raw_book['title'],
10
+ :author => page.title.gsub(/(.*)by\s/, ''),
11
+ :cover => raw_book.xpath(".//img").first['src'],
12
+ :url => raw_book['href']
13
+ }
14
+ response(book)
15
+ end
16
+
17
+ def book_by_isbn(isbn)
18
+ page = request('/search/books?Keywords='+isbn.to_s)
19
+ response(extract_books(page).first)
20
+ end
21
+
22
+ def search(title)
23
+ page = request('/search/books?Keywords='+title.gsub(/\s/, '%20'))
24
+ response(extract_books(page))
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,21 @@
1
+ module Shelfari
2
+ module Helper
3
+ def extract_books(page)
4
+ results = Hash.new
5
+ raw_books = page.parser.xpath("//a[@class='book']").each{ |raw_book|
6
+ if !results.has_key?(raw_book['bookid'])
7
+ book = {
8
+ :bookid => raw_book['bookid'],
9
+ :editionid => raw_book['editionid'],
10
+ :title => raw_book.xpath(".//img").first['alt'],
11
+ :cover => raw_book.xpath(".//img").first['src'],
12
+ :url => raw_book['href']
13
+ }
14
+
15
+ results[raw_book['bookid']] = book
16
+ end
17
+ }
18
+ results.values
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,44 @@
1
+ module Shelfari
2
+ module User
3
+ def user(user)
4
+ page = request("/#{user}")
5
+ desc = page.parser.at('meta[@name="description"]')
6
+ user = {
7
+ :description => desc ? desc[:content] : nil,
8
+ :avatar => page.parser.xpath("//a[@class='avatar']//img").first['src'],
9
+ :books => extract_books(page)
10
+ }
11
+ response(user)
12
+ end
13
+
14
+ def followers(user)
15
+ results = Array.new
16
+ page = request("/#{user}/friends/followers")
17
+ page.parser.xpath("//ul[@class='grid grid_x_2']//li").each{ |raw_follower|
18
+ follower = {
19
+ :user => raw_follower.css('a.name').first['href'].gsub('http://www.shelfari.com/', ''),
20
+ :name => raw_follower.css('a.name').first.content,
21
+ :avatar => raw_follower.xpath(".//a[@class='avatar']//img/@src").first.content,
22
+ :lastseen => raw_follower.xpath(".//span[@class='info']").first.content
23
+ }
24
+ results << follower
25
+ }
26
+ response(results)
27
+ end
28
+
29
+ def now_reading(user)
30
+ page = request("/#{user}/lists/NowReading")
31
+ page.parser.xpath("//script").each do |raw_script|
32
+ raw_script.children.each do |kid|
33
+ if kid.content =~ /shelfPrePopulation/
34
+ # trim the front
35
+ val = kid.content.sub(/^[^{]*/,'')
36
+ # trim the back
37
+ val.sub!(/;\s*$/, '')
38
+ return val
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
data/shelfari.gemspec ADDED
@@ -0,0 +1,14 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = %q{shelfari}
3
+ spec.author = 'Robert Boloc'
4
+ spec.email = 'robertboloc@gmail.com'
5
+ spec.homepage = 'http://robertboloc.github.com/shelfari'
6
+ spec.version = "0.0.1"
7
+ spec.date = %q{2011-03-26}
8
+ spec.summary = %q{Shelfari web API}
9
+ spec.description = <<-EOF
10
+ Simulates a basic API, as there is no official one (yet)
11
+ EOF
12
+ spec.files = `git ls-files`.split("\n")
13
+ spec.require_paths = ["lib"]
14
+ end
@@ -0,0 +1,34 @@
1
+ require 'test/unit'
2
+ require 'shelfari'
3
+
4
+ class ShelfariTest < Test::Unit::TestCase
5
+ @@library = Shelfari.new
6
+ @@test_user = 'robertboloc'
7
+
8
+ def test_book
9
+ testBook = @@library.book_by_id(5831441)
10
+
11
+ # check id
12
+ assert testBook.include? '5831441'
13
+
14
+ # check title
15
+ assert testBook.include? 'The Girl Who Kicked the Hornet\'s Nest'
16
+
17
+ # check author
18
+ assert testBook.include? 'Stieg Larsson'
19
+ end
20
+
21
+ def test_user
22
+ testUser = @@library.user(@@test_user)
23
+
24
+ #check avatar
25
+ assert testUser.include? 'http://'
26
+ end
27
+
28
+ def test_now_reading
29
+ testNowReading = @@library.now_reading(@@test_user)
30
+
31
+ #check
32
+ assert testNowReading.include? 'books'
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shelfari
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Robert Boloc
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-03-26 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! ' Simulates a basic API, as there is no official one (yet)
15
+
16
+ '
17
+ email: robertboloc@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .travis.yml
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE.md
26
+ - README.md
27
+ - Rakefile
28
+ - lib/shelfari.rb
29
+ - lib/shelfari/api.rb
30
+ - lib/shelfari/book.rb
31
+ - lib/shelfari/helper.rb
32
+ - lib/shelfari/user.rb
33
+ - shelfari.gemspec
34
+ - test/test_shelfari.rb
35
+ homepage: http://robertboloc.github.com/shelfari
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.23
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Shelfari web API
59
+ test_files: []