From tire gem's DSL example the following code was given.
index = Tire.index('weather') do
delete
create
# First, a query named _warning_,
register_percolator_query('warning', :tags => ['warning']) { string 'warning OR severe OR extreme' }
# a query named _tsunami_,
register_percolator_query('tsunami', :tags => ['tsunami']) { string 'tsunami' }
end
matches = index.percolate(:message => '[Warning] Extreme flooding expected after tsunami wave.')
I was wondering how can we set the matching criteria to a set of terms instead of ORed string.
For instance string 'warning weather OR severe weather OR extreme weather' would match warning or weather and not both warning weather together.
I am studying ElasticSearch because I will use it soon, but I did not use it yet so I can try and share my thin knowledge :)
Please check their doc http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query/
And look at the default_operator setting, I guess you need to set it to AND
I'm using Rails.logger.debug print variables for debugging purposes. The issue is it prints hashes in an impossible to read format (can't distinguish keys from values). For example, I add the following lines to my code base
#code_base.rb
my_hash = {'a' => 'alligator', 'b'=>'baboon'}
Rails.logger.debug my_hash
Then I launch my rails app and type
tail -f log/development.log
But when my_hash gets printed, it looks like
bbaboonaalligator
The key and values are scrunched up, making it impossible to parse. Do you guys know what I should do to fix this?
Nevermind, I found the answer to my own question. I need to use
my_hash = {'a' => 'alligator', 'b'=>'baboon'}
Rails.logger.debug "#{my_hash.inspect}"
Then, it looks like
{"b"=>"baboon", "a"=>"aligator"}
It's even easier to read it when you use to_yaml eg:
logger.debug my_hash.to_yaml
Which is an easy to read format over multiple lines. The inspect method simply spews out a string.
my_hash = {'a' => 'alligator', 'b'=>'baboon'}
logger.debug "#{my_hash}"
Then, it looks like
{"b"=>"baboon", "a"=>"aligator"}
do not need inspect
There is an another way to do this. There is a ruby built in module pp.rb that is Pretty-printer for Ruby objects.
non-pretty-printed output by p is:
#<PP:0x81fedf0 #genspace=#<Proc:0x81feda0>, #group_queue=#<PrettyPrint::GroupQueue:0x81fed3c #queue=[[#<PrettyPrint::Group:0x81fed78 #breakables=[], #depth=0, #break=false>], []]>, #buffer=[], #newline="\n", #group_stack=[#<PrettyPrint::Group:0x81fed78 #breakables=[], #depth=0, #break=false>], #buffer_width=0, #indent=0, #maxwidth=79, #output_width=2, #output=#<IO:0x8114ee4>>
pretty-printed output by pp is:
#<PP:0x81fedf0
#buffer=[],
#buffer_width=0,
#genspace=#<Proc:0x81feda0>,
#group_queue=
#<PrettyPrint::GroupQueue:0x81fed3c
#queue=
[[#<PrettyPrint::Group:0x81fed78 #break=false, #breakables=[], #depth=0>],
[]]>,
#group_stack=
[#<PrettyPrint::Group:0x81fed78 #break=false, #breakables=[], #depth=0>],
#indent=0,
#maxwidth=79,
#newline="\n",
#output=#<IO:0x8114ee4>,
#output_width=2>
For more complex objects even with ActiveRecord, this could be achieved with a JSON.pretty_generate
Rails.logger.debug JSON.pretty_generate(my_hash.as_json)
I'm using a gem called Desk to return data from desk.com's API. When I run Desk.customers(:custom_external_id => temp.id) it returns a lovely set of data.
I'm looking for the integer 71095620 from id=71095620.
=> #<Hashie::Rash count=20 page=1 results=[#<Hashie::Rash customer=#<Hashie::Rash addresses=[] custom_external_id="58749" emails=[#<Hashie::Rash email=#<Hashie::Rash created_at="2013-02-13T15:59:26-08:00" email="CENSORED" i
d=33622514 updated_at="2013-02-13T15:59:26-08:00" verified_at=nil>>] first_name="CENSORED" id=68712186 language=nil last_name="CENSORED" phones=[#<Hashie::Rash phone=#<Hashie::Rash created_at="2013-02-13T16:00:45-08:00" id=1301079 phone="CENSORED" updated_at="2013-02-13T16:00:45-08:00">>] twitters=[nil]>>, #<Hashie::Rash customer=#<Hashie::Rash addresses=[] custom_external_id="58749" emails=[] first_name="CENSORED" id=71095620 language=nil last_name="CENSORED" phones=[] twitters=[nil]>>] total=2>
There are obviously multiple id fields all over this and i'm not sure which way is best to extract that integer. Thanks in advance!
According to the Hasie::Rash readme, You can probably do this
response = Desk.customers(:custom_external_id => temp.id)
response.results[1].customer.id
I'm using stringscanner on my request URL in order to get the name of the user's currently selected category, but I've been having difficulty dealing with spaces and special characters.
request.url.scan(/\?category=\w+/).to_s.gsub('?category=', '')
URL examples followed by result
http://localhost:3000/search?category=dog&search=&utf8=%E2%9C%93 => ["dog"]
http://localhost:3000/search?category=dog.com&search=&utf8=%E2%9C%93 => ["dog"]
http://localhost:3000/search?category=dog+cat&search=&utf8=%E2%9C%93 => ["dog"]
I'm trying to get ["dog"] ["dog.com"] and ["dog cat"], but am currently stuck. Any ideas?
Note: Considering removing spaces from categories and replacing them with dashes as multiple spaces could be problematic, but if it's possible to create one function to rule them all, that would be awesome.
This is Rails, is there a reason you're not just using params[:category]?
If you are trying to extract params then you could use parse_query :
uri = "http://localhost:3000/search?category=dog+cat&search=&utf8=%E2%9C%93"
result = Rack::Utils.parse_query(URI(uri).query) #=> {"category"=>"dog cat", "search"=>"", "utf8"=>"\xE2\x9C\x93"}
result["category"] #=> dog cat
I have found several websites pointing to using the following code to add support for custom parameter formats:
ActionController::Base.param_parsers[Mime::PLIST] = lambda do |body|
str = StringIO.new(body)
plist = CFPropertyList::List.new({:data => str.string})
CFPropertyList.native_types(plist.value)
end
This one here is for the Apple plist format, which is what I am looking to do. However, using Rails 3.2.1, The dev server won't start, saying that param_parsers is undefined. I cannot seam to find any documentation for it being deprecated or any alternative to use, just that it is indeed included in the 2.x documentation and not the 3.x documentation.
Is there any other way in Rails 3 to support custom parameter formats in POST and PUT requests?
The params parsing moved to a Rack middleware. It is now part of ActionDispatch.
To register new parsers, you can either redeclare the use of the middleware like so:
MyRailsApp::Application.config.middleware.delete "ActionDispatch::ParamsParser"
MyRailsApp::Application.config.middleware.use(ActionDispatch::ParamsParser, {
Mime::PLIST => lambda do |body|
str = StringIO.new(body)
plist = CFPropertyList::List.new({:data => str.string})
CFPropertyList.native_types(plist.value)
end
})
or you can change the constant containing the default parsers like so
ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime::PLIST] = lambda do |body|
str = StringIO.new(body)
plist = CFPropertyList::List.new({:data => str.string})
CFPropertyList.native_types(plist.value)
end
The first variant is probably the cleanest. But you need to be aware that the last one to replace the middleware declaration wins there.