Lチカ開発ブログ

https://l-chika.com/の開発ブログ

Ubuntu 14.04 LTSにYarnをインストール

前提

Ubuntu: 14.04 LTS

参考

https://yarnpkg.com/en/docs/install#linux-tab

インストール

公式の手順どおりにコマンドを実行

$ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
OK
$ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
deb https://dl.yarnpkg.com/debian/ stable main

$ sudo apt-get update && sudo apt-get install yarn
Get:1 https://dl.yarnpkg.com stable InRelease
Get:2 https://dl.yarnpkg.com stable/main amd64 Packages                                                                                                                       
Ign http://us.archive.ubuntu.com trusty InRelease                                                                                                               
Ign http://apt.puppetlabs.com trusty InRelease                                               
Get:3 http://security.ubuntu.com trusty-security InRelease [65.9 kB]  
Get:4 http://us.archive.ubuntu.com trusty-updates InRelease [65.9 kB]            
Get:5 https://dl.yarnpkg.com stable/main i386 Packages                                       
Get:6 http://apt.puppetlabs.com trusty Release.gpg [836 B]                                                          
Get:7 https://dl.yarnpkg.com stable/main Translation-en_US                                   
Get:8 https://dl.yarnpkg.com stable/main Translation-en_US                                                             
Get:9 https://dl.yarnpkg.com stable/main Translation-en_US                                                               
Get:10 https://dl.yarnpkg.com stable/main Translation-en_US                                                                
Get:11 https://dl.yarnpkg.com stable/main Translation-en_US                                                                 
Get:12 https://dl.yarnpkg.com stable/main Translation-en_US                                                                 
Get:13 https://dl.yarnpkg.com stable/main Translation-en_US                                                                 
Get:14 https://dl.yarnpkg.com stable/main Translation-en_US                                                                 
Get:15 https://dl.yarnpkg.com stable/main Translation-en                                                                    
Get:16 https://dl.yarnpkg.com stable/main Translation-en                                                                    
Get:17 https://dl.yarnpkg.com stable/main Translation-en                                                               
Get:18 https://dl.yarnpkg.com stable/main Translation-en                                                                 
Get:19 https://dl.yarnpkg.com stable/main Translation-en                                                                 
Get:20 https://dl.yarnpkg.com stable/main Translation-en                                                                 
Get:21 https://dl.yarnpkg.com stable/main Translation-en                                                                 
Get:22 https://dl.yarnpkg.com stable/main Translation-en                                                                 
Get:23 http://apt.puppetlabs.com trusty Release [54.2 kB]                                                                
Get:24 https://dl.yarnpkg.com stable/main Translation-en_US                                                              
Get:25 https://dl.yarnpkg.com stable/main Translation-en_US                                                              
Get:26 https://dl.yarnpkg.com stable/main Translation-en_US                                                                
Get:27 https://dl.yarnpkg.com stable/main Translation-en_US                                                                
Get:28 https://dl.yarnpkg.com stable/main Translation-en_US                                                                
Get:29 https://dl.yarnpkg.com stable/main Translation-en_US                                                                
Get:30 https://dl.yarnpkg.com stable/main Translation-en_US                                                                
Get:31 https://dl.yarnpkg.com stable/main Translation-en                                                                   
Get:32 https://dl.yarnpkg.com stable/main Translation-en                                                                   
Get:33 https://dl.yarnpkg.com stable/main Translation-en                                                                   
Get:34 http://us.archive.ubuntu.com trusty-backports InRelease [65.9 kB]                                                
Err http://apt.puppetlabs.com trusty Release                                          
  
