Checking for Valid UK PostCode - firebird2.5

I have some code which is written in mysql (i think) to return valid postcodes, and I am trying to convert the code to Firebird SQL but I am getting errors. Stopping at the CASE part at the minute. What I want to happen eventually is for it to delete all non-valid postcodes. So if you know an another solution that would be of great help:
SET TERM ^ ;
CREATE PROCEDURE ValidatePostCode ( PostCode VARCHAR(8) )
RETURNS ( ValidPC CHAR(1) )
AS
BEGIN
RETURN CASE
-- Special case GIR 0AA
WHEN PostCode LIKE 'GIR 0AA' THEN i=1
-- Current postcode prefixes
WHEN LEFT(Postcode, 2) NOT IN ('AB', 'AL', 'BA', 'BB', 'BD', 'BH', 'BL', 'BN', 'BR', 'BS', 'BT', 'CA', 'CB', 'CF', 'CH', 'CM', 'CO', 'CR', 'CT', 'CV', 'CW', 'DA', 'DD', 'DE', 'DG', 'DH', 'DL', 'DN', 'DT', 'DY', 'EC', 'EH', 'EN', 'EX', 'FK', 'FY', 'GL', 'GU', 'GY', 'HA', 'HD', 'HG', 'HP', 'HR', 'HS', 'HU', 'HX', 'IG', 'IM', 'IP', 'IV', 'JE', 'KA', 'KT', 'KW', 'KY', 'L', 'LA', 'LD', 'LE', 'LL', 'LN', 'LS', 'LU', 'ME', 'MK', 'ML', 'NE', 'NG', 'NN', 'NP', 'NR', 'NW', 'OL', 'OX', 'PA', 'PE', 'PH', 'PL', 'PO', 'PR', 'RG', 'RH', 'RM', 'SA', 'SE', 'SG', 'SK', 'SL', 'SM', 'SN', 'SO', 'SP', 'SR', 'SS', 'ST', 'SW', 'SY', 'TA', 'TD', 'TF', 'TN', 'TQ', 'TR', 'TS', 'TW', 'UB', 'WA', 'WC', 'WD', 'WF', 'WN', 'WR', 'WS', 'WV', 'YO', 'ZE')
OR WHEN LEFT(Postcode, 1) NOT IN ('B', 'E', 'G', 'L', 'N', 'S', 'W') THEN 0
-- AANN NAA
WHEN PostCode LIKE '[ABCDEFGHIJKLMNOPRSTUWYZ][ABCDEFGHKLMNOPQRSTUVWXY][0123456789][0123456789] [0123456789][ABDEFGHJLNPQRSTUWXYZ][ABDEFGHJLNPQRSTUWXYZ]' THEN 1
-- AANA NAA
WHEN PostCode LIKE '[ABCDEFGHIJKLMNOPRSTUWYZ][ABCDEFGHKLMNOPQRSTUVWXY][0123456789][ABEHMNPRVWXY] [0123456789][ABDEFGHJLNPQRSTUWXYZ][ABDEFGHJLNPQRSTUWXYZ]' THEN 1
-- ANN NAA
WHEN PostCode LIKE '[ABCDEFGHIJKLMNOPRSTUWYZ][0123456789][0123456789] [0123456789][ABDEFGHJLNPQRSTUWXYZ][ABDEFGHJLNPQRSTUWXYZ]' THEN 1
-- AAN NAA
WHEN PostCode LIKE '[ABCDEFGHIJKLMNOPRSTUWYZ][ABCDEFGHKLMNOPQRSTUVWXY][0123456789] [0123456789][ABDEFGHJLNPQRSTUWXYZ][ABDEFGHJLNPQRSTUWXYZ]' THEN 1
-- ANA NAA
WHEN PostCode LIKE '[ABCDEFGHIJKLMNOPRSTUWYZ][0123456789][ABCDEFGHJKSTUW] [0123456789][ABDEFGHJLNPQRSTUWXYZ][ABDEFGHJLNPQRSTUWXYZ]' THEN 1
-- AN NAA
WHEN PostCode LIKE '[ABCDEFGHIJKLMNOPRSTUWYZ][0123456789] [0123456789][ABDEFGHJLNPQRSTUWXYZ][ABDEFGHJLNPQRSTUWXYZ]' THEN 1
-- Not a valid postcode
ELSE 0
END
END^
SET TERM ; ^

