skooby 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in skooby.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Irio Irineu Musskopf Junior
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ Skooby
2
+ =====
3
+
4
+ Gives you some API like's methods to access [Skoob](http://skoob.com.br/) data.
5
+
6
+ Installation
7
+ ------------
8
+
9
+ Add in your Gemfile
10
+
11
+ ```ruby
12
+ gem 'skooby'
13
+ ```
14
+
15
+ Then run `bundle install`
16
+
17
+ Usage
18
+ -----
19
+
20
+ **Fetching information about a book**
21
+ ```ruby
22
+ book = Skooby::Book.new(id: 108)
23
+ book.url
24
+ # => "http://skoob.com.br/livro/108"
25
+ book.fetch # forces eager load of attributes
26
+ # => #<Skooby::Book:0x007faafa445898
27
+ # @author="J. K. Rowling",
28
+ # @id=108,
29
+ # @rating=0.88,
30
+ # @title="Harry Potter e a Pedra Filosofal",
31
+ # @votes="47688">
32
+ ```
33
+
34
+ **Searching books**
35
+ ```ruby
36
+ collection = Skooby::Search.new.book("O Iluminado")
37
+ # => [#<Skooby::Book:0x007faafa335098
38
+ # @author="Stephen King",
39
+ # @id="19733",
40
+ # @title="O Iluminado">,
41
+ # #<Skooby::Book:0x007faafa333d60
42
+ # @author="Deepak Chopra",
43
+ # @id="1896",
44
+ # @title="Buda">,
45
+ # #<Skooby::Book:0x007faafa3327d0
46
+ # @author="Helena Jobim",
47
+ # @id="4447",
48
+ # @title="Antonio Carlos Jobim">, ...]
49
+ ```
50
+
51
+ Contributing
52
+ ------------
53
+
54
+ Feel free to contribute with any patch, even removing extra white spaces.
55
+
56
+ Open a pull request and make us all happy!
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = 'test/*/test_*.rb'
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
@@ -0,0 +1,46 @@
1
+ require 'nokogiri'
2
+
3
+ module Skooby
4
+ class Book
5
+ attr_reader :id, :title, :author, :rating, :votes
6
+
7
+ def initialize(params = {})
8
+ @id = params[:id] if params.has_key?(:id)
9
+ @title = params[:title] if params.has_key?(:title)
10
+ @author = params[:author] if params.has_key?(:author)
11
+ @rating = params[:rating] if params.has_key?(:rating)
12
+ @votes = params[:votes] if params.has_key?(:votes)
13
+ end
14
+
15
+ def url
16
+ Skooby::Request.base_uri + path
17
+ end
18
+
19
+ def author
20
+ @author || fetch.author
21
+ end
22
+
23
+ def rating
24
+ @rating || fetch.rating
25
+ end
26
+
27
+ def votes
28
+ @votes || fetch.votes
29
+ end
30
+
31
+ def fetch
32
+ doc = Nokogiri::HTML.parse(Request.new.get(path))
33
+ @title = doc.css('#barra_titulo h1')[0].content
34
+ @author = doc.css('.l11')[0].content
35
+ @rating = (doc.css('#bt_ranking')[0].content.to_f / 5).round(2)
36
+ @votes = /(\d+)/.match(doc.css('#bt_estrelas')[0].content)[1]
37
+ self
38
+ end
39
+
40
+ private
41
+ def path
42
+ raise ArgumentError, 'Skooby::Book must have an id' if @id.nil?
43
+ "/livro/#@id"
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,14 @@
1
+ module Skooby
2
+ class Request
3
+ include HTTParty
4
+ base_uri 'skoob.com.br'
5
+
6
+ def post(path, options = {})
7
+ self.class.post(path, options).body
8
+ end
9
+
10
+ def get(path, options = {})
11
+ self.class.get(path, options).body
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,27 @@
1
+ require 'nokogiri'
2
+
3
+ module Skooby
4
+ class Search
5
+ def book(query)
6
+ opts = { body: { data: { Busca: { tipo: "livro", tag: query } } } }
7
+ page = Request.new.post('/livro/lista/', opts)
8
+ parse_result(page)
9
+ end
10
+
11
+ private
12
+ def parse_result(page_body)
13
+ doc = Nokogiri::HTML(page_body)
14
+ if doc.css('#livro').empty?
15
+ doc.css('.box_lista_busca').map do |book_node|
16
+ title_node = book_node.css('.l15ab')[0]
17
+ Book.new(id: /\A\/livro\/(\d+).*\Z/.match(title_node[:href])[1],
18
+ title: title_node.content,
19
+ author: book_node.css('.l11')[0].content)
20
+ end
21
+ else
22
+ Book.new(id: /\A\/livro\/(\d+).*\Z/.match(doc.css('#menubusca li:first a')[0][:href])[1],
23
+ title: doc.css('#barra_titulo h1')[0].content)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module Skooby
2
+ VERSION = "0.0.1"
3
+ end
data/lib/skooby.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'skooby/version'
2
+ require 'httparty'
3
+
4
+ $LOAD_PATH << "./lib/skooby"
5
+ files = Dir[File.dirname(__FILE__) + "/skooby/*.rb"]
6
+ files.each { |f| require f }
7
+
8
+ module Skooby
9
+ end
data/skooby.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'skooby/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "skooby"
8
+ gem.version = Skooby::VERSION
9
+ gem.authors = ["Irio Irineu Musskopf Junior"]
10
+ gem.email = ["irio.musskopf@caixadeideias.com.br"]
11
+ gem.description = "API like interface to provide information about books available at Skoob."
12
+ gem.summary = "Gives you some API like's methods to access Skoob data."
13
+ gem.homepage = "https://github.com/Irio/skooby"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_development_dependency "minitest"
20
+ gem.add_development_dependency "mocha"
21
+ gem.add_development_dependency "vcr"
22
+ gem.add_dependency "httparty"
23
+ gem.add_dependency "nokogiri"
24
+ end