我正在使用Facebook API和Ruby on Rails,我正在尝试解析返回的JSON.我遇到的问题是Facebook base64URL对其数据进行编码.Ruby没有内置的base64URL解码.
有关base64编码和base64URL编码之间的区别,请参阅维基百科.
如何使用Ruby/Rails解码?
编辑:
因为有些人阅读有困难 - base64 URL与base64不同
我有一个经典的依赖解决问题.我以为我朝着正确的方向前进,但现在我遇到了障碍,我不知道该怎么办.
在已知的Universe(所有工件的缓存及其依赖关系)中,每个工件和版本之间都存在1-> n关系,并且每个版本可能包含一组不同的依赖关系.例如:
A
1.0.0
B (>= 0.0.0)
1.0.1
B (~> 0.1)
B
0.1.0
1.0.0
Run Code Online (Sandbox Code Playgroud)
给定一组"需求约束",我想找到最好的解决方案("最佳"是仍然满足所有约束的最高版本).以下是解决方案的"需求约束"示例:
solve!('A' => '~> 1.0') #=> {"A" => "1.0.1", "B" => "0.1.0"}
Run Code Online (Sandbox Code Playgroud)
实际上,有更多的要求:
solve!('A' => '~> 1.0', 'B' => '>= 0.0.0', 'C' => '...', 'D' => '...')
Run Code Online (Sandbox Code Playgroud)
(版本遵循语义版本标准)
当前的解决方案使用回溯并且性能不高.我做了一些挖掘,发现性能问题是由于宇宙的大小造成的.我决定尝试另一种方法,构建了"可能性" DAG图的只是一组要求:
class Graph
def initialize
@nodes = {}
@edges = {}
end
def node(object)
@nodes[object] ||= Set.new
self
end
def edge(a, b)
node(a)
node(b)
@nodes[a].add(b)
self
end
def nodes
@nodes.keys …Run Code Online (Sandbox Code Playgroud) 根据所有文档,该:read操作都有别名:index和:show:
alias_action :index, show, :to => :read
Run Code Online (Sandbox Code Playgroud)
但是,请考虑使用嵌套资源的以下方案:
resources :posts
resources :comments
end
Run Code Online (Sandbox Code Playgroud)
如果我定义这样的能力:
# ability.rb
can :read, Post
can :show, Comment
# comments_controller.rb
load_and_authorize_resource :organization, :find_by => :permalink
load_and_authorize_resource :membership, :through => :organization
Run Code Online (Sandbox Code Playgroud)
事情按预期工作.但是,如果我将:read操作更改为[:index,:show]:
# ability.rb
can [:index, :show], Post
can :show, Comment
# comments_controller.rb
load_and_authorize_resource :organization, :find_by => :permalink
load_and_authorize_resource :membership, :through => :organization
Run Code Online (Sandbox Code Playgroud)
我是未经授权的访问/posts/:post_id/comments,/posts/:post_id/comments/:id等我还在,但是,可以同时访问:index和:show为posts_controller.
如果这些行为的行为不同,这些行为有可能是"别名"吗?
在我的摆弄中,我也遇到了以下情况.更改load_and_authorize_resource为以下允许的访问权限:
# …Run Code Online (Sandbox Code Playgroud) 当引用控制器时,是否可以强制Rails使用短划线( - )而不是下划线.
目前存在一个很好的被Inflector调用函数parameterize.它允许非常好的永久链接删除所有特殊字符并替换为破折号...
但是,当使用具有多个单词的控制器(contact_methods_controller.rb例如)时,您可以定义路线:
resources :contact_methods
Run Code Online (Sandbox Code Playgroud)
这会创建一个到/contact_methods(NOT /contact-methods)的地图.当我混合这两个时,我会得到丑陋的URL:
/contact_methods/1-preferred-email
Run Code Online (Sandbox Code Playgroud)
我想让Rails地图控制器用破折号而不是下划线.我所有的研究都说要分别映射每个控制器:
match 'contact-methods(/:action)' => 'contact_methods'
Run Code Online (Sandbox Code Playgroud)
但在我看来,这真的是愚蠢的,如果我在嵌套资源,它会变得混乱......我不应该将它们定义为自定义路径.是否有ActionDispatch自动重写这些内容的设置?我找不到一个......
我已经看过很多关于这个的帖子,但似乎都没有解决我的问题.我有一个default_scope像这样的模型:
default_scope where(:is_active => true).order('LOWER(table.name)');
Run Code Online (Sandbox Code Playgroud)
我有其他(正常)范围,我想使用创建inactive范围unscoped.我想将它定义为范围,但只有在定义为类方法时才有效:
# works
def self.inactive
unscoped { where(:is_active => false) }
end
# none of these work
scope :inactive, unscoped { where(:is_active => false) }
scope :inactive, with_exclusive_scope { where(:is_active => true) }
scope :inactive, unscoped.where(:is_active => false)
scope :inactive, lambda { unscoped { where(:is_active => false) } }
scope :inactive, unscoped { lambda { where(:is_active => false) } }
unscoped do
scope :inactive, where(:is_active => false)
end
Run Code Online (Sandbox Code Playgroud)
有没有我错过的方法,或者我是否必须使用类方法来定义此范围?
我正在尝试使用ChefSpec测试一个角色.我宁愿不使用Chef Zero(通过要求'chefspec/server'),因为它比ChefSpec自己运行起来要慢一些.
也许我正在阅读文档错误,但看起来不需要Chef Zero来测试角色.但是,我对当前的配置没有任何好运.这是我的测试:
require 'chefspec'
RSpec.configure do |config|
config.cookbook_path = 'C:\projects\chef\cookbooks'
config.role_path = 'C:\projects\chef\roles'
config.log_level = :debug
config.platform = 'ubuntu'
config.version = '12.04'
end
describe 'my_cookbook::default' do
let(:chef_run) do
ChefSpec::Runner.new.converge('role[my_role]')
end
it 'runs without failing' do
expect(chef_run)
end
end
Run Code Online (Sandbox Code Playgroud)
角色(位于角色/ my_role.json):
{
"name": "my_role",
"description": "My role",
"default_attributes": {
},
"run_list": [
"recipe[my_cookbook::default]"
]
}
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,我收到:
NoMethodError: undefined method `run_list_for' for #<Hash:0x4fa3280>
./spec/role_spec.rb:13:in `block (2 levels) in <top (required)>'
./spec/role_spec.rb:17:in `block (2 levels) in <top …Run Code Online (Sandbox Code Playgroud) 我想创建像Facebook这样的推送通知系统.每当Facebook想要告诉你一些事情(比如有人在帖子上发表评论,标记你等)时,你会看到屏幕左下角出现一个小通知.它淡入淡出.
如何使用jQuery和Rails构建这样的系统?它甚至如何工作?JS经常询问服务器,"是否有新的通知?" 或服务器以某种方式推送到这项服务.
现在,如果用户向另一个用户发送消息(例如),我可以向用户的队列添加一条通知,说"你有一条新消息",但是在页面重新加载之前它不会出现...
我在一些当地的流浪汉VM上运行一些Capistrano和Chef任务.我可以通常从命令行SSH到这些框.
当我尝试运行一些Capistrano任务时,我将在第一次运行Cap任务时遇到以下错误:
connection failed for: 192.168.0.220 (Errno::ECONNRESET: Connection reset by peer)
Run Code Online (Sandbox Code Playgroud)
它偶尔会失败第二次.但是当我再次运行它时,它的工作原理!这种行为对我没有任何意义,我对如何进行故障排除没有任何想法.
您可以提供的任何帮助将不胜感激!
我对Ruby很陌生,但过去两周我一直在做大量的厨师测试研究.这个测试使用了ChefSpec和Fauxhai,但它看起来并不是"ruby-ish",我希望社区可以给我一些关于编码风格的指示.有没有更好的方法来编写这样的嵌套循环?
食谱/富/食谱/ default.rb
package "foo" do
action :install
end
Run Code Online (Sandbox Code Playgroud)
食谱/富/规格/ default_spec.rb
require 'chefspec'
describe 'foo::default' do
platforms = {
"debian" => ['6.0.5'],
"ubuntu" => ['12.04', '10.04'],
"centos" => ['5.8', '6.0', '6.3'],
"redhat" => ['5.8', '6.3'],
"mac_os_x" => ['10.6.8', '10.7.4', '10.8.2'],
"windows" => ['2008R2']
}
platforms.each do |platform,versions|
versions.each do |version|
context "on #{platform} #{version}" do
before do
Fauxhai.mock(platform: platform, version: version)
end
it 'should install foo' do
@runner = ChefSpec::ChefRunner.new.converge('foo::default')
@runner.should install_package 'foo'
end
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
欢迎任何和所有反馈.谢谢!
我创建了自定义LWRP,但是当我运行ChefSpec单元测试时.它不知道我的LWRP动作.
这是我的资源:
actions :install, :uninstall
default_action :install
attribute :version, :kind_of => String
attribute :options, :kind_of => String
Run Code Online (Sandbox Code Playgroud)
这是我的提供者:
def whyrun_supported?
true
end
action :install do
version = @new_resource.version
options = @new_resource.options
e = execute "sudo apt-get install postgresql-#{version} #{options}"
@new_resource.updated_by_last_action(e.updated_by_last_action?)
end
Run Code Online (Sandbox Code Playgroud)
这是我的ChefSpec单元测试:
require_relative '../spec_helper'
describe 'app_cookbook::postgresql' do
let(:chef_run) do
ChefSpec::Runner.new do |node|
node.set[:app][:postgresql][:database_user] = 'test'
node.set[:app][:postgresql][:database_password] = 'test'
node.set[:app][:postgresql][:database_name] = 'testdb'
end.converge(described_recipe)
end
it 'installs postgresql 9.1 package' do
expect(chef_run).to run_execute('sudo apt-get install postgresql-9.1 …Run Code Online (Sandbox Code Playgroud) chef-infra ×4
chefspec ×3
ruby ×3
activerecord ×1
algorithm ×1
base64 ×1
cancan ×1
capistrano ×1
connection ×1
encoding ×1
graph ×1
growl ×1
jquery ×1
json ×1
rspec ×1
seo ×1
ssh ×1
unit-testing ×1
url ×1
vagrant ×1