I ended up doing the following thanks to useful comment:
select postcode from CUSTOMER_ADDRESSES
where LEFT(postcode, 2) NOT IN ('AB', 'AL', 'BA', 'BB', 'BD', 'BH', 'BL', 'BN', 'BR', 'BS', 'BT', 'CA', 'CB', 'CF', 'CH', 'CM', 'CO', 'CR', 'CT', 'CV', 'CW', 'DA', 'DD', 'DE', 'DG', 'DH', 'DL', 'DN', 'DT', 'DY', 'EC', 'EH', 'EN', 'EX', 'FK', 'FY', 'GL', 'GU', 'GY', 'HA', 'HD', 'HG', 'HP', 'HR', 'HS', 'HU', 'HX', 'IG', 'IM', 'IP', 'IV', 'JE', 'KA', 'KT', 'KW', 'KY', 'L', 'LA', 'LD', 'LE', 'LL', 'LN', 'LS', 'LU', 'ME', 'MK', 'ML', 'NE', 'NG', 'NN', 'NP', 'NR', 'NW', 'OL', 'OX', 'PA', 'PE', 'PH', 'PL', 'PO', 'PR', 'RG', 'RH', 'RM', 'SA', 'SE', 'SG', 'SK', 'SL', 'SM', 'SN', 'SO', 'SP', 'SR', 'SS', 'ST', 'SW', 'SY', 'TA', 'TD', 'TF', 'TN', 'TQ', 'TR', 'TS', 'TW', 'UB', 'WA', 'WC', 'WD', 'WF', 'WN', 'WR', 'WS', 'WV', 'YO', 'ZE') OR LEFT(postcode, 1) NOT IN ('B', 'E', 'G', 'L', 'N', 'S', 'W')
and postcode not SIMILAR TO '[ABCDEFGHIJKLMNOPRSTUWYZ][ABCDEFGHKLMNOPQRSTUVWXY][0123456789][0123456789][0123456789][ABDEFGHJLNPQRSTUWXYZ][ABDEFGHJLNPQRSTUWXYZ]'
and postcode not SIMILAR TO '[ABCDEFGHIJKLMNOPRSTUWYZ][ABCDEFGHKLMNOPQRSTUVWXY][0123456789][ABEHMNPRVWXY][0123456789][ABDEFGHJLNPQRSTUWXYZ][ABDEFGHJLNPQRSTUWXYZ]'
and postcode not SIMILAR TO '[ABCDEFGHIJKLMNOPRSTUWYZ][0123456789][0123456789][0123456789][ABDEFGHJLNPQRSTUWXYZ][ABDEFGHJLNPQRSTUWXYZ]'
and postcode not SIMILAR TO '[ABCDEFGHIJKLMNOPRSTUWYZ][ABCDEFGHKLMNOPQRSTUVWXY][0123456789][0123456789][ABDEFGHJLNPQRSTUWXYZ][ABDEFGHJLNPQRSTUWXYZ]'
and postcode not SIMILAR TO '[ABCDEFGHIJKLMNOPRSTUWYZ][0123456789][ABCDEFGHJKSTUW][0123456789][ABDEFGHJLNPQRSTUWXYZ][ABDEFGHJLNPQRSTUWXYZ]'
and postcode not SIMILAR TO '[ABCDEFGHIJKLMNOPRSTUWYZ][0123456789][0123456789][ABDEFGHJLNPQRSTUWXYZ][ABDEFGHJLNPQRSTUWXYZ]'
Before doing this however I removed all the spaces as they were not needed. If they were needed I would have put a space in the similar to string. Here is the code I used to remove the spaces:
update CUSTOMER_ADDRESSES set POSTCODE = replace(POSTCODE, ' ', '');

Related

How to check the RegionRestriction of a Channel using youtube API

Is there any option exists in the Youtube API to check whether a channel is blocked in a country. For Video, in the API there is the item value " Region Restriction" {'blocked':['IN']}. But nothing for the Channel so far. Want to check whether a channel is restricted or not using the Youtube API
The solution can be found on the JSON of the video in question. To find the countries where the video is unavailable, it searches for the term "regionRestriction" in the JSON of the video. Here is the code I built which allows you to display the countries (with Python):
import requests,json
api_key = 'tap_your_api_code'
url = f"https://www.googleapis.com/youtube/v3/videos?id=mCChxHZXacA&part=contentDetails&key={api_key}"
dictio_data = json.loads(requests.get(url).text)
contentDetails_json = dictio_data["items"][0]["contentDetails"]["regionRestriction"]["blocked"]
print(contentDetails_json)
Result:
['AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AO', 'AR', 'AS', 'AT', 'AU', 'AW', 'AX', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BM', 'BN', 'BO', 'BR', 'BS', 'BT', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU', 'CV', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GF', 'GG', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IM', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JE', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'ME', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RS', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'ST', 'SV', 'SY', 'SZ', 'TC', 'TD', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO', 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN', 'VU', 'WF', 'WS', 'YE', 'YT', 'ZA', 'ZM', 'ZW']

