Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions lib/chinese_permalink.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ def self.included(base)
class_attribute :permalink_attrs, :permalink_field, :before_methods, :after_methods
end
base.extend ClassMethods
base.include InstanceMethods
end

private

def create_permalink
if self.permalink.nil?
if self.permalink.nil? || self.permalink.blank?
chinese_permalink = self.class.permalink_attrs.collect do |attr_name|
chinese_value = self.send(attr_name)
end * '-'
Expand All @@ -23,11 +25,21 @@ def create_permalink
english_permalink = self.send(method, english_permalink)
end

english_permalink = remove_duplicate_dash(remove_heading_dash(remove_tailing_dash(remove_non_ascii(remove_space(remove_punctuation(english_permalink)))))).downcase
self.update_attribute(self.class.permalink_field, english_permalink)
english_permalink = format_process(english_permalink)
self.update_column(:"#{self.class.permalink_field}", english_permalink)
end
end

def try_create_permalink
create_permalink
rescue => e
# do nothing
end

def format_process(text)
remove_duplicate_dash(remove_heading_dash(remove_tailing_dash(remove_non_ascii(remove_space(remove_punctuation(text)))))).downcase
end

def remove_heading_dash(text)
text.gsub(/^-+/, '')
end
Expand Down Expand Up @@ -60,25 +72,35 @@ def chinese_permalink(attr_names, options = {})
self.before_methods = Array(options[:before_methods])
self.after_methods = Array(options[:after_methods])

after_save :create_permalink
after_save :try_create_permalink
end
end

module InstanceMethods
def sanitize_format(text)
format_process(text)
end
end

class Translate
class <<self
def t(text)
response = Net::HTTP.get(URI.parse(URI.encode(translate_url + text)))
response = Net::HTTP.get(URI.parse(URI.encode(translator_endpoint + text)))
response =~ %r|<string.*?>(.*?)</string>|
$1.to_s
end

def translate_url
@translate_url ||= begin
config = YAML.load(File.open(File.join(Rails.root, "config/chinese_permalink.yml")))
app_id = config['bing']['app_id']
language = config['bing']['language']
"http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=#{app_id}&from=#{language}&to=en&text="
end
def translator_endpoint
"https://api.microsofttranslator.com/V2/Http.svc/Translate?appId=#{authorization_token}&to=en&text="
end

def authorization_token
config = YAML.load(File.open(File.join(Rails.root, "config/chinese_permalink.yml")))
access_token = config['microsoft']['key']
access_token_endpoint = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken?Subscription-Key=#{access_token}"
response = Net::HTTP.post_form(URI(access_token_endpoint), {})

response.code == "200" ? "Bearer #{response.body}" : nil
end
end
end
Expand Down