Get:35 https://dl.yarnpkg.com stable/main Translation-en_US                 
Get:36 https://dl.yarnpkg.com stable/main Translation-en_US                    
Get:37 https://dl.yarnpkg.com stable/main Translation-en_US                    
Get:38 https://dl.yarnpkg.com stable/main Translation-en                       
Get:39 https://dl.yarnpkg.com stable/main Translation-en                   
Get:40 https://dl.yarnpkg.com stable/main Translation-en                   
Hit http://us.archive.ubuntu.com trusty Release.gpg                        
Get:41 http://security.ubuntu.com trusty-security/main Sources [129 kB] 
Get:42 https://dl.yarnpkg.com stable/main Translation-en_US
Get:43 https://dl.yarnpkg.com stable/main Translation-en_US                             
Get:44 https://dl.yarnpkg.com stable/main Translation-en_US                             
Get:45 https://dl.yarnpkg.com stable/main Translation-en                            
Get:46 https://dl.yarnpkg.com stable/main Translation-en                             
Get:47 https://dl.yarnpkg.com stable/main Translation-en                             
Get:48 http://us.archive.ubuntu.com trusty-updates/main Sources [395 kB]             
Get:49 https://dl.yarnpkg.com stable/main Translation-en_US                          
Get:50 https://dl.yarnpkg.com stable/main Translation-en_US       
Get:51 https://dl.yarnpkg.com stable/main Translation-en_US       
Ign https://dl.yarnpkg.com stable/main Translation-en_US                                     
Get:52 https://dl.yarnpkg.com stable/main Translation-en                                     
Get:53 https://dl.yarnpkg.com stable/main Translation-en                                      
Get:54 https://dl.yarnpkg.com stable/main Translation-en                                      
Ign https://dl.yarnpkg.com stable/main Translation-en                                         
Get:55 http://security.ubuntu.com trusty-security/restricted Sources [4,941 B]            
Get:56 http://us.archive.ubuntu.com trusty-updates/restricted Sources [6,327 B]
Get:57 http://security.ubuntu.com trusty-security/universe Sources [51.2 kB]
Get:58 http://us.archive.ubuntu.com trusty-updates/universe Sources [177 kB]
Get:59 http://security.ubuntu.com trusty-security/multiverse Sources [3,196 B]
Get:60 http://us.archive.ubuntu.com trusty-updates/multiverse Sources [7,759 B]
Get:61 http://us.archive.ubuntu.com trusty-updates/main amd64 Packages [972 kB]
Get:62 http://security.ubuntu.com trusty-security/main amd64 Packages [606 kB]
Get:63 http://us.archive.ubuntu.com trusty-updates/restricted amd64 Packages [17.1 kB]
Get:64 http://us.archive.ubuntu.com trusty-updates/universe amd64 Packages [403 kB]           
Get:65 http://security.ubuntu.com trusty-security/restricted amd64 Packages [14.0 kB]
Get:66 http://us.archive.ubuntu.com trusty-updates/multiverse amd64 Packages [14.3 kB]    
Get:67 http://security.ubuntu.com trusty-security/universe amd64 Packages [155 kB]  
Get:68 http://us.archive.ubuntu.com trusty-updates/main i386 Packages [932 kB]
Get:69 http://security.ubuntu.com trusty-security/multiverse amd64 Packages [4,135 B]
Get:70 http://us.archive.ubuntu.com trusty-updates/restricted i386 Packages [16.9 kB]
Get:71 http://security.ubuntu.com trusty-security/main i386 Packages [565 kB]  
Get:72 http://us.archive.ubuntu.com trusty-updates/universe i386 Packages [405 kB]
Get:73 http://security.ubuntu.com trusty-security/restricted i386 Packages [13.8 kB]
Get:74 http://us.archive.ubuntu.com trusty-updates/multiverse i386 Packages [14.6 kB]         
Get:75 http://security.ubuntu.com trusty-security/universe i386 Packages [156 kB]
Get:76 http://us.archive.ubuntu.com trusty-updates/main Translation-en [482 kB]
Get:77 http://security.ubuntu.com trusty-security/multiverse i386 Packages [4,288 B]
Get:78 http://us.archive.ubuntu.com trusty-updates/multiverse Translation-en [7,430 B]    
Get:79 http://security.ubuntu.com trusty-security/main Translation-en [332 kB]             
Get:80 http://us.archive.ubuntu.com trusty-updates/restricted Translation-en [3,975 B]
Get:81 http://security.ubuntu.com trusty-security/multiverse Translation-en [2,201 B]      
Get:82 http://us.archive.ubuntu.com trusty-updates/universe Translation-en [214 kB]
Get:83 http://security.ubuntu.com trusty-security/restricted Translation-en [3,505 B]
Get:84 http://us.archive.ubuntu.com trusty-backports/main Sources [9,712 B]   
Get:85 http://us.archive.ubuntu.com trusty-backports/restricted Sources [28 B]                                                                                                
Get:86 http://security.ubuntu.com trusty-security/universe Translation-en [90.7 kB]                                                                                           
Get:87 http://us.archive.ubuntu.com trusty-backports/universe Sources [35.3 kB]                                                                                               
Get:88 http://us.archive.ubuntu.com trusty-backports/multiverse Sources [1,898 B]                                                                                             
Get:89 http://us.archive.ubuntu.com trusty-backports/main amd64 Packages [13.3 kB]                                                                                            
Get:90 http://us.archive.ubuntu.com trusty-backports/restricted amd64 Packages [28 B]                                                                                         
Get:91 http://us.archive.ubuntu.com trusty-backports/universe amd64 Packages [43.2 kB]                                                                                        
Get:92 http://us.archive.ubuntu.com trusty-backports/multiverse amd64 Packages [1,571 B]                                                                                      
Get:93 http://us.archive.ubuntu.com trusty-backports/main i386 Packages [13.3 kB]                                                                                             
Get:94 http://us.archive.ubuntu.com trusty-backports/restricted i386 Packages [28 B]                                                                                          
Get:95 http://us.archive.ubuntu.com trusty-backports/universe i386 Packages [43.2 kB]                                                                                         
Get:96 http://us.archive.ubuntu.com trusty-backports/multiverse i386 Packages [1,552 B]                                                                                       
Get:97 http://us.archive.ubuntu.com trusty-backports/main Translation-en [7,503 B]                                                                                            
Get:98 http://us.archive.ubuntu.com trusty-backports/multiverse Translation-en [1,215 B]                                                                                      
Get:99 http://us.archive.ubuntu.com trusty-backports/restricted Translation-en [28 B]                                                                                         
Get:100 http://us.archive.ubuntu.com trusty-backports/universe Translation-en [36.8 kB]                                                                                       
Hit http://us.archive.ubuntu.com trusty Release                                                                                                                               
Hit http://us.archive.ubuntu.com trusty/main Sources                                                                                                                          
Hit http://us.archive.ubuntu.com trusty/restricted Sources                                                                                                                    
Hit http://us.archive.ubuntu.com trusty/universe Sources                                                                                                                      
Hit http://us.archive.ubuntu.com trusty/multiverse Sources                                                                                                                    
Hit http://us.archive.ubuntu.com trusty/main amd64 Packages                                                                                                                   
Hit http://us.archive.ubuntu.com trusty/restricted amd64 Packages                                                                                                             
Hit http://us.archive.ubuntu.com trusty/universe amd64 Packages                                                                                                               
Hit http://us.archive.ubuntu.com trusty/multiverse amd64 Packages                                                                                                             
Hit http://us.archive.ubuntu.com trusty/main i386 Packages                                                                                                                    
Hit http://us.archive.ubuntu.com trusty/restricted i386 Packages                                                                                                              
Hit http://us.archive.ubuntu.com trusty/universe i386 Packages                                                                                                                
Hit http://us.archive.ubuntu.com trusty/multiverse i386 Packages                                                                                                              
Hit http://us.archive.ubuntu.com trusty/main Translation-en                                                                                                                   
Hit http://us.archive.ubuntu.com trusty/multiverse Translation-en                                                                                                             
Hit http://us.archive.ubuntu.com trusty/restricted Translation-en                                                                                                             
Hit http://us.archive.ubuntu.com trusty/universe Translation-en                                                                                                               
Ign http://us.archive.ubuntu.com trusty/main Translation-en_US                                                                                                                
Ign http://us.archive.ubuntu.com trusty/multiverse Translation-en_US                                                                                                          
Ign http://us.archive.ubuntu.com trusty/restricted Translation-en_US                                                                                                          
Ign http://us.archive.ubuntu.com trusty/universe Translation-en_US                                                                                                            
Fetched 6,685 kB in 16s (414 kB/s)                                                                                                                                            
Reading package lists... Done
W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: http://apt.puppetlabs.com trusty Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 7F438280EF8D349F