How to pass the phone number from Intl-Tel-Input to an input on Ruby-on-rails?

I have a simple_form with a phone number. I'm using Intl-Tel-Input to get the entire phone_number and confirm SMS in different countries (twilio). In console.log, I succeed to display the entire number. When I submit, I just get the phone_number without the country indicator (dial_code)
I read the doc, tried to get it by other way (.value, .data, etc...), but impossible to save the entire phone_number (for example, +33612345678, I just get 61234567).
Here's my code :
<div class="userphonenumber">
<%= f.input :phone_number, label: 'N° de téléphone', required: true, autofocus: true ,
input_html: { autocomplete: "intlNumber" }, wrapper_html: { id: 'userphonenumber' }%>
</div>
<script>
$("#user_phone_number").intlTelInput({
formatOnInit: true,
separateDialCode: true,
onlyCountries: ['fr', 'at', 'be', 'bg', 'cz', 'dk', 'de', 'ee', 'ie', 'el', 'es', 'hr', 'it', 'cy', 'lv', 'lt', 'lu', 'hu', 'mt', 'nl', 'pl', 'pt', 'ro', 'si', 'sk', 'fi', 'se', 'uk'],
initialCountry: "fr"
});
const flag = document.querySelector(".flag-container")
flag.addEventListener("click", () => {
var intlNumber = $("#user_phone_number").intlTelInput("getNumber");
console.log("intlNumber", intlNumber)
var input = document.querySelector("#user_phone_number");
window.intlTelInput(input);
});
</script>
I'm expecting to get +33612345678 to use the phone_number with twilio. Beginner on RoR and JS. Did I miss something ?
Thanks for your help.
Finally fix with this solution :
<script>
$("#user_phone_number").intlTelInput({
formatOnInit: true,
separateDialCode: true,
onlyCountries: ['at', 'be', 'bg', 'cz', 'dk', 'de', 'ee', 'ie', 'el', 'es', 'fr', 'hr', 'it', 'cy', 'lv', 'lt', 'lu', 'hu', 'mt', 'nl', 'pl', 'pt', 'ro', 'si', 'sk', 'fi', 'se', 'uk'],
initialCountry: "fr",
});
var phone = document.getElementById('user_phone_number')
phone.addEventListener('keyup', (event) => {
console.log(event)
});
$("form").submit(function() {
$('#user_phone_number').val($(phone).intlTelInput("getNumber"));
});
</script>
$(".form").submit( function(eventObj) {
$("<input />").attr("type", "hidden")
.attr("name", "ccode")
.attr("value", $(this).find(".selected-dial-code").text())
.appendTo(".form");
return true;
});

Ransack: how to join table multiple times with different alias?

Suppose I have :items with a has_many association with :properties, then I can search for all items that have a property with name 'a_name' and value 'a_value' like this
q: { properties_name_eq: 'a_name', properties_value_eq: 'a_value' }
Now what if I want to search for all items that have a property with name 'a_name' and value 'a_value' and also a property with name 'another_name' and value 'another_value'?
The following doesn't work as it joins the properties table only once
q: {
g: {
'0' => { properties_name_eq: 'a_name', properties_value_eq: 'a_value' },
'1' => { properties_name_eq: 'another_name', properties_value_eq: 'another_value'}
}
}
The generated SQL looks something like this
SELECT DISTINCT "items".* FROM "items"
LEFT OUTER JOIN "properties" ON "properties"."item_id" = "items"."id"
INNER JOIN ((SELECT "items".* FROM "items")) AS sel_111 on sel_111.id
WHERE
(("properties"."name" = 'a_name' AND "properties"."value" = 'a_value') AND ("properties"."name" = 'another_name' AND "properties"."value" = 'another_value'))
EDIT:
To make it more clear what I am after, I'll paste a spec below.
Item.create name: 'ab', properties_attributes: [{ name: 'a', value: 'a1'}, {name: 'b', value: 'b1'}]
Item.create name: 'a', properties_attributes: [{ name: 'a', value: 'a1'}]
Item.create name: 'b', properties_attributes: [{name: 'b', value: 'b1'}]
Item.create name: 'ax', properties_attributes: [{ name: 'a', value: 'a1'}, {name: 'b', value: 'x'}]
Item.create name: 'bx', properties_attributes: [{ name: 'a', value: 'x'}, {name: 'b', value: 'b1'}]
Item.create name: 'other', properties_attributes: [{ name: 'other', value: '123'}]
get :index, q: { properties_name_eq: 'a', properties_value_eq: 'a1' }
names = JSON.parse(response.body).map{|u| u['name']}
expect(names).to match_array ['ab', 'a', 'ax'] # OK!
get :index,
q: {
m: 'or',
g: {
'0' => { properties_name_eq: 'a', properties_value_eq: 'a1' },
'1' => { properties_name_eq: 'b', properties_value_eq: 'b1'}
}
}
names = JSON.parse(response.body).map{|u| u['name']}
expect(names).to match_array ['ab'] #FAILS!
Just use Model.search(params[:q].try(:merge, m: 'or')), using your example:
q: {
m: 'or',
g: {
'0' => { properties_name_eq: 'a_name', properties_value_eq: 'a_value' },
'1' => { properties_name_eq: 'another_name', properties_value_eq: 'another_value'}
}
}
You can find more information here
You need an or at the where level of your query, because properties.name can't be equal 'a_name' and 'another_name' at the same time. A second alias for the table is not required.
You can solve this by using multiple queries.
For each name + value property, get all item IDs with this property
Intersect the resulting IDs for each property into item_ids
In the final query on :items, add the clause WHERE id IN (item_ids)
Here's a code example that does steps 1 & 2:
def property_item_ids(conditions)
conditions.inject([]) do |result, (key, condition)|
result.method(result.empty? ? '+' : '&').(Property.ransack(m: "and", g: condition).pluck(:item_id).to_a)
end
end
Get the item IDs that have all properties:
conditions = {
'0' => { properties_name_eq: 'a', properties_value_eq: 'a1' },
'1' => { properties_name_eq: 'b', properties_value_eq: 'b1'}
}
item_ids = property_item_ids(conditions)
For step 3, invoke ransack with item_ids:
q: {
m: 'and',
g: {
'0' => { item_id_in: item_ids }
}
}

