Skip to content
Browse files

synchronize profile data

  • Loading branch information...
1 parent 8bdffba commit ad4f717e6ac8f131c1d38ec2bc6ca56d34c4eff8 @ck3g committed Mar 23, 2013
View
2 Gemfile
@@ -32,7 +32,7 @@ gem "carrierwave", "~> 0.8.0"
gem "mini_magick", "~> 3.5.0"
gem "ckeditor", "~> 4.0.2"
gem "twitter_cldr", "~> 2.2.0"
-
+gem "battlenet_info", path: "../battlenet_info"
group :assets do
gem 'sass-rails', '~> 3.2.5'
View
7 Gemfile.lock
@@ -1,3 +1,9 @@
+PATH
+ remote: ../battlenet_info
+ specs:
+ battlenet_info (0.2.0)
+ nokogiri (~> 1.5.6)
+
GEM
remote: https://rubygems.org/
specs:
@@ -303,6 +309,7 @@ DEPENDENCIES
acts-as-taggable-on (~> 2.3.3)
acts_as_commentable (~> 3.0.1)
anjlab-bootstrap-rails (>= 2.3)
+ battlenet_info!
bitmask_attributes (~> 0.4.0)
cancan (~> 1.6.8)
capistrano
View
5 app/controllers/profiles_controller.rb
@@ -20,6 +20,11 @@ def update
respond_with @profile
end
+ def sync
+ @profile.sync!
+ redirect_to @profile
+ end
+
private
def find_profile
@profile = Profile.find params[:id]
View
24 app/models/profile.rb
@@ -6,6 +6,8 @@ class Profile < ActiveRecord::Base
:last_name, :details, :avatar_style, :achievements, :rank, :points, :wins, :loses, :win_rate,
:profile_url, :synchronized_at
+ validate :profile_url_cannot_be_invalid_battlenet_url
+
delegate :email, :current_sign_in_at, to: :user, prefix: true
delegate :username, to: :user, prefix: false
delegate :name, to: :country, prefix: true, allow_nil: true
@@ -25,4 +27,26 @@ def has_league?
def league_parts
league.split "_"
end
+
+ def sync!
+ player = BattleNetInfo.new profile_url
+ data = player.to_hash
+ self.bnet_server = data[:server]
+ self.bnet_name = data[:player_name]
+ self.achievements = data[:achievement_points]
+ self.points = data[:points]
+ self.wins = data[:wins]
+ self.rank = data[:rank]
+ self.league = data[:league]
+ self.avatar_style = player.portrait_html_style("/assets/bnet/")
+ self.synchronized_at = Time.current
+ self.save
+ end
+
+ private
+ def profile_url_cannot_be_invalid_battlenet_url
+ if profile_url.present? && !BattleNetInfo.new(profile_url).valid_url?
+ errors.add(:profile_url, I18n.t("activerecord.errors.profile.profile_url.invalid"))
+ end
+ end
end
View
2 app/views/articles/_article.html.haml
@@ -18,7 +18,7 @@
.pull-right
= succeed ":" do
= t(:published_by)
- = link_to user_path(article.user) do
+ = link_to profile_path(article.user.profile) do
= article.user_name || article.user_email
= " @"
= l article.published_at, format: :long
View
2 app/views/profiles/show.html.haml
@@ -27,7 +27,7 @@
.span2
= avatar_html @profile
- if can? :manage, @profile
- = link_to t(:reload), "#", class: "btn"
+ = link_to t(:reload), sync_profile_path(@profile), class: "btn", method: :put
.span10
%p
%strong= "#{Profile.human_attribute_name(:bnet_name)}:"
View
4 config/locales/models/profile.ru.yml
@@ -1,6 +1,10 @@
ru:
activerecord:
+ errors:
+ profile:
+ profile_url:
+ invalid: Неверный URL
attributes:
profile:
first_name: Имя
View
4 config/routes.rb
@@ -19,7 +19,9 @@
resources :pages, except: [:show]
get "pages/:permalink" => "pages#show", as: :page
- resources :profiles, except: [:index, :new, :create, :destroy]
+ resources :profiles, only: [:show, :edit, :update] do
+ put :sync, on: :member
+ end
root :to => 'articles#index'
end
View
12 spec/controllers/profiles_controller_spec.rb
@@ -21,7 +21,7 @@
describe "PUT #update" do
context "with valid attributes" do
- let(:profile_url) { "http://battle.net/profiles/fake/73" }
+ let(:profile_url) { "http://eu.battle.net/sc2/en/profile/267901/1/Zakk/" }
def update_profile_url
put :update, id: profile, profile: attributes_for(:profile, profile_url: profile_url)
@@ -38,4 +38,14 @@ def update_profile_url
end
end
end
+
+ describe "PUT #sync" do
+ before do
+ Profile.any_instance.should_receive(:sync!)
+ put :sync, id: profile
+ end
+
+ it { should assign_to(:profile).with profile }
+ it { should redirect_to profile }
+ end
end
View
1 spec/factories/profiles.rb
@@ -4,5 +4,6 @@
factory :profile do
user
country
+ sequence(:profile_url) { |n| "http://eu.battle.net/sc2/en/profile/#{n}/1/name/" }
end
end
View
65 spec/models/profile_spec.rb
@@ -1,6 +1,9 @@
require 'spec_helper'
describe Profile do
+ let(:zakk_url) { "http://eu.battle.net/sc2/en/profile/267901/1/Zakk/" }
+ let(:google_url) { "http://google.com" }
+
it "has a valid factory" do
expect(create :profile).to be_valid
end
@@ -9,4 +12,66 @@
it { should belong_to :user }
it { should belong_to :country }
end
+
+ describe ".validations" do
+ subject { create :profile }
+ context "when valid" do
+ it { should allow_value(zakk_url).for :profile_url }
+ it { should allow_value("").for :profile_url }
+ end
+
+ context "when invalid" do
+ it { should_not allow_value(google_url).for(:profile_url).with_message(I18n.t("activerecord.errors.profile.profile_url.invalid")) }
+ end
+ end
+
+ describe "#sync!" do
+ let!(:zakk) { create :profile, profile_url: zakk_url }
+ let(:portrait_style) { "background: url('bnet/0-90.jpg') -450px -90px no-repeat; width: 90px; height: 90px;" }
+
+ before do
+ player = mock BattleNetInfo
+ BattleNetInfo.should_receive(:new).with(zakk_url).and_return(player)
+ player.stub(:to_hash).and_return({
+ server: "eu",
+ player_name: "Zakk",
+ achievement_points: 2450,
+ points: 74,
+ wins: 5,
+ rank: 2,
+ league: "none_4"
+ })
+ player.stub(:portrait_html_style).with("/assets/bnet/").and_return(portrait_style)
+ zakk.should_receive(:profile_url_cannot_be_invalid_battlenet_url).and_return(nil)
+ end
+
+ it { zakk.sync! }
+ it "changes bnet_server" do
+ expect { zakk.sync! }.to change { zakk.bnet_server }.to "eu"
+ end
+ it "changes bnet_name" do
+ expect { zakk.sync! }.to change { zakk.bnet_name }.to "Zakk"
+ end
+ it "changes the achievement points" do
+ expect { zakk.sync! }.to change { zakk.achievements }.to 2450
+ end
+ it "changes the points" do
+ expect { zakk.sync! }.to change { zakk.points }.to 74
+ end
+ it "changes the wins" do
+ expect { zakk.sync! }.to change { zakk.wins }.to 5
+ end
+ it "changes the rank" do
+ expect { zakk.sync! }.to change { zakk.rank }.to 2
+ end
+ it "changes the league" do
+ expect { zakk.sync! }.to change { zakk.league }.to "none_4"
+ end
+ it "changes the points" do
+ expect { zakk.sync! }.to change { zakk.avatar_style }.to portrait_style
+ end
+ it "changes the synchronized_at" do
+ expect { zakk.sync! }.to change { zakk.synchronized_at }
+ end
+ end
end

0 comments on commit ad4f717

Please sign in to comment.
Something went wrong with that request. Please try again.