Skip to content

Commit

Permalink
Get legoset
Browse files Browse the repository at this point in the history
  • Loading branch information
pelf committed Jun 4, 2017
0 parents commit b83bb0a
Show file tree
Hide file tree
Showing 25 changed files with 935 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--format documentation
--color
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: ruby
rvm:
- 2.3.0
before_install: gem install bundler -v 1.11.2
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in brickset.gemspec
gemspec
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Pedro Freitas

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:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

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.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Brickset

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/brickset`. To experiment with that code, run `bin/console` for an interactive prompt.

TODO: Delete this and the text above, and describe your gem

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'brickset'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install brickset

## Usage

TODO: Write usage instructions here

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/brickset.


## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).

6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
14 changes: 14 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "brickset"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require "irb"
IRB.start
8 changes: 8 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
Binary file added brickset-0.1.0.gem
Binary file not shown.
35 changes: 35 additions & 0 deletions brickset.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'brickset/version'

Gem::Specification.new do |spec|
spec.name = "brickset"
spec.version = Brickset::VERSION
spec.authors = ["Pedro Freitas"]
spec.email = ["[email protected]"]
spec.date = ["2013-01-01"]
spec.summary = %q{Get LEGO set details from brickset.com.}
spec.homepage = "http://github.com/pelf/brickset"
spec.license = "MIT"
spec.files = Dir['{lib,spec}/**/*', 'Rakefile', 'README.md']

# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
# delete this section to allow pushing this gem to any host.
if spec.respond_to?(:metadata)
spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
else
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
end

spec.add_dependency "nokogiri", "~> 1.7"
spec.add_dependency "activesupport", "~> 4.0"
spec.add_dependency "virtus", "~> 1.0"

spec.add_development_dependency "bundler", "~> 1.14"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "webmock", "~> 2.3"
spec.add_development_dependency "vcr", "~> 3.0"
spec.add_development_dependency "pry", "~> 0"
end
12 changes: 12 additions & 0 deletions lib/brickset.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'nokogiri'
require 'virtus'
require 'active_support/all'

require 'brickset/version'

require 'brickset/client'
require 'brickset/web'

require 'brickset/services/get_legoset'

require 'brickset/models/legoset'
7 changes: 7 additions & 0 deletions lib/brickset/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Brickset
class Client
def get_set(legoset_id)
Services::GetLegoset.new(legoset_id).run
end
end
end
28 changes: 28 additions & 0 deletions lib/brickset/models/legoset.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Brickset
class Legoset
include ::Virtus.model

attribute :legoset_id
attribute :name
attribute :theme
attribute :subtheme
attribute :year
attribute :pieces
attribute :minifigs
attribute :rrpp
attribute :rrpd
attribute :rrp
attribute :ppp
attribute :score
attribute :available_since
attribute :eol

def initialize(legoset_id)
self.legoset_id = legoset_id
end

def url
"https://brickset.com/sets/#{@legoset_id}-1"
end
end
end
Empty file added lib/brickset/models/theme.rb
Empty file.
82 changes: 82 additions & 0 deletions lib/brickset/services/get_legoset.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
module Brickset
module Services
class GetLegoset

def initialize(legoset_id)
@legoset_id = legoset_id
end

def run
details = parse_details Web.parse(legoset.url)
legoset.attributes = details
legoset
end

private

def legoset
@legoset ||= Legoset.new(@legoset_id)
end

def parse_details(html)
# TODO: refactor / cleanup
details = { }
eol = 0
html.css('section.featurebox dl').css('dt,dd').to_a.each_slice(2) do |dt, dd|
case dt.text.strip
when 'Name'
details[:name] = dd.text
when 'Theme'
details[:theme] = dd.text
when 'Subtheme'
details[:subtheme] = dd.text
when 'Year released'
details[:year] = dd.text.to_i
when 'Pieces'
details[:pieces] = dd.text.to_i
when 'Minifigs'
details[:minifigs] = dd.text.to_i
when 'Weight'
dd.text =~ /([\d|\.]+)Kg/
details[:weight] = $1.to_f
when 'RRP'
rrps = dd.text.split('/').map{|p| p.match(/([\d|\.]+)/).to_s}
details[:rrpp] = rrps.first.to_f
details[:rrpd] = rrps.second.to_f
if rrps.third.present?
details[:rrp] = rrps.third.to_f
else
# we're converting price in dollars (almost) directly on purpose (in US sets tend to be cheaper)
if details[:rrpp] and details[:rrpd] # use avg between dollars and bgp
details[:rrp] = ((details[:rrpp] * 1.25)+(details[:rrpd] * 0.9))/2.0
elsif details[:rrpp]
details[:rrp] = (details[:rrpp] * 1.25)
elsif details[:rrpd]
details[:rrp] = (details[:rrpd] * 0.9)
end
end
when 'Price per piece'
dd.text =~ /([\d|\.]+)p\s+\/\s+([\d|\.]+)c/
details[:ppp] = $1.to_f
when 'Rating'
dd.text =~ /[^\d]*([\d|\.]+) from/
details[:score] = $1.to_f
when 'United Kingdom'
if dd.children.first.text =~ /(.+) - (.+)/
details[:available_since] = Date.parse($1)
if $2.strip != 'now'
eol += 1
details[:available_until] = Date.parse($2)
end
end
when 'United States'
eol += 1 unless dd.children.first.text =~ /now/
when 'Canada'
eol += 1 unless dd.children.first.text =~ /now/
end
end
return details.merge({eol: eol})
end
end
end
end
3 changes: 3 additions & 0 deletions lib/brickset/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Brickset
VERSION = "0.1.0"
end
14 changes: 14 additions & 0 deletions lib/brickset/web.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'net/http'
require 'open-uri'

class Web
USER_AGENT = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36"

def self.get(url, headers={})
open url, { "User-Agent" => USER_AGENT }.merge(headers)
end

def self.parse(url, headers={})
Nokogiri::HTML Web.get(url, headers)
end
end
20 changes: 20 additions & 0 deletions spec/brickset/client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'spec_helper'

describe Brickset::Client do
let(:described_instance) { described_class.new }
let(:legoset_id) { '10251' }

it 'initializes' do
expect(described_instance).to be
end

describe '#get_set' do
subject { described_instance.get_set legoset_id }

it 'calls the correct service' do
expect(Brickset::Services::GetLegoset).to receive(:new).and_call_original.with(legoset_id)
expect_any_instance_of(Brickset::Services::GetLegoset).to receive(:run)
subject
end
end
end
18 changes: 18 additions & 0 deletions spec/brickset/models/legoset_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'spec_helper'

describe Brickset::Legoset do
let(:described_instance) { described_class.new legoset_id }
let(:legoset_id) { '10251' }

it 'initializes' do
expect(described_instance).to be
end

describe '#url' do
subject { described_instance.url }

it 'returns a url' do
expect(subject).to match /http.*brickset.*/
end
end
end
24 changes: 24 additions & 0 deletions spec/brickset/services/get_legoset_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'spec_helper'

describe Brickset::Services::GetLegoset do
use_vcr_cassette "get_legoset"

let(:described_instance) { described_class.new legoset_id }
let(:legoset_id) { '10251' }

it 'initializes' do
expect(described_instance).to be
end

describe '#run' do
subject { described_instance.run }

it 'returns a set with details' do
expect(subject).to be

[:name, :theme, :subtheme, :year, :pieces, :minifigs, :rrpp, :rrpd, :rrp, :ppp, :score, :available_since, :eol].each do |attr|
expect(subject.send(attr)).to be_present
end
end
end
end
7 changes: 7 additions & 0 deletions spec/brickset_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'spec_helper'

describe Brickset do
it 'has a version number' do
expect(Brickset::VERSION).not_to be nil
end
end
Loading

0 comments on commit b83bb0a

Please sign in to comment.