Twitter's typeahead

I am trying to add twitter's typeahead plugin to my rails application. I have downloaded the typeahead.bundle.js and placed it in vendor/assets/javascripts directory. And I have added //= require typeahead.bundle to app/assets/javascripts/application.js. Then I put the first example given there
<div id="the-basics">
<input class="typeahead" type="text" placeholder="States of USA">
</div>
on one of my views and the following javascript on
app/assets/javascripts/example.js
but nothing happens when i try to type characters, not even on the logs. Thanks for your help
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substrRegex;
matches = [];
substrRegex = new RegExp(q, 'i');
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
matches.push({ value: str });
}
});
cb(matches);
};
};
var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];
$('#the-basics .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'states',
displayKey: 'value',
source: substringMatcher(states)
});

Dictionary-based NLTK tagger

I want to tag location string in text using NLTK and also in Stanford-NLP
and am looking for dictionary lookup tagger for NLTK/Stanford-NLP, for so far I haven't found anything with Dictionary-lookup method.
One way is to use RegexpTagger(NLTK) and supply every location strings in there, but it might slow.
I don't need to do any semantic analysis, other than to tag the locations based on my location-dictionary.
Ideas ?
You could use UnigramTagger:
#!/usr/bin/env python2
from nltk.tag.sequential import UnigramTagger
from nltk.tokenize import word_tokenize, sent_tokenize
text = 'I visited Paris and Bordeaux. Not Los Angeles'
locations = [[('Paris', 'LOC'), ('Bordeaux', 'LOC'), ('France', 'LOC'),
('Los Angeles', 'LOC')]]
location_tagger = UnigramTagger(locations)
for sentence in sent_tokenize(text):
tokens = word_tokenize(sentence)
print(location_tagger.tag(tokens))
Prints:
[('I', None), ('visited', None), ('Paris', 'LOC'), ('and', None),
('Bordeaux', 'LOC'), (',', None), ('but', None), ('not', None),
('Los', None), ('Angeles', None)]
You will need a better tokenizer if you want to tag multi-word locations like Los Angeles.
If all you need is to look up from dictionaries, then htql.RegEx() may be a good fit. Here is the example from http://htql.net:
import htql;
address = '88-21 64th st , Rego Park , New York 11374'
states=['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut',
'Delaware', 'District Of Columbia', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana',
'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan',
'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma',
'Oregon', 'PALAU', 'Pennsylvania', 'PUERTO RICO', 'Rhode Island', 'South Carolina', 'South Dakota',
'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin',
'Wyoming'];
a=htql.RegEx();
a.setNameSet('states', states);
state_zip1=a.reSearchStr(address, "&[s:states][,\s]+\d{5}", case=False)[0];
# state_zip1 = 'New York 11374'
state_zip2=a.reSearchList(address.split(), r"&[ws:states]<,>?<\d{5}>", case=False)[0];
# state_zip2 = ['New', 'York', '11374']
You can use parameter: useindex=True to return matching positions.

Resources