W: Failed to fetch http://apt.puppetlabs.com/dists/trusty/Release  

W: Some index files failed to download. They have been ignored, or old ones used instead.
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Recommended packages:
  nodejs
The following NEW packages will be installed:
  yarn
0 upgraded, 1 newly installed, 0 to remove and 245 not upgraded.
Need to get 2,040 kB of archives.
After this operation, 28.1 MB of additional disk space will be used.
Get:1 https://dl.yarnpkg.com/debian/ stable/main yarn all 0.23.2-1 [2,040 kB]
Fetched 2,040 kB in 0s (4,499 kB/s)
Selecting previously unselected package yarn.
(Reading database ... 88937 files and directories currently installed.)
Preparing to unpack .../archives/yarn_0.23.2-1_all.deb ...
Unpacking yarn (0.23.2-1) ...
Setting up yarn (0.23.2-1) ...

確認

$ yarn --version
0.23.2

Rails Consoleで確認するとConfig(Settings)の更新がされない時

定数を環境毎に管理できるgem config で、config/settings.yml の中身を変更して、Rails Consoleで確認しても反映されない場合がある。

github.com

その場合は、単純にspringが起動していたためだった。

$ ./bin/spring status
Spring is running:

 4475 spring server | xxx | started 57 mins ago                       
 4768 spring app    | xxx | started 22 mins ago | development mode                                                                                                                                                 
$ ./bin/spring stop
Spring stopped.
$ ./bin/spring status
Spring is not running.
$ ./bin/rails c
> Settings.hoge
=> "hoge"

参考

qiita.com

RubyでAWS SNS からiOSにプッシュ通知

サーバーからiOS端末にPUSH通知を送る

前提

  • IAMでSNS権限があるユーザを作成されている
  • バイストークンを取得している
  • SNSでApplicationのArnが作成されている
require 'aws-sdk'
require 'json'

aws_access_key = 'IAMのaccessKeyを設定'
aws_secret_key = 'IAMのsecret_keyを設定'

sns = Aws::SNS::Client.new(
  access_key_id: aws_access_key,
  secret_access_key: aws_secret_key,
  region: 'ap-northeast-1'
)

token = "デバイスのトークン"

# endpointの作成。SNSで設定したApplicationのArn
app_arn = "arn:aws:sns:xxxxx"

resp = sns.create_platform_endpoint(
  platform_application_arn: app_arn,
  token: token
)

apns_payload = {
    'aps' => {
        'alert' => 'ほげほげ',
        'sound' => 'default',
        'content-available' => 1,
    }
}

# Push Notification Platformが
#「Apple Development」の場合は 「APNS_SANDBOX」
#「Apple Ploduction」の場合は「APNS」
message = { "APNS_SANDBOX" => apns_payload.to_json }.to_json


sns.publish(
  target_arn: resp.endpoint_arn,
  message: message,
  message_structure: 'json'
)

参考

Amazon Web Services クラウドネイティブ・アプリケーション開発技法 (Informatics&IDEA)

Amazon Web Services クラウドネイティブ・アプリケーション開発技法 (Informatics&IDEA)

nattoとclassifier-rebornを利用してRubyで文書分類

Pythonの統計・数学ライブラリを利用した文書分類はあるが、Rubyで実現できる方法を模索。

目的

文書(データ) の内容から、いくつかのグループ毎に自動的に分類する

手順

  1. 訓練データを予め作成しておく
  2. 訓練データを形態素解析して、訓練する
  3. 実際のデータを形態素解析して、分類を実行

Gemfile

gem 'natto'
gem 'classifier-reborn'

訓練データ (db/fixtures/train.csv)

内容は、分類, 文章 となる。 - データのバリデーションが多ければ多いほど精度が向上した - 各分類間で訓練データ数に偏りがあるとうまく分類してくれないでの、A,B,C でそれぞれデータ数に偏りがないようにした

"A","ほげほげ..."
"B","ほげほげ..."
"C","ほげほげ..."
"A","ほげほげ..."
"C","ほげほげ..."
...

実装

require 'csv'

namespace :classifier do
  desc 'classify'
  task classify: :environment do

    # 品詞分解して名詞を取得する関数
    mecab = Natto::MeCab.new
    def mecab.filter(text)
      words = []
      parse(text) do |n|
        next unless n.feature.match(/名詞/)
        next if n.feature.match(/(非自立||代名詞)/)
        next if n.surface.match(/\./)
        words << n.surface
      end
      words
    end

    features = ['A', 'B', 'C']
    classifier = ClassifierReborn::Bayes.new(features, auto_categorize: true)

    # 訓練データから学習
    csv = CSV.read('db/fixtures/train.csv')
    csv.each do |data|
      classifier.train(data[0], mecab.filter(data[1]).join(' '))
    end

    # 分類
    result = []
    Product.all.each do |product|
      next if product.description.blank?
      # Product.descriptionカラムの内容から適切なfeatureに分類する
      feature_id = classifier.classify(mecab.filter(product.description).join(' '))
      result << { product_id: product.id, feature_id: feature_id }
    end

    result
  end
end

参考

はじめての自然言語処理

はじめての自然言語処理

実践 機械学習システム

実践 機械学習システム

qiita.com

RubyでMarkdownのImgタグのサイズ指定

Markdownエディタを作成したので、次は表示をつくる。

l-chika.hatenablog.com

前提

以下のGemを利用

実現したい事

![image](http://url.to/image.png = 250x250)

![image](http://url.to/image.png = 250x)

のようなMarkdownを、

<img src="http://url.to/image.png" height="250px" width="250px">
<img src="http://url.to/image.png" height="250px">

と、したい

参考

Resize image in the wiki of GitHub using Markdown - Stack Overflow

実装

Decorator

class HogeDecorator < Draper::Decorator
  ...
  def markdown_content
    markdown = Redcarpet::Markdown.new(CustomRender.new(with_toc_data: true),
                                       autolink: true, space_after_headers: true, tables: true, fenced_code_blocks: true)
    markdown.render(object.content).html_safe
  end
  ...
end
...
class CustomRender < Redcarpet::Render::HTML
  def image(link, title, alt_text)
    if link =~ /(.*)=(\d+)x(\d+|)/
      "<img src='#{$1.strip}' width='#{$2}px' height='#{$3}px' title='#{title}' alt='#{alt_text}'>"
    else
      "<img src='#{link}' title='#{title}' alt='#{alt_text}'>"
    end
  end
end

view

   <div>
      <%= @hoge.markdown_content %>
   </div>

参考

こんなアプローチもあった

ruby on rails - Markdown external image links with Redcarpet - Stack Overflow

Easier Image Markup With Redcarpet

RailsでsimplemdeをつかってMarkdownエディタをつくる

RailsMarkdownのエディタを実装するために、simplemdeを利用する。

github.com

前提

l-chika.hatenablog.com

インストール

npm でフロントのライブラリは管理しているので、npm install でインストール

$ npm install simplemde --save

実装

app/assets/javascripts/application.js

//= require simplemde/dist/simplemde.min.js

app/assets/stylesheets/application.scss

@import 'simplemde/dist/simplemde.min.css';

form

なぜかスペルチェックでエラーが出力されていたので無効にする

<%= form_for(hoge) do |f| %>

  <div class="form-group">
    <%= f.text_area :foo, class: 'form-control', size: '20x15' %>
  </div>

  <div class='row justify-content-md-center'>
    <%= f.submit('投稿', class: 'btn btn-primary btn-lg') %>
  </div>
<% end %>

<script>
  var simplemde = new SimpleMDE({ element: document.getElementById('hoge_foo'), spellChecker: false});
</script>

スクリーンショット

f:id:l-chika:20170308001805p:plain

include_all_helpersについて

include_all_helpersについてのメモ。

以下にあるとおりController名と同名のhelperしか読み込まないように設定をすると、ちょっと厄介なことがある。 qiita.com

HelperをもつGemを利用する場合。例えば、font-awesome-railsgithub.com

課題

  1. include_all_helpers でhelperの読み込みに制限をかける
  2. font-awesome-rails no fa_icon ヘルパーを利用
  3. エラーが発生

config/application.rb

module Hoge
  class Application < Rails::Application
    ...
    config.action_controller.include_all_helpers = false
  end
end

view

<%= fa_icon 'envelope-o' %>

エラー

NoMethodError - undefined methodfa_icon' for …` が発生

解決

application_helperfont-awesome-railsinclude をすれば良い。

app/helpers/application_helper.rb

module ApplicationHelper
  include FontAwesome::Rails::IconHelper
  ...

ただGemのHelperを利用する度に、このを設定していくのは面倒なので、ひとまず include_all_helpers = false はやめる。