#ruby gemfile bundler
Explore tagged Tumblr posts
govindhtech · 1 year ago
Text
AWS CodeArtifact: Secure Your Software Supply Chain
Tumblr media
AWS CodeArtifact Documentation
CodeArtifact
AWS CodeArtifact is now available for Ruby developers to safely store and retrieve their gems. CodeArtifact is compatible with bundler and gem, two common developer tools.
Numerous packages are frequently used by applications to expedite development by offering reusable code for frequent tasks including data manipulation, network access, and cryptography. In order to access distant services, developers can also include SDKs, such the AWS SDKs. These packages could originate from outside sources like open source initiatives or from other departments inside your company. The management of dependencies and packages is essential to software development. Ruby developers commonly utilise gem and bundler, although other languages such as Java, C#, JavaScript, Swift, and Python have tools for fetching and resolving dependencies.
Nevertheless, there are security and legal issues when employing third-party software. It is imperative for organisations to verify that package licences align with their projects and do not infringe against intellectual property rights. It is imperative that they confirm the safety of the supplied code and rule out any potential vulnerabilities that could lead to a supply chain assault. Organisations usually employ private package servers to overcome these issues. Only packages approved by legal and security departments and accessible through private repositories may be used by developers.
With the managed service AWS CodeArtifact, packages may be safely distributed to internal development teams without requiring infrastructure management. In addition to npm, PyPI, Maven, NuGet, SwiftPM, and generic formats, CodeArtifact now supports Ruby gems.
Using already-existing technologies like gem and bundler, you may publish and download Ruby gem dependencies from your CodeArtifact repository on the AWS Cloud. Packages can be referenced in your Gemfile after being stored in AWS CodeArtifact. During the build process, your build system will then download approved packages from the CodeArtifact repository.
Keep and distribute artefacts among accounts, granting your teams and building systems the proper amount of access. Use a fully managed service to cut down on the overhead associated with setting up and maintaining an artefact server or infrastructure. Pay as you go for software packages, requests performed, and data moved outside of the region; you only pay for what you use.
How AWS CodeArtifacts functions
Using well-known package managers and build tools like Maven, Gradle, npm, Yarn, Twine, pip, NuGet, and SwiftPM, you may save artefacts using AWS CodeArtifact. To give you access to the most recent iterations of application dependencies, AWS CodeArtifact has the capability to automatically fetch software packages from public package repositories on demand.
Features of AWS CodeArtifacts
Any size organisation can securely store, publish, and distribute software packages used in software development with AWS CodeArtifact, a fully managed artefact repository service.
Consume public artefact repository packages
With a few clicks, CodeArtifact may be configured to retrieve software packages from public repositories like NuGet.org, Maven Central, PyPI, and the npm Registry. Your developers and CI/CD systems can always get the application dependencies they need since CodeArtifact automatically downloads and saves them from these repositories.
Release and distribute packages
You can publish packages created within your company using the package managers you already have, such npm, pip, yarn, twine, Maven, NuGet, and SwiftPM. Instead of building their own packages, development teams can save time by fetching packages published to and shared in a single organisational repository.
Approve a package’s use and observe its use
CodeArtifact APIs and AWS EventBridge can be used to create automated procedures that approve packages for use. By integrating with AWS CloudTrail, leaders can easily discover packages that require updating or removal by having visibility into which packages are being used and where.
High availability and robustness
AWS CodeArtifact uses Amazon S3 and Amazon DynamoDB to store artefact data and metadata, and it functions in different Availability Zones. Your encrypted data is extremely available and highly durable since it is redundantly stored across many facilities and various devices inside each facility.
Make use of a completely managed service
With CodeArtifact, you can concentrate on providing for your clients rather than setting up and managing your development environment. A highly available solution that can grow to accommodate any software development team’s demands is CodeArtifact. There are no servers to maintain or software updates to do.
Turn on monitoring and access control
Amazon CodeArtifact gives you visibility into who has access to your software packages and control over who can access them thanks to its integrations with AWS CloudTrail and IAM. For package encryption, CodeArtifact additionally interfaces with AWS Key Management Service (KMS).
Package access inside a VPC
By configuring AWS CodeArtifact to use AWS PrivateLink endpoints, you can improve the security of your repositories. This prevents data from being sent over the open internet and enables devices operating within your VPC to access packages stored in CodeArtifact.
CodeArtifact Use cases
Obtain software packages whenever needed. Set up CodeArtifact to retrieve content from publicly accessible repositories, including NuGet, Maven Central, Python Package Index (PyPI), and npm Registry.
Release and distribute packages
By publishing to a central organisational repository, you can safely distribute private products throughout organisations.
Accept bundles and check use
Using CodeArtifact APIs and Amazon EventBridge, create automated review processes. AWS CloudTrail provides package visibility.
Use packages in automated builds, and publish them
Update your private packages securely with IAM and publish new versions by pulling dependencies from CodeArtifact in AWS CodeBuild.
Cost and accessibility
The CodeArtifact fees for Ruby packages are identical to those of the currently supported other package formats. Three criteria determine CodeArtifact’s billing: storage (measured in gigabytes per month), requests, and data transferred to and from other AWS regions or the internet. You can perform your continuous integration and delivery (CI/CD) operations on Amazon Elastic Compute Cloud (Amazon EC2) or AWS CodeBuild, for example, without paying for the CodeArtifact data transfer because data transfer to AWS services in the same Region is free. The information is on the pricing page as usual.
Read more on govindhtech.com
0 notes
asolodigitalnomad · 5 years ago
Text
Code of the day
source 'https://rubygems.org' gem 'nokogiri' gem 'rack', '~> 2.0.1' gem 'rspec'
0 notes
soonthornpen · 2 years ago
Text
Using Jekyll
1. Install Ruby  (Ruby+Devkit)
 at https://rubyinstaller.org/downloads/ (Windows)  follow instruction here (https://jekyllrb.com/docs/installation/windows)
2. Install jekyll  
gem install bundler jekyll  
3. Create jekyll project.
jekyll new my-awesome-site  
4. Run
Go to folder.
cd my-awesome-site  
Run server.
bundle exec jekyll serve
Now browse to http://localhost:4000
Generated site locate inside the “_site” folder.
source: jekyll
5. Install Jekyll Admin
Add the following to your site's Gemfile in  jekyll_plugins
gem 'jekyll-admin'
Run 
bundle install
Start server again.
bundle exec jekyll serve
Go to address.
http://localhost:4000/admin
Source : Jakyll Admin
6. host images in Cloudnary.com
You can host and get url for using in jekyll admin
https://cloudinary.com/
0 notes
michaelbumann · 7 years ago
Text
bundle thank-you - a donation system for ruby gems (and other package managers)
Nearly two years ago I’ve experimented with the idea of using bitcoin payments to tip open source projects. The idea is to analyze the dependencies of a project (by parsing the bundler Gemfile or npm package.json), extract donation information and send a “thank you” to these projects. 
Bitcoin is the perfect protocol for this. We can directly transfer value and do not need any intermediary. The projects and users do not have to agree on a service provider (like paypal or patreon), no signup es required. It is implemented on a protocol level and not as a tool/service.
As a demo I had created a small video showing the interaction and user flow:
vimeo
How does it work?
Thanks to the nature of Bitcoin transactions it is possible (and even encouraged) to send one transaction to multiple recipients (outputs).
bundle-thankyou analyzes the dependencies from the package manger file (Gemfile, package.json,...) and gathers the recipient addresses from the locally installed gems - thus no need for a central directory or similar. Then we the user is asked how much should be paid and a single transaction is created and published. Done.
Obviously one could envision any fancy user interface and features to do that.
What the author needs to do:
The gem author adds a Bitcoin address to the gemspec (in the metadata field). That’s all the author has to do.
Because the address is in the gemspec we can be sure it is the address the author/maintainer wants the money to go to.
The author could also decide to dedicate the donations to somebody else. For example the rails gems could say thankyous should go to RailsGirls or similar projects.
What the user has to do:
Use this tool and basically run bundle thankyou and pay the desired amount. The amount will automatically be split among all the used gems.
With Bitcoin becoming more and more popular it also gets easier for people to start using it. And it would also be possible to create a service on top of this “standard” that allows users to pay with credit cards and handles the bitcoin payments.
Advantages
No signup whatsover
User and maintainer do not need to agree on a service (like paypal) to perform the transaction
No central directories and services
Based on existing tools (rubygems, npm, etc.)
Implemented on a "standard/protocol level" - additional service can be built and integrated. (like any existing bitcoin services, subscriptions, etc.)
Works internationally (not everybody has a credit card or can have a paypal account)
One transaction for multiple recipients
Usable in the moment where the user interacts with the gems (in the terminal running a bundle command)
Questions:
Why Bitcoin? It is probably the most broadly adopted solution. But any other cryptocurrency would be possible.
But I want to pay with credit card (or whatever else) Bitcoin is used as a method/"protocol" to transfer value.
We could provide additional service (for both user and project maintainer separately) to better fit their needs - for example different payment methods, subscriptions, etc. For example card payments could be easily possible with services like Coinbase.
But I want to receive payments on my credit card Again Bitcoin is the "protocol". There are already plenty tools out there that for example give you a visa/master card for spending the received bitcoins. Or bank transfers, or m-pesa mobile money payments, or....
A comment about money
I am very critical about the human perception of the "payment". I do not want it to feel like I've "paid" somebody for something. The tone/message is super important and it should not be "payment" but a way of saying "thank you"... thus bundle thankyou.
1 note · View note
y-yagi · 8 years ago
Text
Bundler 1.16.0で生成したbinstubがRailsアプリで動かない
表題の通り。Bundlerのバージョンは1.16.0。Railsのバージョンは5.1.4。ただ、Railsは5.14以下であれば、バージョンを問わず同じ問題が起きる。
Bundler 1.16.0で生成したbinstubをRailsアプリで引数を指定して動かそうとすると、"Unknown switches"でエラーになる。
$ rails new rails_app $ cd rails_app # Gemfileにunicornを追加 $ bundle binstubs unicorn $ ./bin/unicorn --port 3000 #=> Unknown switches '--port'
上記はunicornの場合だが、他のgem(rspecとか)でも同じ問題が発生する。なお、引数を指定しなければ動く。
原因
Bundler 1.16.0から、binstubでbundleのbinstubを読み込むようになった。実際に生成されるbinstubは下記の通り(unicornの場合)。
11, 12行目でbundleのbinstubが読み込まれているのがわかる。
上記読み込み処理は、はこのコミットで追加されているのだが、合わせてbundleのbinstubが追加されている("lib/bundler/templates/Executable.bundler"というファイル)。
このコミットは、Special-case binstubs bundler for the app's bundler version のissueをcloseする為に対応されている。どうやらBundler 2系がリリースされた後に、アプリが正しいバージョンのBundlerをロード出来るようにする為に対応された模様。
実際にBundlerが1.16.0が生成するbundleのbinstubを見てみると、下記のような内容になっている。
引数のハンドリングやったりlockfileみてバージョン確認したりと、割と色々とやっている。
という訳で、1.16.0で生成されるbinstubは、上記bundleのbinstubを経由して使用される事を想定している。しかし、Railsは独自にbundleのbinstubを生成しており、それは上記事情をまったく考慮していない内容なので、結果エラーになる、という状態になっている。
対応方法
根本的な対応については一旦置いておくとして、とりあえずの対応としては、Bundlerが生成したbundleのbinstubを使用するのが良さそうな気がする。
$ bundle exec bundle binstubs bundler --force
上記のように、Bundlerを使用してbundleのbinstubを再生成すればOK。 これが一番まっとうなやり方な気がする。
因みにbundleのbinstubを読み込まなければ問題無いので、単純にbundleのbinstub読み込み処理を削除するだけでも問題無かったりする。ただこれだとbinstubを生成する度に同じ事をやる必要があるのでちょっと手間か。
参考:
Bundlerのissue:1.16.0 is intercepting binstub arguments and throwing 'unknown switches' error
1 note · View note
siva3155 · 6 years ago
Text
300+ TOP RUBY ON RAILS Objective Questions and Answers
RUBY ON RAILS Multiple Choice Questions :-
1. What is bundler? a) rails generate scaffold User name:string email:string b) self-contained bundles of gems. Useful for versioning. c) Edit Gemfile, then run ‘bundle install’ or just ‘bundle’. d) Ruby Version Manager. Install and manage multiple versions of Ruby. Ans: C 2. What is gemset? a) self-contained bundles of gems. Useful for versioning. b) self-contained packages of Ruby code c) Ruby Version Manager. Install and manage multiple versions of Ruby. d) rails generate scaffold User name:string email:string Ans: A 3. What is gem? a) rails generate scaffold User name:string email:string b) self-contained packages of Ruby code c) self-contained bundles of gems. Useful for versioning. d) Ruby Version Manager. Install and manage multiple versions of Ruby. Ans: B 4. what is scaffold command? a) rails generate scaffold User name:string email:string b) Ruby Version Manager. Install and manage multiple versions of Ruby. c) self-contained packages of Ruby code d) self-contained bundles of gems. Useful for versioning. Ans: A 5. What is rake? a) self-contained bundles of gems. Useful for versioning. b) bundle exec rake db:migrate. Using bundle exec ensures that the rake version in our Gemfile is used. c) rails generate scaffold User name:string email:string d) Ruby Version Manager. Install and manage multiple versions of Ruby. Ans: B 6. What is rvm? a) rails generate scaffold User name:string email:string b) Ruby Version Manager. Install and manage multiple versions of Ruby. c) self-contained bundles of gems. Useful for versioning. d) self-contained packages of Ruby code Ans: B 7.how do you create a migration that adds a “phone” field to the “tickets” table? a) unit, functional, and integration tests b) RAILS_ENV (RAILS_ENV=production) c) integration test d) rails generate migration AddPhoneToTickets phone:string Ans: D 8.Rails’ application framework is called ———-? a) ActionPack b) ActiveRecord c) a web page d) an object Ans: A 9.how do you run a migration? a) CamelCase b) ruby make c) an object d) rake db:migrate Ans: D 10.what does “show” refer to as the :action value? a) the method within the controller to call b) HTTP methods (post, get, put, delete) c) the data being presented by the web page d) their names begin with an underscore Ans: A
Tumblr media
RUBY ON RAILS MCQs 11.how do you update? a) update_attributes (as in @ad.update_attributes(params)) b) unit, functional, and integration tests c) format (that period is a concatenation period) d) it is not associated with a model Ans: A 12.what comes after Embedded Ruby -> ruby code (file.rb) -> fild.html c) the method within the controller to call d) the data being presented by the web page Ans: B 21.what is the filter code that calls on the “check_logged_in” method for only the “edit” and “update” methods? a) the data being presented by the web page b) HTTP methods (post, get, put, delete) c) data from model objects AND the page template d) before_filter :check_logged_in, :only => Ans: D 22.representation of a resource is called ————-. a) CamelCase b) ruby make c) a web page d) ActionPack Ans: C 23.converts database records to objects is called ————-. a) for ad in @ads b) integration test c) Representational State Transfer d) an object-relational mapping library Ans: D 24.what checks if an input is numeric? a) integration test b) a responder object c) validate d) validates_numericality_of Ans: D 25.what is Rails’ object-relational mapping library? a) CamelCase b) ActiveRecord c) an object d) underscores Ans: B 26.where do you add the filter code? a) rails generate migration AddPhoneToTickets phone:string b) the data being presented by the web page c) data from model objects AND the page template d) right after the line introducing the class in the controller Ans: D 27.why is it good to redirect after inserting info into the database? a) unit, functional, and integration tests b) a script that alters the structure of the underlying database c) so that users can’t accidentally resubmit information by refreshing the page d) the data being presented by the web page Ans: C 28.which two steps to include test.xml data to the partial “map” (via the variable “data”)…? a) from general to specific (‘/ads/’ and then ‘/ads/:id’) b) include test.xml to root of “public” folder and , render (:partial=>’map’, :locals=>{:data=>’/test.xml’}) c) just replace the word “scaffold” with the word “model” d) login method (that checks a username and password) and , a filter (that calls the login method when needed) Ans: B 29.what environments does Rails have by default? a) development, test, production b) render :partial c) RAILS_ENV (RAILS_ENV=production) d) integration test Ans:A 30.how do you generate regular model code (not scaffolding)? a) just replace the word “scaffold” with the word “model” b) it is not associated with a model c) username == “admin” && password == “some_password” d) do |f| (where “f” can be anything) Ans: A 31.how should you order routes? a) just replace the word “scaffold” with the word “model” b) from general to specific (‘/ads/’ and then ‘/ads/:id’) c) rails generate migration AddPhoneToTickets phone:string d) do |f| (where “f” can be anything) Ans: B 32.what should you write in routes.rb to give meaning to http://mebay.com/ads/3? a) from general to specific (‘/ads/’ and then ‘/ads/:id’) b) map.connect ‘/ads/:id’, :controller=>’ads’, :action=>’show’ c) include test.xml to root of “public” folder and ,render (:partial=>’map’, :locals=>{:data=>’/test.xml’}) d) form_for(@object, :url=>{:action=>’create’}) Ans: B 33.to add login security, which two pieces of code do you need? a) 1.all important data is a resource2. every resource has a proper name (URL) b) .:format (that period is a concatenation period) c) the method within the controller to call d) 1. login method (that checks a username and password) and 2. a filter (that calls the login method when needed) Ans: D 34.how do partials look compared to regular erb files? a) underscores b) it runs its validators c) it is not associated with a model d) their names begin with an underscore Ans: D 35.how does “rake” differ from “ruby”? a) right after the line introducing the class in the controller b) a script that alters the structure of the underlying database c) it can do more complex things like modifying the database structure and running tests d) expression is ruby code that prints out a value in html Ans: C 36.why should partials generally use local variables? a) because they make the partial less dependent on other code b) the data being presented by the web page c) their names begin with an underscore d) the request parameters table Ans: A 37.do models have singular or plural names? a) controller b) CamelCase c) flight_id d) singular Ans: D 38.how does rake tell which migrations have been run and which have not? a) rails generate migration AddPhoneToTickets phone:string b) rails records the latest timestamp of all the migrations it runs (and runs only the latest ones) c) because they make the partial less dependent on other code d) it can do more complex things like modifying the database structure and running tests Ans: B 39.how does Rails choose to correct format to generate? a) in the controller: respond_to do |format| … end b) it is not associated with a model c) redirect_to ” … ” d) the data being presented by the web page Ans: A 40.how do you express error messages in a form (do |f|)? a) ruby make b) f.error_messages c) for ad in @ads d) underscores Ans: B 41.what are the three “magic columns” a) id, created_at, updated_at b) render :partial:id c) integration test d) rake db:migrate:id:delete Ans: A 42.what tag starts a form? a) integration test b) c) d) Ans: C 43.what is params? a) have the same URL of a table b) the word “private” c) a responder object d) the request parameters table Ans: D 44.what comes after “def check_logged_in” line for site “ads”? a) in the controller: respond_to do |format| … end b) errors.add_to_base(“there was an error”) c) .:format (that period is a concatenation period) d) authenticate_or_request_with_http_basic(“Ads”) do |username, password| Ans: D 45.what is naming convention for classes? a) ActionPack b) CamelCase c) ruby make d) a web page Ans: B 46.to create scafolding for “thing” data, run… a) because they make the partial less dependent on other code b) expression is ruby code that prints out a value in html c) just replace the word “scaffold” with the word “model” d) rails generate scaffold thing : : Ans: D 47.what variable changes the environment? a) RAILS_ENV (RAILS_ENV=production) b) development, test, production c) render :partial d) validates_numericality_of Ans: A 48.what is naming convention for file names ? a) ActiveRecord b) underscores c) an object d) validate Ans: B 49.why do you only need to call “render” in the controller sometimes and not always? a) .:format (that period is a concatenation period) b) redirect_to ” … ” c) errors.add_to_base(“there was an error”) d) if you’re happy with the default template, you can omit “render” Ans: D 50.a RESTful application uses … to define an interaction with the data. a) a responder object b) it is not associated with a model c) HTTP methods (post, get, put, delete) d) the request parameters table Ans: C RUBY ON RAILS Objective type Questions with Answers 51.what generates web pages from page templates? a) underscores b) Embedded Ruby (ERb) c) a web page d) render :partial Ans: B 52.where do you edit routs? a) :controller b) underscores c) ActiveRecord d) config/routes.rb Ans: D 53.if looping through “f” object, how do you make a text field “name”? a) b) … have the same URL c) d) Ans: D 54.what does a model object do before it saves or updates data? a) underscores b) it runs its validators c) validate d) rake db:migrate Ans: B 55.what does “rake” mean? a) #{1+1} b) singular c) ruby make d) flight_id Ans: C 56.how do you start a loop that reads through the array object “@ads”? a) a web page b) ruby make c) for ad in @ads d) ActionPack Ans: C 57.what scripts does the model-generator command create? a) their names begin with an underscore b) the model class (app/models/something.rb) and the data migration (db/migrate/…_create_something.rb) c) model names are singular, controller (and table) names are plural d) rails generate migration AddPhoneToTickets phone:string Ans: B 58.what does Embedded Ruby (ERb) process as inputs? a) data from model objects AND the page template b) HTTP methods (post, get, put, delete) c) it is not associated with a model d) their names begin with an underscore Ans: A 59.what kind of test do you create yourself? a) an object b) ActionPack c) rake db:migrate d) integration test Ans: D 60.what is “format” in “repond_to do |format|”? a) underscores b) an object c) a responder object d) integration test Ans: C 61.what does REST stand for? a) runs State test b) the request State Transfer c) Representational State Transfer d) runs State Transfer Ans: C 62.what parameters are in ? a) data from model objects AND the page template b) form_for(@object, :url=>{:action=>’create’}) c) .:format (that period is a concatenation period) d) do |f| (where “f” can be anything) Ans: B 63.how do you add an error (called “there was an error”) in the validate method? a) redirect_to ” … ” b) errors.add_to_base(“there was an error”) c) the word “private” d) their names begin with an underscore Ans: B 64.how do you call a partial? a) a web page b) underscores c) singular d) render :partial Ans: D 65.how do you run the latest migrations? a) ruby make b) a web page c) rake db:migrate d) CamelCase Ans: C 66.what’s special about a search form? a) for ad in @ads b) it is not associated with a model c) Representational State Transfer d) it runs its validators Ans: B 67.what expression do all layouts have? a) b) c) integration test d) Ans: A 68.if the table Seats is associated with the table Flights, how should you name the table column in Seats to indicate its flight? a) an object b) flight_id c) ruby make d) #{1+1} Ans: B 69.how would you insert “1 + 1” into a string? a) CamelCase b) #{1+1} c) singular d) validate Ans: B 70.what is the second uri indicate? a) an object b) ActionPack c) CamelCase d) :controller Ans: D 71.what precedes the “check_logged_in” method in a class? a) the request parameters table b) the word “private” c) d) redirect_to ” … ” Ans: B 72.what method name indicates to rails that it should validate before saving/updating? a) validate b) ActionPack c) flight_id d) ruby make Ans: A 73.how do you ask for all records of the Ads table? a) singular b) @ads = Ad.find(:all) c) validate d) rake db:migrate Ans: B 74.when data is retrieved from the database, what does Rails turn it into? a) a responder object b) validate c) flight_id d) an object Ans: D 75.what are the main principles of REST? a) 1. all important data is a resource, 2. every resource has a proper name (URL), 3. you can perform a standard set of operations on resources (usually CRUD), 4. client and server talk statelessly b) model names are singular, controller (and table) names are plural c) a script that alters the structure of the underlying database d) no. arrays just keep references to objects stored in memory. Ans: A 76.When is it best to use an array rather than a hash? a) An array uses indices and a hash uses “keys” b) puts (pronounced “put-ess”) c) Red refers to writing a failing test d) When the order matters, use an array. Ans: D 77.Describe the TDD cycle. a) An array uses indices and a hash uses “keys” b) It’s a test that simulates a browser accessing our application. c) Red, Green, Refactor,Red refers to writing a failing test,Green refers to writing a passing test Once we have a passing test we are free to refactor the code. d) This maps requests for the URL /pages/home to the home action in the Pages controller. Ans: C 78.How does a local variable differ from an instance variable when empty? a) This is not a standard rails directory, but a directory installed by RSpec in which all test files are kept. b) The @ indicates that this is an “instance variable”. An instance variable defined in the action (or function) of a controller is automatically available in the view of that same name. c) It’s a test that simulates a browser accessing our application. d) Ruby complains if we try to evaluate an undefined local variable, but issues no such complaint for an instance variable; instead, instance variables are nil if not defined Ans: D 79.What is a .erb file? a) The “erb” stands for “Embedded RuBy”. .erb files look like about.html.erb and live in the “views” directory.They are just html files with embedded ruby in them. b) The @ indicates that this is an “instance variable”. An instance variable defined in the action (or function) of a controller is automatically available in the view of that same name. c) puts (pronounced “put-ess”) d) It’s a test that simulates a browser accessing our application. Ans: A 80.What’s the best way to describe an object in Ruby? a) An array uses indices and a hash uses “keys” b) It’s easier to describe what objects do, which is respond to messages. An object like a string, for example, can respond to the message length, which returns the number of characters in the string c) This maps requests for the URL /pages/home to the home action in the Pages controller. d) It’s a test that simulates a browser accessing our application. Ans: B 81.What is a Class? a) An array uses indices and a hash uses “keys” b) Classes are simply a convenient way to organize functions (also called methods). In pages_controller.rb, PagesController is a Class which holds a “home” action. c) A controller is a container for a group of (possibly dynamic) web pages. d) This maps requests for the URL /pages/home to the home action in the Pages controller. Ans: B 82.What kind of object is “@title” and what is special about it in Rails? a) It’s a test that simulates a browser accessing our application. b) The “erb” stands for “Embedded RuBy”. .erb files look like about.html.erb and live in the “views” directory.They are just html files with embedded ruby in them. c) Ruby complains if we try to evaluate an undefined local variable, but issues no such complaint for an instance variable; instead, instance variables are nil if not defined d) The @ indicates that this is an “instance variable”. An instance variable defined in the action (or function) of a controller is automatically available in the view of that same name. Ans: D 83.What does the routes.rb file do with “get pages/home”? a) When a request is made for, say, /pages/home, the Pages controller executes the code in the “home” action and then automatically renders the view corresponding to the action — in this case, home.html.erb. b) This maps requests for the URL /pages/home to the home action in the Pages controller. c) When the order matters, use an array. d) It’s a test that simulates a browser accessing our application. Ans: B 84.Describe a “symbol” in Rails? a) Ruby complains if we try to evaluate an undefined local variable, but issues no such complaint for an instance variable; instead, instance variables are nil if not defined b) Symbols look kind of like strings, but prefixed with a colon instead of surrounded by quotes. For example, :name is a symbol. You can think of symbols as basically strings without all the extra baggage c) An array uses indices and a hash uses “keys” d) This is not a standard rails directory, but a directory installed by RSpec in which all test files are kept. Ans: B 85.What is the status code for “success” when throwing a GET? a) 300 b) 200 c) 400 d) 150 Ans: B 86.What is the command to print to the screen? a) When the order matters, use an array. b) An array uses indices and a hash uses “keys” c) It’s a test that simulates a browser accessing our application. d) puts (pronounced “put-ess”) Ans: D 87.What is the main benefit that a Class gets by inheritance from ApplicationController? a) An array uses indices and a hash uses “keys” b) When a request is made for, say, /pages/home, the Pages controller executes the code in the “home” action and then automatically renders the view corresponding to the action — in this case, home.html.erb. c) This maps requests for the URL /pages/home to the home action in the Pages controller. d) When the order matters, use an array. Ans: B 88.Name two ways to indicate a “block” in Rails a) This maps requests for the URL /pages/home to the home action in the Pages controller. b) The request hits the rails router which dispatches to the proper controller action. In this case, it goes to the index action in the Users controller. c) Curly braces or do..end. It’s best to use curly braces for short one-line blocks and the do..end for mult-iline blocks. d) When the order matters, use an array. Ans: C 89.What is an integration test? a) It’s a test that simulates a browser accessing our application. b) When the order matters, use an array. c) puts (pronounced “put-ess”) d) An array uses indices and a hash uses “keys” Ans: A 90.What is the most special aspect of nil? a) This maps requests for the URL /pages/home to the home action in the Pages controller. b) A controller is a container for a group of (possibly dynamic) web pages. c) When the order matters, use an array. d) Other than the object “false” itself, nil is the only object that defaults to false. Ans: D 91.What is the /spec directory in rails? a) Ruby complains if we try to evaluate an undefined local variable, but issues no such complaint for an instance variable; instead, instance variables are nil if not defined b) Symbols look kind of like strings, but prefixed with a colon instead of surrounded by quotes. For example, :name is a symbol. You can think of symbols as basically strings without all the extra baggage c) This is not a standard rails directory, but a directory installed by RSpec in which all test files are kept. d) This maps requests for the URL /pages/home to the home action in the Pages controller. Ans: C 92.What’s the difference between a hash and an array? a) An array uses indices and a hash uses “keys” b) When the order matters, use an array. c) It’s a test that simulates a browser accessing our application. d) puts (pronounced “put-ess”) Ans: A 93.What is a controller? a) A controller is a container for a group of (possibly dynamic) web pages. b) An array uses indices and a hash uses “keys” c) It’s a test that simulates a browser accessing our application. d) When the order matters, use an array. Ans: A 94.how do you define a “status” parameter (within a “tweet” parameter)? a) code = tweet,url = /tweets/1 b) @tweet = Tweet.create(:status => params) c) params = {:tweet => {:status => “I’m dead” }} d) @tweet = Tweet.create(:status => params) Ans: D 95.how do you check the logged in user’s id? a) :status b) session c) t.errors d) the attribute Ans: B 96.let’s say you have a method… def get_tweet @tweet = Tweet.find(params) end how do you tie an action to that that only kicks in for the edit, update, and destroy methods? a) index, show, new, edit, create, update, destroy b) code = tweet, :method => :delete url = /tweets/1 c) before_filter :get_tweet, :only => d) @tweet = Tweet.create(:status => params) Ans: C 97.How to includes all javascript? a) b) c) d) Ans: B 98.what do you use to send messages to the user? a) t.errors b) :status c) a flash d) @tweet Ans: C 99.How includes all stylesheet files? a) b) c) d) Ans: D 100.How do you make sure your forms don’t get hacked? a) b) c) d) tweets_path Ans: B RUBY ON RAILS Questions and Answers Pdf Download Read the full article
0 notes
syndicode · 6 years ago
Text
Reducing memory usage in Rails by tuning Gemfile
As Rails agency, Syndicode knows that the default Rails dependency management can easily lead to large memory footprint because it loads all gems even if they are unused. That’s why today we’d like to share with you the material about reducing memory usage in Rails by tuning...
0 notes
sovereignsoft · 5 years ago
Text
Catch Up Ruby on Rails 6 New Update with New Features
Ruby on Rails
Ruby is a much-matured language now. Rails 6 are booming popularity worldwide again. It has many helpful tools and gems. Using these gems and tools, you can build a web application in a faster way. Ruby on Rails has a big community to give solution to the developer’s problem for every small and complex issue.
 Ruby on Rails updates continuously with good features. Rails 6.0 update released and is filled with many features that both smaller and bigger applications. It will benefit from, as it feels like many improvements around speed and scalability were introduced in Rails 6. A huge number of outlooks were riding on this Ruby on Rails 6 release and the following are some main key points to note on its upgrading.
 Ruby on Rails 6 upgradation from Ruby on Rails 5      
 Ruby on Rails Version: Rails 6 require Ruby version 2.5+.
 Considerations: On the off chance that your application Rails adaptation is 5.2 and you have to move up to Rails form 6 and your current application rails rendition has the most recent fix form before moving to the following major/minor variant.
 Gems: Check all your gems from Gemfile are compatible with Rails 6 gem. By visiting the respective gem’s GitHub page you can check Gemfile.
 Config files: In RoR there is a task called `rails app: update`. It will update the app to the latest version. At some stage in-app updates, it will give you conflict over current files. You need to fix those files carefully.
 Removals: There are many removals in Rails 6. You need to take care of that during upgradation.
 Webpacker: Webpacker is by default added in Rails 6. It is a modern javascript compiler. You can manage your asset pipeline using a webpacker.
 Credentials: Rails 6 given support for all credentials. It means you can create separate credentials to each environment e.g. dev, staging and production.
 On July 22, 2019 Rails 6 latest version rc2 has released. When you want to start a new rails app today then definitely you can use 6.0.0.rc1 or 6.0.0.rc2 gem version. Let’s see what the Ruby on Rails 6 new features added are in the final release of 6.0.0.rc1.
 Action Mailbox: Email is a powerful tool to communicate. Almost everyone needs to send emails - but increasingly many also want to add a human touch with their users. In the world of bots, instead of do-not-reply emails, people are sending emails that can be replied to by real people. Action Mailbox allows you to course incoming emails to controller-like mailboxes. Ruby on Rails 6.0 now ships with inbuilt support for Mailgun, Mandrill, Postmark, and SendGrid. The emails are processed via Active Record and stored on the cloud through Active Storage and can be sent to one more multiple mailboxes.
 Action Text: The Action Text includes Active Storage-backed file and image processing, and a text-processing flow that ties it all together. It brings rich text content and editing to Rails. Action Text includes the Trix editor that handles everything from formatting to links to quotes to lists to embedded images and galleries. This feature is helpful when you're not using any UI framework like React with Ruby on Rails.
 Parallel Testing: Parallel testing was a little bit hard up until Rails 6 especially in CI systems when you needed isolation between processes and interference with the database. Although forking processes is the default method, threading is supported as well. Parallel testing reduces the time it takes your entire test suite to run.
 Zeitwerk: The new code loader for Ruby is Zeitwerk and it is efficient, thread-safe, and matches Ruby semantics for constants. In specified conventional file structure, Zeitwerk loads your project’s classes and modules on demand meaning you don’t need to write require calls for your files.
  Action Cable Testing: This is one of the small features added in Rails 6. In this, Action Cable testing tools allow you to test your Action Cable functionality at any level: connections, channels, broadcasts.
 Webpack: Earlier Ruby on Rails uses Assets Pipeline to built JS and CSS. But after updating its using webpacker gem for all Style Sheets, images and JS libraries wrap into a single bundle with a single access point. In Ruby on Rails 6 Webpack bundlers have been added as default.
 Hoping that this blog will be helpful to learn new features added in Ruby on Rails 6 as well as the upgradation process. Ruby on Rails playing a very important role in Web Application Development and Web API Development. Sovereign developers always have an approach towards new trending programming languages to help you to build web applications within less time and cost compared to other technology.
0 notes
y-yagi · 8 years ago
Text
Bootsnapについて
Bootsnapに調べたのでメモ。なお、確認した際のバージョンは1.0.0です。
Shopifyが出しているBootsnapというgemがあります。説明に書いてある通り、巨大なRuby / Railsのアプリを高速化する為のgemらしいです。
何をやってくれるかというと、Ruby, Active Support、YAMLのメソッドの呼び出しの最適化、及び、キャッシュをしてくれる、というものらしいです。 なお、1.0.0の時点でサポートしているプラットフォームはMacOSとLinuxだけです。Windowsは未対応。 また、Rubyは2.3.0以上のみサポートしています。
使い方
Gemfileにgem 'bootsnap'を追加し、下記設定をrequire 'bundler/setup'の後に定義すればOKです。
require 'bootsnap' Bootsnap.setup( cache_dir: 'tmp/cache', # Path to your cache development_mode: ENV['MY_ENV'] == 'development', load_path_cache: true, # Should we optimize the LOAD_PATH with a cache? autoload_paths_cache: true, # Should we optimize ActiveSupport autoloads with cache? disable_trace: false, # Sets `RubyVM::InstructionSequence.compile_option = { trace_instruction: false }` compile_cache_iseq: true, # Should compile Ruby code into ISeq cache? compile_cache_yaml: true # Should compile YAML into a cache? )
各項目の詳細についてはコメント参照。Railsを使用している場合であれば、config/boot.rbに定義すればOKです。
何をやっているのか
READMEによると、2つの事(Path Pre-Scanning、Compilation caching)をやっているようです。
Path Pre-Scanning
ざっくりいうと、requireやloadの結果をcacheして、次に読み込みが処理が行われる際はそのcacheを使用する���うにして、不要なLOAD_PATHの読み込み処理を行わないようにしている、という事らしいです。
たとえば、fooというファイルをrequireしたい場合、通常であれば$LOAD_PATH配下に該当のファイルがあるかどうかチェックが行われます。
open x/foo.rb # (fail) # (imagine this with 500 $LOAD_PATH entries instead of two) open y/foo.rb # (success) close y/foo.rb open y/foo.rb
上記のような感じですね。
これがBootsnapを使用すると、下記のようになります(cacheがある場合)。
open y/foo.rb
同様の対応をActiveSupport::Dependencies.autoload_pathsに対しても行うようになっています。
Compilation Caching
Rubyにはコンパイル済みの命令列をバイナリとして生成 / ロードする機能(RubyVM::InstructionSequence#to_binary / RubyVM::InstructionSequnece.load_from_binary)があり、どうやらその機能を使用しているようです。yomikomu: Dump compiled iseq by binary (kakidasu) and load binary (yomidasu)と同じですね。
bootsnapでは更にYAMLに対しても同じような事を行っています。
YAMLの場合はYAML objectをMessagePackフォーマットで書きだして、YAML.load_fileではそのMessagePackで書きだした内容を読み込むようにしています。その方が普通にYAMLを読むこむより速くなるとの事です。なお、MessagePackでサポートされてないタイプがYAMLに含まれていた場合、Marshalが使われるようになっています。
実際に速くなるのか
READMEのQuick Performance Overviewによると、discourseでは起動時間が50%程度高速化(6s -> 3s)し、Shopifyでは75%の高速化(25s -> 6.5s)があったそうです。
因みに、手元の小さいアプリ(modelが20個程度)で試した感じだと、それでも10%程度は速くなりました���
今後の展開
bootsnapをrailsがデフォルトで生成するGemfileに追加しよう、というPRが作者の方からきています。
Add bootsnap to default Gemfile by burke · Pull Request #29313 · rails/rails
特に反対意見も無いようなので、そのまま入りそうな気がしています。というわけで、何もしないでも5.2辺りからは標準でbootsnapを使う状態になってそうな気がします。
参考
Rubyist Magazine - YARV Maniacs 【第 13 回】 事前コンパイルへの道
7 notes · View notes
kkkeag · 6 years ago
Text
RubyKaigi 2019に行った
中身ないけどまとめ
1日目
きいた
The Year of Concurrency
Matzのキーノート
How to use OpenAPI3 for API developer
Open APIの話
Comitteeのはなし
Pragmatic Monadic Programing in Ruby
ハイレベルなモナドの話
Pattern matching - New feature in Ruby 2.7
パターンマッチングの話
福岡
はじめての福岡だったのだけれども、空港から博多が異様に近くて驚いた。博多からSmartHR社スポンサーのシャトルバスで会場へ。キーノートを聞いたあとはOpen APIをの話やパターンマッチングの話をほどほどに聞きつつ、スポンサーブースで来場者対応をしたり、スポンサーを巡ったりした。懇親会は商店街を貸し切ってド派手にやっていて、その調整力に驚かされた…
2日目
きいた
All bugfixes are incompatibilities
リリースメンテナンスマネージャのはなし
Better CSV processing with Ruby 2.6
CSVのはなし。
Zeitwerk: A new code loader
RubyのAutoloaderのはなし。 Gemfileの中身をが
Actionable Code Coverage
SingleCovのはなし
Building a game for the Nintendo Switch using Ruby
スイッチのゲームをmRubyで作ってビルドした話。話聞きながらこれ大丈夫なの?って思ってたら、後にプラットフォームからバンされていた…
Lightning Talks
MACCRO、衛星の話がとても印象に残っている。
福岡
ホテルからメルチャリを使ったけど、体験が最高だった。お昼はお弁当。
3日目
きいた
Ruby Committers vs the World
臨時開発者会議
若干の大喜利含め。
暗黙的な一時ブロック変数、PowerShell触っていた身からすると$_の印象があるんだけど、変数のプリフィックスに$を使うからすんなり受け入れられるのであって、変数のプリフィックスがない言語でどうやってわかり易い名前にするかを考えるのはとても難しいと思った。
Cleaning up a huge ruby application
CockPadのコードを頑張って消していく話
カバレッジをとって通っていないところを消していく
アクセス解析1年分の件数を集計して、アクセスされていないものを消す
Best practices in web API client development
APIクライアントを作る上でのプラクティス
faraday_curl便利そうだった
The future of the Bundled Bundler with RubyGems
BundlerとRubyGemsのこれからについて
Timezone API
Timeクラスでタイムゾーンの��字列表記に対応させようとしたら野良開発者会議になった
クックパッドのブースでみらくいさんとカメラの話ができてよかった。pixel3欲しくなった…
福岡
お昼は屋台のラーメン、夜は黒田。黒田のやきとりは豚なんですよ〜って言われたんだけど、地元のやきとりも豚なので、特に不思議に思わなかったんだけど、多分普通じゃないんだと思う。黒田を出たあと、川を見に行ったんだけど、誰もいなかったので解散した。
0 notes
rubydailystream · 6 years ago
Text
Build a chat app with Ruby on Rails
Not all web apps need chat, but if you want to communicate with your customers, or you want them to communicate with each other, you’re going to run into this problem quickly. CometChat Pro is a fantastic alternative to rolling your own chat service. You’ll see that you can easily insert chat into your application, without it getting in the way of other content you may be delivering.
Prerequisites
This tutorial doesn’t assume much knowledge at all. Ruby on Rails is an incredibly easy framework to get started with. If you’ve worked with other web technologies, you’ll probably find it very clear and very opinionated.
CometChat Pro itself needs to run in Javascript, so you’ll see a marriage between a Ruby-based framework and view manipulation through Javascript. There are many more complicated ways to handle this in Rails. For this tutorial, I kept everything in basic Javascript and the templating engine Rails ships with: erb. Rails enthusiasts will be able to easily translate these foundational tools into their specialized setups.
I assume you are able to run Rails applications – which means you already have Ruby and bundler installed. This application uses Ruby 2.5.3 and Rails 5.2.2 but nothing happening here is special for latest versions – you could plug these tools into any modern version of Rails.
Introduction
One of the hardest parts of building chat is dealing with concurrency – users need to see new messages without refreshing their browsers. CometChat Pro achieves this with very little code, as you will see.
CometChat Pro example running in two windows.
If you’ve ever tried to use Action Cable to achieve similar results, you will be very pleased with how quickly you can copy-paste your way to real-time updates in the browser.
You can jump right into the code on Github, or take this guy for a spin yourself. Let’s get it!
Setting up
Initialization
Grab yourself a terminal and fire up a new Rails app: rails new cometchatpro --skip-active-record -T. We don’t need a database for this, and the -T prevents Rails from creating test files for us. If you’ve already got an application, you can skip this step – it should be easy to pop this code into an existing project.
Environmental variables and CometChat credentails
Next, you’re going to need an API key and an App ID from CometChat Pro. Once you’ve logged in, head to the dashboard, create a new app, then generate an API key. You’ll need fullAccess, not just authOnly. Grab those credentials and add them to your .env file. You’ll need your file to look something like this:
COMETCHAT_APP_ID=23n2f2n3p2y3 COMETCHAT_API_KEY=av22g24ll
Next, add dotenv-rails to your Gemfile and bundle. Now your application can read your environmental variables. These are private credentials for your CometChat Pro account; do not commit this file to your version control!
Chat pages
Next we’ll add some boilerplate Rails views and routing so we can chat as a user with other users. Generate a new controller: rails generate controller Users. This will create a number of files, which we’ll get into shortly. You’ll also need to add a route to your new view in config/routes.rb. Here’s what my routes.rb looks like:
Rails.application.routes.draw do resources :users, only: %i[index show] root 'users#index' end
If you jump into the GitHub code or pop over to the hosted app, you’ll see that I have added user creation. I’ve left it in place because I already built it, but CometChat Pro comes with sample users baked into every account, so you can start testing right away without needing to set up user creation first. I’ve omitted discussion of user creation for this tutorial in the sake of brevity, but you can see the code needed for that in the Github repo.
You’ll also need to create a /views/users/show.html.erb file. We’ll fill it will content in a moment.
CometChat Service
Let’s get into the meat of our application – interacting with the CometChat API. I organized interactions with CometChat Pro into a service. Here’s what it looks like:
class CometChatService include HTTParty BASE_URI = 'https://api.cometchat.com/v1'.freeze def fetch_users response = HTTParty.get("#{BASE_URI}/users", headers: headers) response.dig('data') &.map { |user| {name: user['name'], id: user['uid']} } end private def headers { apikey: ENV['COMETCHAT_API_KEY'], appid: ENV['COMETCHAT_APP_ID'] } end end
CometChat Pro does a lot of things, and as we grow our app, we might want to fill this service with further interactions with their API. Here, all we need to do is fetch a list of users we can chat with. Note that no gem is needed to interact with CCP, you simply send your requests however you like (I used HTTParty) with the proper headers (from our .env file) and CCP sends back the information you requested.
I map the JSON response from CCP into the information my application needs – the user name and the user ID.
Working with the service in the users controller
Now that we have a protocol established for speaking with the CCP API, we can use it in our UsersController. Here’s what mine looks like:
class UsersController < ApplicationController def index @users = CometChatService.new.fetch_users end def show users = CometChatService.new.fetch_users @current_user = users.find { |user| user[:id] == params[:id] } @users = users.reject { |user| user[:id] == @user[:id] } end end
I’ve skipped over the index page for brevity, but it’s just a list of users who we may “chat as”. You can think of it as the most insecure, ridiculous login page imaginable. On both views, we fetch the users registered with our CometChat Pro app. We’ve already mapped the JSON response in the service, so on the index and show pages we loop through users to display them. On the show page, we are chatting “as” a user, so we don’t want him to show up in the list of possible people to chat with (line 9).
List of users in a CometChat Pro app
The user show view
Let’s walk through the sections of our user show page in turn. In a minute, we’ll discuss the Javascript necessary to make them run. I’ve also omitted discussion of styling. You can see full code for styling in the GitHub repo, or of course you could implement your own (surely much-better) styling.
Who is talking?
To make information about our “logged-in” user (the one whose show page we are on) available to the CometChat service, we’ll want to include an invisible div with the user name and id. You can put this anywhere on your page:
The user list
To display the list of users as shown above, here’s the erb code:
&ltul class="list-group list-group-flush"> &lt% @users.each do |user| %> &ltli class="list-group-item user-select bg-light" id="&lt%= user[:id] %>" name='user-select'> &ltdiv class="d-flex w-100 justify-content-between"> &lth5 class="mb-1">&lt%= user[:name] %>&lt/h5> &lt/div> &lt/li> &lt% end %> &lt/ul>
There’s nothing special happening here – the only thing to note is that we are setting the user id as an id on the list-item. We’ll need it later to reference which user we are speaking to. Remember that our @users has come from the controller, where we gathered the list of users from the CometChat service.
Receiving and displaying messages
Next, let’s look at how we will display incoming messages. Our code starts as this:
&ltdiv id="messages" class="messages"> Messages loading... &lt/div>
We need to supply a div for CometChat Pro to load messages into. I’ve called mine messages but you could call yours anything you like.
Sending messages
We’ll use Ruby’s form helpers to create our message form, but we don’t let Ruby handle any of the functionality here:
&lt%= form_for :message, remote: true do |f| %> &ltdiv class="col-xs-9"> Message : &lt%= f.text_area :text, class: "form-control" %>&ltbr/> &lt/div> &ltdiv class="col-xs-3 capitalize"> &lt%= button_tag "Send Message", type: 'button', onclick: "javascript:sendMessage()", class: "btn btn-info btn-block" %> &lt/div> &lt% end %>
When we click the “Send Message” button, we’ll be using Javascript to work our magic. The Rails form will just sit there, happily waiting for more input.
The heart of our application: Javascript methods
The CometChat Pro service runs on Javascript and we’ll be using their clear sample code to make the pieces of our app work.
We’ll need to take the following steps:
1. Initialization – set up a connection to the CometChat service.
2. Log in a current user.
3. Select a user to chat with and fetch message history with that person.
4. Add an event listener to pick up new messages from the other person.
5. Set up an action to send messages.
Initialization
First, we’ll need to get our app talking to the CometChat Pro service and log in. In application.html.erb, add this line between the <head> tags:
&ltscript type="text/javascript" src="https://unpkg.com/@cometchat-pro/chat/CometChat.js">&lt/script>
Add these lines to the bottom of /views/users/show.html.erb:
&lt% javascript_include_tag 'show', cache: 'myfiles' %> &ltscript type="text/javascript"> setUserListeners(); document.addEventListener('turbolinks:load', initializeChat); &lt/script>
We’ll look at the setUserListeners method in a second. Finally, we’re referencing a show.js.erb, which we’ll need to create and add to app/assets/javascripts.
Here’s our first Javascript method:
const initializeChat = () => { CometChat.init('').then( hasInitialized => { loginUser() }, error => { console.log("Initialization failed with error:", error); } )};
Because we loaded the CometChat Pro Javascript in our application.html.erb, we can now use CometChatmethods in our Javascript file. The first one we’ll use is .init, which requires us to send in our APP ID as a credential. We’re calling the loginUser method once our chat is initialized.
User login
After initialization, we need to log in the current user. This application includes no security whatsoever – anyone can chat as anyone else. Obviously you’d want to have something just a tiny bit more customized in a production app. Here’s how we log in our user on CometChat Pro:
const loginUser = () => { const userDiv = document.getElementById('user-id'); if (!userDiv) { return true } const id = userDiv.dataset.id; CometChat.login(id, '').then( User => { const messageDiv = document.getElementById('messages'); messageDiv.innerHTML = `<div class="whisper">Choose a user to start chatting</div>`; }, error => { console.log("Login failed with exception:", {error}); }) }
Remember that weird tag('div') that held our user information from show.html.erb? Here we use it to find out what user needs to log in. If something has gone wrong with that div, we just bail from this operation.
Next, we call our next CometChat method: .login. We take the id we got from the user div, and use our API credentials. If CometChat Pro sends back a User, we update the message div to say “Choose a user to start chatting”.
setUserListeners
Let’s hop back to the show.html.erb page where we called setUserListeners() at the bottom of the view. In order to know who we want to chat with, we’ll need to listen for clicks on the user list. Here’s what that code looks like:
const setUserListeners = () => { const userDivs = document.getElementsByName('user-select'); userDivs.forEach(user => user.addEventListener("click", e => { const id = e.target.id || e.target.offsetParent.id setUser(id); fetchMessages(id); })) }
userDivs is a node list of divs, one for each of the users we can chat with. We loop through them and add event listeners on each. If we click on a user, we want to first call setUser to tell our application who we want to talk to, and then fetchMessages for that user.
setUser and addMessageListener
Here’s where we really get some magic and CometChat Pro takes care of the heavy lifting for us. Once we log our user in and select someone to chat with, we want to see any new messages from that person in real time. With CometChat Pro, we don’t have to worry about keeping any websockets open or dealing with refreshing – the messages just appear as you would expect.
The setUser function is only concerned with making the active user blue in the list, so I’ve skipped it here. But it also calls the addMessageListener class, which is critical for receiving new messages as they come in:
const addMessageListener = id => { CometChat.addMessageListener( 'listener_id', new CometChat.MessageListener({ onTextMessageReceived: textMessage => displayNewMessage(id, textMessage) }) )}
This is taken almost directly from the CometChat Pro documentation. We call the CometChat addMessageListener method, then using incoming messages to update our message div. Fire up two different browsers to see this in action (or just check out the gif above).
displayNewMessage
When that new message comes in, we’ll need to update our view. Here’s the code:
const displayNewMessage = (currentChatterId, msg) => { const userDiv = document.getElementById('user-id'); const id = userDiv.dataset.id; if (![currentChatterId, id].includes(msg.sender.uid)) { return; } const newNode = document.createElement("div") newNode.innerHTML = newMessage(msg, id) const messageDiv = document.getElementById('messages') messageDiv.appendChild(newNode) messageDiv.scrollTop = messageDiv.scrollHeight }
First, if the incoming message isn’t between our logged-in user and the user we’re chatting with, we ignore it. Then, we need to process the new message – we construct a new div with the message, then we append it to the messages div, again scrolling to the bottom so we can see it.
newMessage
Our newMessage method will simply format the message – it’s a new bubble in our chat app. We check if the sender has the same id as the logged-in user so we can apply the self class to the div – making it pink for “self” and blue for everybody else.
const newMessage = (msg, id) => { return ( `&ltdiv class='message ${msg.sender.uid === id && 'self'}'> &ltdiv class='message-text'>${msg.text}&lt/div> &ltdiv class='message-sender'>- ${msg.sender.name}&lt/div> &lt/div>` ) }
fetchMessages
Before we start chatting, we need to see the messages that have come before and anything that was exchanged when we weren’t chatting with that particular user. Our fetchMessages method will grab the messages between our logged-in user and our selected user.
const fetchMessages = id => { if (!id) { return; } const limit = 30; const messagesRequest = new CometChat.MessagesRequestBuilder().setUID(id).setLimit(limit).build(); messagesRequest.fetchPrevious().then( messages => { const messageDiv = document.getElementById('messages'); messageDiv.innerHTML = messages.length > 0 ? messages.map(msg => newMessage(msg, id)).join('') : `&ltdiv class="whisper">Start of message history&lt/div>`; messageDiv.scrollTop = messageDiv.scrollHeight }, error => { console.log("Message fetching failed with error:", error); } ); }
If for some reason we don’t have an id, we’ll return so as not to get errors. First we construct our request, setting the ID of the user we want to talk to, and the limit of messages we want to receive. More complicated applications would want to automatically fetch results from further back in time as the user scrolls.
Once our message requester is set up, we can call fetchPrevious to grab a list of messages. This returns messages, which we can use to populate our messages div. Each message is a JSON object with a sender.uid, sender.name and a text. We map through the messages and return them as HTML objects and replace whatever is inside messages with our new message node list. Remember to .join('') them or you’ll see a bunch of commas between divs. If there were no messages, we want to show the user that this is the “start of message history”.
Last, we want to scroll to the bottom of the messages div to show the user the latest messages.
Sending new messages
We’re logging in, selecting a user to chat with, fetching all the old message history between these two, and receiving any new messages sent to us from that user. Finally, we want to be able to send messages. Here’s how we do it:
const sendMessage = () => { const recipient_id = document.getElementsByClassName('bg-info')[0].id; const message_text = document.getElementsByName('message[text]')[0].value; document.getElementsByName('message[text]')[0].value = '' const messageType = CometChat.MESSAGE_TYPE.TEXT; const receiverType = CometChat.RECEIVER_TYPE.USER; const textMessage = new CometChat.TextMessage(recipient_id, message_text, messageType, receiverType); CometChat.sendMessage(textMessage).then( message => displayNewMessage(recipient_id, message), error => { console.log("Message sending failed with error:", error); } ); }
First, we determine which user we are chatting with, based on the highlighted user in the list. Next, we read the message from the form, and clear the form so it’s ready for the next message.
Our textMessage object will call CCP’s TextMessage method, constructing a message to send based on the logged-in user, receiver id, message text, and the types of message and recipient. Then we just sendMessagewith our textMessage object, wait for a message response, and render the message in the same way we would an incoming message from another user.
And that, my friends, is everything we need to install chat in our Rails app!
Conclusion
If you’ve been following along, you now have a fully-functional one-to-one chat app in your project. You can start exploring the other functionalities available through CometChat Pro, or you can start refactoring with jQuery, Slim, or other tools you may wish to incorporate to pare down the code. Hopefully seeing everything in plain Javascript made it clear what we are accomplishing, even if you want to have more efficient code in your own application.
Best of luck installing chat in your own app and have fun!
Links
Github repository with this code + user creation
Heroku deployment so you can see it running quickly. Try two browsers!
CometChat Pro documentation
HTTParty gem
0 notes
awsexchage · 7 years ago
Photo
Tumblr media
続・日本で 128 番目くらいに Ruby で AWS Lambda を試したメモ 〜俺のライブラリを Lambda で動かしたいけど…〜 https://ift.tt/2Cjw900
追記
追記 (3) aws-sdk 以外の gem だけをロードする場合ってどうなのか
検証コード
以下のように furikake で利用する aws-sdk を除いて, 依存している gem を require してみます.
require 'benchmark' def run(event:, context:) Benchmark.bm(10) do |r| r.report 'require gems' do require 'thor' require 'markdown-tables' require 'backlog_kit' end end end run(event: {}, context: {})
結果
# On macOS (Ruby 2.5.1) $ bundle exec ruby lambda.rb user system total real require gems 0.091279 0.049211 0.140490 ( 0.387624) $ bundle exec ruby lambda.rb user system total real require gems 0.091667 0.041068 0.132735 ( 0.133426) $ bundle exec ruby lambda.rb user system total real require gems 0.087537 0.043253 0.130790 ( 0.132725) # On Docker Container (lambci/lambda:build-ruby2.5) bash-4.2# ruby lambda.rb user system total real require gems 0.060000 0.310000 0.370000 ( 1.752973) bash-4.2# ruby lambda.rb user system total real require gems 0.060000 0.190000 0.250000 ( 1.729625) bash-4.2# ruby lambda.rb user system total real require gems 0.140000 0.120000 0.260000 ( 1.668639) # On Lambda user system total real require gems 0.000016 0.000006 0.000022 ( 0.000020) user system total real require gems 0.000021 0.000008 0.000029 ( 0.000026) user system total real require gems 0.000030 0.000011 0.000041 ( 0.000037)
Lambda での処理時間が圧倒的に高速で優秀だと思います. 全く aws-sdk に依存しない gem を利用する場合にはロードに要する時間はとても短いことを確認しました.
見解
これらの結果から, 以下のようなことが言えそうです.
サードパーティ製の gem において,aws-sdk に依存している gem を Lambda で利用する場合, 関数の初回起動時だけ gem のロードに時間が掛かる可能性がる (furikake はまさにそうで, furikake 自身が aws-sdk に依存している)
追記 (2) Ruby ランタイムに組み込まれている gem の一覧
以下, Ruby ランタイムに事前に組み込まれている gem の一覧です. aws-sdk は予め組み込まれていました (そりゃそうだ…).
codestar (1.7.0) aws-sdk-cognitoidentity (1.5.0) aws-sdk-cognitoidentityprovider (1.10.0) aws-sdk-cognitosync (1.5.0) aws-sdk-comprehend (1.8.0) aws-sdk-configservice (1.19.0) aws-sdk-connect (1.8.0) aws-sdk-core (3.37.0) aws-sdk-costandusagereportservice (1.5.0) aws-sdk-costexplorer (1.12.0) aws-sdk-databasemigrationservice (1.13.0) aws-sdk-datapipeline (1.5.0) aws-sdk-dax (1.7.0) aws-sdk-devicefarm (1.12.0) aws-sdk-directconnect (1.8.0) aws-sdk-directoryservice (1.10.0) aws-sdk-dlm (1.6.0) aws-sdk-dynamodb (1.16.0) aws-sdk-dynamodbstreams (1.5.0) aws-sdk-ec2 (1.56.0) aws-sdk-ecr (1.8.0) aws-sdk-ecs (1.22.0) aws-sdk-efs (1.6.0) aws-sdk-eks (1.7.0) aws-sdk-elasticache (1.9.0) aws-sdk-elasticbeanstalk (1.13.0) aws-sdk-elasticloadbalancing (1.7.0) aws-sdk-elasticloadbalancingv2 (1.16.0) aws-sdk-elasticsearchservice (1.14.0) aws-sdk-elastictranscoder (1.6.0) aws-sdk-emr (1.7.0) aws-sdk-firehose (1.8.0) aws-sdk-fms (1.6.0) aws-sdk-gamelift (1.9.0) aws-sdk-glacier (1.13.0) aws-sdk-glue (1.20.0) aws-sdk-greengrass (1.10.0) aws-sdk-guardduty (1.10.0) aws-sdk-health (1.7.0) aws-sdk-iam (1.10.0) aws-sdk-importexport (1.5.0) aws-sdk-inspector (1.11.0) aws-sdk-iot (1.18.0) aws-sdk-iot1clickdevicesservice (1.5.0) aws-sdk-iot1clickprojects (1.5.0) aws-sdk-iotanalytics (1.9.0) aws-sdk-iotdataplane (1.5.0) aws-sdk-iotjobsdataplane (1.6.0) aws-sdk-kinesis (1.8.0) aws-sdk-kinesisanalytics (1.7.0) aws-sdk-kinesisvideo (1.6.0) aws-sdk-kinesisvideoarchivedmedia (1.6.0) aws-sdk-kinesisvideomedia (1.5.0) aws-sdk-kms (1.11.0) aws-sdk-lambda (1.13.0) aws-sdk-lambdapreview (1.5.0) aws-sdk-lex (1.8.0) aws-sdk-lexmodelbuildingservice (1.11.0) aws-sdk-lightsail (1.10.0) aws-sdk-machinelearning (1.5.0) aws-sdk-macie (1.5.0) aws-sdk-marketplacecommerceanalytics (1.5.0) aws-sdk-marketplaceentitlementservice (1.5.0) aws-sdk-marketplacemetering (1.5.0) aws-sdk-mediaconvert (1.16.0) aws-sdk-medialive (1.15.0) aws-sdk-mediapackage (1.9.0) aws-sdk-mediastore (1.6.0) aws-sdk-mediastoredata (1.7.0) aws-sdk-mediatailor (1.6.0) aws-sdk-migrationhub (1.7.0) aws-sdk-mobile (1.5.0) aws-sdk-mq (1.7.0) aws-sdk-mturk (1.8.0) aws-sdk-neptune (1.6.0) aws-sdk-opsworks (1.8.0) aws-sdk-opsworkscm (1.9.0) aws-sdk-organizations (1.15.0) aws-sdk-pi (1.5.0) aws-sdk-pinpoint (1.12.0) aws-sdk-pinpointemail (1.0.0) aws-sdk-polly (1.13.0) aws-sdk-pricing (1.5.0) aws-sdk-rds (1.36.0) aws-sdk-redshift (1.13.0) aws-sdk-rekognition (1.14.0) aws-sdk-resourcegroups (1.7.0) aws-sdk-resourcegroupstaggingapi (1.5.0) aws-sdk-resources (3.27.0) aws-sdk-route53 (1.15.0) aws-sdk-route53domains (1.7.0) aws-sdk-s3 (1.23.1) aws-sdk-sagemaker (1.22.0) aws-sdk-sagemakerruntime (1.6.0) aws-sdk-secretsmanager (1.19.0) aws-sdk-serverlessapplicationrepository (1.9.0) aws-sdk-servicecatalog (1.12.0) aws-sdk-servicediscovery (1.7.0) aws-sdk-ses (1.13.0) aws-sdk-shield (1.8.0) aws-sdk-signer (1.4.0) aws-sdk-simpledb (1.5.0) aws-sdk-sms (1.5.0) aws-sdk-snowball (1.9.0) aws-sdk-sns (1.7.0) aws-sdk-sqs (1.9.0) aws-sdk-ssm (1.32.0) aws-sdk-states (1.7.0) aws-sdk-storagegateway (1.12.0) aws-sdk-support (1.5.0) aws-sdk-swf (1.5.0) aws-sdk-transcribeservice (1.10.0) aws-sdk-translate (1.6.0) aws-sdk-waf (1.10.0) aws-sdk-wafregional (1.11.0) aws-sdk-workdocs (1.6.0) aws-sdk-workmail (1.6.0) aws-sdk-workspaces (1.8.0) aws-sdk-xray (1.8.0) aws-sigv2 (1.0.1) aws-sigv4 (1.0.3) bigdecimal (default: 1.3.4) bundler (1.17.1) cmath (default: 1.0.0) csv (default: 1.0.0) date (default: 1.0.0) dbm (default: 1.0.0) etc (default: 1.0.0) fcntl (default: 1.0.0) fiddle (default: 1.0.0) fileutils (default: 1.0.2) gdbm (default: 2.0.0) io-console (default: 0.4.6) ipaddr (default: 1.2.0) jmespath (1.4.0) json (default: 2.1.0) openssl (default: 2.1.2) psych (default: 3.0.2) rdoc (default: 6.0.1) scanf (default: 1.0.0) sdbm (default: 1.0.0) stringio (default: 0.0.1) strscan (default: 1.0.0) webrick (default: 1.4.2) zlib (default: 1.0.0)
追記 (1) Ruby ランタイムの gem environment
以下のようになっていました.
RubyGems Environment: - RUBYGEMS VERSION: 2.7.6 - RUBY VERSION: 2.5.3 (2018-10-18 patchlevel 105) [x86_64-linux] - INSTALLATION DIRECTORY: /var/runtime - USER INSTALLATION DIRECTORY: /.gem/ruby/2.5.0 - RUBY EXECUTABLE: /var/lang/bin/ruby - EXECUTABLE DIRECTORY: /var/runtime/bin - SPEC CACHE DIRECTORY: /.gem/specs - SYSTEM CONFIGURATION DIRECTORY: /var/lang/etc - RUBYGEMS PLATFORMS: - ruby - x86_64-linux - GEM PATHS: - /var/runtime - /var/task/vendor/bundle/ruby/2.5.0 - /opt/ruby/gems/2.5.0 - GEM CONFIGURATION: - :update_sources => true - :verbose => true - :backtrace => false - :bulk_threshold => 1000 - REMOTE SOURCES: - https://rubygems.org/ - SHELL PATH: - /var/lang/bin - /var/lang/bin - /usr/local/bin - /usr/bin/ - /bin - /opt/bin
これは
俺でもわかる!俺でもわかった!ことを将来の俺達のために残すアドベントカレンダーです 俺でもわかるシリーズ Advent Calendar 2018 - Qiita - Qiita
qiita.com
俺でもわかるシリーズ Advent Calendar 2018 第 8 日目の記事になる予定です.
集え、初老丸達よ。初老丸達による世界に向けた技術的(又はそれに関連する)な物語を綴るカレンダーです。我こそ初老丸という方、初老丸予備軍の方も奮ってご参加下さい。ジーク・初老丸! 初老丸 Advent Calendar 2018 - Qiita - Qiita
qiita.com
初老丸 Advent Calendar 2018 第 12 日目の記事になる予定です.
tl;dr
昨日, 以下のような記事を書きました.
# これは初老丸アドベントカレンダー 2018 の 8 日目の記事になる予定です.https://ift.tt/2SOSxUe 加藤さん, 事件です.ずっと待たれ... 日本で 128 番目くらいに Ruby で AWS Lambda を試したメモ - Qiita - Qiita
qiita.com
Ruby が AWS Lambda 上で動くなんてすごいなあと興奮していたのもつかの間, 自分で作ったライブラリが動くかなあと試そうとして躓いた (今も躓いている) ので, メモっておきます.
あれ, 関数の起動が遅いよ…これ
furikake を Lambda で
以下のような issue を立てて, furikake を Lambda 上で動かしてみようとしました.
AWS Lambda 上で動くかどうか試す https://ift.tt/2KPfap3 AWS Lambda 上で動くかどうか試す · Issue #42 · inokappa/furikake - GitHub
github.com
以下のようなエントリーポイントになるコードを書いて sam でローカルテストを実行するところまで漕ぎ着けました.
require 'furikake' def run(event:, context:) report = Furikake::Report.new report.show end
以下, 実際の実行例です. 関数のタイムアウト自体は 60 秒に設定しています.
$ echo '{"test":"dayo"}' | sam local invoke ServerlessFurikake 2018-12-01 07:26:47 Reading invoke payload from stdin (you can also pass it from file with --event) 2018-12-01 07:26:47 Found credentials in shared credentials file: ~/.aws/credentials 2018-12-01 07:26:47 Invoking lambda.run (ruby2.5) Fetching lambci/lambda:ruby2.5 Docker container image...... 2018-12-01 07:26:49 Mounting /path/to/serverless-furikake as /var/task:ro inside runtime container 2018-12-01 07:27:50 Function 'ServerlessFurikake' timed out after 60 seconds
あれ…なんでや…タイムアウトしてしまいました.
���が起きているのか
gem のロードが遅くない??
furikake は aws-sdk 等の幾つかの gem に依存しているのですが, これらの gem のロードが遅いんじゃないかな…という仮説を立ててみました. ほんとにそれくらいしか思いつかなかったので. ということで, ローカル環境と sam invoke local で実行する Docker コンテナ環境, Lambda 環境の三環境で gem の読み込み速度の比較をしてみたいと思います.
検証環境
sam は local invoke する際に Docker コンテナを起動して, そのコンテナ上で関数を実行しているので, そのコンテナ上でデバッグしてみたいと思います.
尚, 利用する Docker イメージは以下のイメージです.
Docker images and test runners that replicate the live AWS Lambda environment - lambci/docker-lambda lambci/docker-lambda - GitHub
github.com
また, Docker を動かしている環境は以下の通りです.
$ sw_vers ProductName: Mac OS X ProductVersion: 10.13.6 BuildVersion: 17G65 $ ruby --version ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]
ハードウェア的には以下の通りです.
MacBook Pro (Retina, 13-inch, Early 2015)
2.7 GHz Intel Core i5
16 GB 1867 MHz DDR3
2018 年でも快適に利用出来る環境だと思います.
で, 一旦, furikake から離れて, シンプルな検証用のコードを書いて検証してみます.
require 'benchmark' def run(event:, context:) Benchmark.bm(10) do |r| r.report 'require aws-sdk' do require 'aws-sdk' end end end run(event: {}, context: {})
gem は bundle init で Gemfile を生成して以下のように記載して bundle install --path vendor/bundle と実行してインストールしました.
# frozen_string_literal: true source "https://rubygems.org" git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } gem 'aws-sdk'
以下のようなディレクトリ, ファイル構成となります.
$ tree . -L 2 . ├── Gemfile ├── Gemfile.lock ├── lambda.rb ├── template.yaml └── vendor └── bundle 2 directories, 4 files
ローカル環境
以下のように, real で 1.5 秒程度掛かっていますが, 2 回目以降は 0.5 秒程度となり, 個人的に違和感はありません.
$ bundle exec ruby lambda.rb user system total real require aws-sdk 0.327319 0.268804 0.596123 ( 1.496711) $ bundle exec ruby lambda.rb user system total real require aws-sdk 0.296917 0.187150 0.484067 ( 0.489871) $ bundle exec ruby lambda.rb user system total real require aws-sdk 0.299350 0.188319 0.487669 ( 0.494612)
sam の local invoke で利用する Docker 環境
以下のようにコンテナを起動します.
$ docker run -t -i --rm -v "$PWD":/var/task lambci/lambda:build-ruby2.5 /bin/bash
以下のように 100 秒以上掛かるようになりました.
bash-4.2# ruby lambda.rb && ruby lambda.rb && ruby lambda.rb user system total real require aws-sdk 27.580000 8.450000 36.030000 (119.208587) user system total real require aws-sdk 25.780000 9.320000 35.100000 (105.250906) user system total real require aws-sdk 24.730000 9.860000 34.590000 (102.976592)
あれ.
Lambda 環境 (sam でデプロイ)
sam を使ってデプロイしてマネジメントコンソールで確認します. タイムアウトは最大の 15 分に設定しています.
user system total real require aws-sdk 17.486155 1.504303 18.990458 (232.400251) user system total real require aws-sdk 0.000095 0.000008 0.000103 ( 0.000101) user system total real require aws-sdk 0.000026 0.000002 0.000028 ( 0.000025) user system total real require aws-sdk 0.000026 0.000002 0.000028 ( 0.000025)
初回が鬼のように時間が掛かっています. 2 回目以降は 1 回目が嘘のように速くなっていて, むしろローカル環境で実行するよりも速くなっています. どのような仕組みになっているかは分かりかねますが, gem がキャッシュに載っているんだと思います… きっと.
エントリーポイントになるコードを修正して, デプロイし直したところ, キャッシュを含む環境が破棄されて新しい環境に展開される為, 初回起動と同じくらいの起動時間を要してしまいました.
user system total real require aws-sdk 18.216496 1.705607 19.922103 (243.839962) Hello. user system total real require aws-sdk 0.000100 0.000010 0.000110 ( 0.000107) Hello.
なぜか, 初回起動時だけ 2 回 require 'aws-sdk' が実行されているのは…すいません. コードの問題だとおもいます.
Lambda 環境 (手動で Lambda 関数を作成)
マネジメントコンソールにて手動で Lambda 関数を作って, コードを貼り付けてテストしてみます.
user system total real require aws-sdk 0.264964 0.043559 0.308523 ( 3.765794) user system total real require aws-sdk 0.000027 0.000004 0.000031 ( 0.000027) user system total real require aws-sdk 0.000024 0.000004 0.000028 ( 0.000026)
初回起動時は 3 秒ほど掛かっていますが, 2 回目以降は一瞬です. Ruby ランタイムには既に aws-sdk が利用可能な状態で組み込まれているようですので, 単純に aws-sdk だけを利用する Lambda 関数を実行する場合には, この程度の起動時間を要すると考えておくと良さそうです.
結果のまとめ
Lambda 環境では初めて関数を起動する際, サードパーティ製 gem のロードに時間を要する場合があるので気長に待つ (タイムアウトは長めに設定しておく必要がある)
2 回目以降の実行は爆速, コードを変更してデプロイし直すと環境は破棄されて gem を再ロードしてしまうので…起動は遅くなる
sam を使ってローカル実行する場合には, 毎回 gem をロードしてしまうので注意 (そんなもんだと割り切るしかないのかな…)
以上
初回起動時間にさえ注意すれば, 多くの Ruby ライブラリを Lambda 上で動かすことができそうです!
ということで, furikake を動かせそうな気がしてきました.
元記事はこちら
「続・日本で 128 番目くらいに Ruby で AWS Lambda を試したメモ 〜俺のライブラリを Lambda で動かしたいけど…〜」
December 18, 2018 at 12:00PM
0 notes
confrnz · 7 years ago
Text
Debugging Postgresql on Your Local Machine
Let’s look at some of the ways that we can interact with Postgresql and figure out how to make it work with our Rails app locally.
First off, for ease of working, you can open up an additional tab on your shell while the shell simultaneously keeps hosting the RoR environment.  Once you open up a new tab and navigate to the program directory so you can work, remember the best practice of shortening your bash shell path, the same way that we did in the previous post:
$ export PS1='\W$ '
Once you have that going, let’s see if we can connect to the postgres server:
confrnz_app$ psql
As a result we get:
psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
So in this situation, evidently we can’t connect to postgres.  Something we forgot to do in the last post was to ensure that all of our gemfiles were updated.  Typically before you host a rails app, you want to do the following:
$ bundle install
What that does is makes sure all dependencies in your Gemfile are available to your application.  This is something called bundler.  So going back to ensure that bundler is installed, you will want to run:
$ gem install bundler
Once you have done this, you can run “rails s” once again, but you will still come out with the same error.
Let’s update postgres with:
$ brew install postgres
After installation, we see:
postgresql 10.5 is already installed and up-to-date To reinstall 10.5, run `brew reinstall postgresql`
So now that we are running an updated version of postgres.  To make sure we have all of our, “foundations” correct and software installed correctly, let’s go back and make sure everything is installed and up-to date on our local machine.  This is a manual process, but is a good way to make sure all of the foundations of the, “versions” of various programs are working how we would expect them to do so.  In the future, we can avoid much of this manual work by setting up something called, “docker” which is essentially a program which runs on local machines that allows an “image” to be run that mimics the runtime environment identical to what you would have in production.  For example, we can load the Heroku-16 stack right into docker, install all of the programs that we would like to have ready to go (such as postgres) and test the, “production environment,” right on our local computers (in theory).  That’s a lot of work and for now we’re just trying to get things up and running, so let’s keep going through the manual process for now.
Let’s make sure rails is installed.
$ gem install rails
Let’s install the postgres gem.
$ gem install pg
We may go back and choose to re-install other gems along the way if we see specific gems being called out within errors to ensure that we are completely up-to-date, but let’s leave this aside for now.
Rake in Ruby on Rails
What does it mean in Ruby to, “rake” something?
In Rails, Rake is used for common administration tasks, especially sophisticated ones that build off of each other.  It’s basically your admin/devops utility kit for Ruby on Rails.
There are a variety of tools you can use within rake, that are linked to here. Something important to keep in mind is that since this is an “in-app” tool, we have to feed all of our environmental variables that is being required on the front-end if we haven’t hard-coded it into the RoR app, so in our case we have to add, SPARKPOST_API_KEY=123 (assuming that the errors ask for it - in the future we may add this into the development environment to create more convenience).
So for example we can run:
$ SPARKPOST_API_KEY=123 rake about
To get information about the app itself.  Go ahead, try it and see what comes up!
There are additional rake top line commands which can be used, summarized below:
rake db
The most common tasks of the db: Rake namespace are migrate and create, and it will pay off to try out all of the migration rake tasks (up, down, redo, reset). rake db:version is useful when troubleshooting, telling you the current version of the database.
rake doc
can be used to generated documentation within the doc/app file, as well as on guidelines, APIs and plugins rake notes will search through your code for comments beginning with FIXME, OPTIMIZE or TODO. The search is done in files with extension .builder, .rb, .erb, .haml and .slim for both default and custom annotations.
rake routes
will list all of your defined routes, which is useful for tracking down routing problems in your app, or giving you a good overview of the URLs in an app you’re trying to get familiar with.
rake test
A good description of unit testing in Rails is given in A Guide to Testing Rails Applications
rake tmp
The Rails.root/tmp directory is, like the *nix /tmp directory, the holding place for temporary files like sessions (if you’re using a file store for files), process id files, and cached actions.
rake stats
is great for looking at statistics on your code, displaying things like KLOCs (thousands of lines of code) and your code to test ratio.
rake secret
will give you a pseudo-random key to use for your session secret.
rake time:zones:all
lists all the timezones Rails knows about.
So how do we use the rake db: database tool?
More information about “rake db” commands can be found in the database migrations guide.
db:migrate
runs (single) migrations that have not run yet.
db:create
creates the database
db:drop
deletes the database
db:schema:load
creates tables and columns within the existing database
db:setup
runs db:create, db:schema:load, db:seed
So at this point, let’s start out and assume we don’t have a database running, or that we should take a database and kill it, and restart it to see if that works.  We can do this with the following:
$ SPARKPOST_API_KEY=123 rake db:drop
$ SPARKPOST_API_KEY=123 rake db:create
We see that no matter what we do, whether we reset the database, drop and create it, we keep getting the same original error:
PG::ConnectionBad: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
How about we go and check whether postgres is even running in the first place?
$ ps auxwww | grep postgres
Basically what this is doing is on MacOS, “ps aux” shows all of the processes running on your machine, with the command “ps” being modified by “aux” to show which types of processes.  The | (pipe) command means send information from one program to another for processing, so basically send from “ps” to “grep” and “grep” means “search for a string of characters,” so it is essentially a filter for, in this case, “postgres”  The “wwww” at the end of aux controls the sizing as displayed within the terminal.
From this, we get a bunch of information about different postgres processes running.  We don’t know which one is which at this point.
Tumblr media
Side Note - Difference Between psql and postgres
Side note, taking a minute to understand the difference between “postgres” and “psql” commands within the context of the terminal.
postgres is the server itself, and runs separately from all of the client connections. It is almost never run by hand. Even if you want to start and stop it, this is normally done through pg_ctl or an init script if you installed a postgres package from your distribution.
psql is the command-line client, that connects to the server and allows you to execute individual queries by hand.
If we run the same command above and look at psql, which is our command-line client that connects us to the server, we see a different output:
Tumblr media
So at this point, we should check whether we can even use the command-line client, “psql” to log into the server.  This would be a good test - if we can’t log in, then probably RoR can’t log in with its gem either.
$ psql
We see that using this command, we get the same error.  So there appears to be some kind of problem with whether the server is listening at all.
By default, postgres listens on port 5432.  We can filter for all connections lisetning on this port by using the following command:
netstat -an | grep 5432
netstat -an ("network statistics") is a command-line tool that displays network connections (both incoming and outgoing), routing tables, and a number of network interface (network interface controller or software-defined network interface) and network protocol statistics.  The -an just shows everything on the computer in question, and grep 5432 filters out for that port.
If that command produces no output, then postgres is not listening on the normal port, or is not listening on any port.
Is The Server Running?
The following command will output directly whether the server is running or not.
$ pg_ctl -D /usr/local/var/postgres status
Starting The Server
$ postgres -D /usr/local/var/postgres
Tumblr media
Other Notes
WARNING ABOUT postmaster.pid File
PostgreSQL puts a file named postmaster.pid in the data directory to store the process id of the PostgreSQL server process. If PostgreSQL crashes, this file can contain an old pid that confuses PostgreSQL.
The Postgres help file states:
If you delete the postmaster.pid file while PostgreSQL is running, bad things will happen.
If you’re having problems, check there is no postmaster.pid in your postgres directory, probably /usr/local/var/postgres/
Postgres App
The postgres app itself can be found here.  You have to physically run postgres in order to be able to have a port to listen to.  Postgres has to be running and “open” order for the app to function in the first place.
Postgres.app includes psql, a versatile command line client for PostgreSQL. But it’s not the only option; there are plenty of great graphical clients available for PostgreSQL. Two popular tools are Postico and PGAdmin4.
Once you download the PostgreSQL app, it should look like the following upon initialization (prior to this, you have to hit “start” in order to get the program running):
Tumblr media
Now that we are running postgres, we should be able to connect to it via various programs.  Within shell, we now do the command:
$ psql
And for output we get:
psql (10.5)
Now we’re getting somewhere.  If we use the database management tool, “Postico” we see the following upon connecting to “localhost”
Tumblr media
Now that we are fairly confident that postgres is actually running, let’s go back and see if we can create a database using the RoR rake tool.
$ confrnz_app$ SPARKPOST_API_KEY=123 rake db:create Created database 'project_development' Created database 'project_test'
OK, that sounds good.  Now we seem to have a ‘project_development’ database.  What happens if we attempt to serve the site?
ActiveRecord::PendingMigrationError - Migrations are pending. To resolve this issue, run:
       bin/rails db:migrate RAILS_ENV=development
Ah yes, that’s right - we need to migrate the database after creating it.  What does it mean to migrate a database?  Basically it just means to create the table within the structure that we are looking to create.
$ SPARKPOST_API_KEY=123 rake db:migrate
After this command is run, you will see a bunch of commands on the terminal being run in which the tables are being created.  After this, run:
$ SPARKPOST_API_KEY=123 rails s
Success!  Let’s review.  After we have ensured that the following is complete, everything works.
1.  Database is running by downloading, installing and running Postgres.
2.  We re-installed things as needed to prevent versioning errors for all of the programs we have installed on our machine.
3.  We created a data base, then migrated it.
4.  We served the program in development mode, injecting environmental variables to make sure that it runs.
Tumblr media
Other Problems
Old postmaster.pid File Message
One potential problem upon attempting to run Postgres within the PGAdmin4 GUI, you could run into is the following message:
Tumblr media
You may also see an error as follows in the command terminal:
2018-10-14 19:32:06.620 CDT [41200] DETAIL:  The data directory was initialized by PostgreSQL version 9.6, which is not compatible with this version 10.5.
Many Instances of Postgres
This answer on StackExchange/SuperUser mentions the following:
Public service announcement: never delete postmaster.pid. Really. Great way to get data corruption.
Within this scenario, you may get the following message:
No Such postmaster.pid File or Directory
Running command as follows, we get the following error:
$ cat: /usr/local/var/postgres/postmaster.pid: No such file or directory
In the Database Administrator StackExchange, points to another StackOverflow answer about Postgres in general.
You can restart the server with the following command:
$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
However if you try to manually restart it, you might get the following response:
Tumblr media
So instead you can try the following to stop and then restart postgresql (brew servies):
$ brew services stop postgresql
Tumblr media
$ brew services start postgresql
Tumblr media
After restarting the server, you might be able to get things going.  However if this does not work, the best way to get everything going might be within the Postgres troubleshooting file, “Resetting the Postgres App.”
To do this, perform the following:
Quit Postgres.app
Open Activity Monitor, see if any processes name postgres are running. If so, kill them. Kill the process with the lowest pid first; child processes are respawned automatically after killing them.
Delete the Folder ~/Library/Application Support/Postgres
Delete all settings using the command: defaults delete com.postgresapp.Postgres2
Open Postgres.app again
If you can’t follow this above, try uninstalling and reinstalling Postgres:
$ brew uninstall postgresql
$ brew install postgres
Different Version Usage of Postgresql
There are several versions of Postgresql, which can be viewed here:
https://formulae.brew.sh/formula/postgresql
If you want to install an exact version of postgres, for example postgresql10, use:
$ brew reinstall postgresql@10
Then, update the database from a previous version to the current version.
$ brew postgresql-upgrade-database
Once you have reinstalled and upgraded the database to migrate to version 10...
$ pg_ctl -D /usr/local/var/postgres status
Should output the following:
Tumblr media
Port 5432 “Already In Use”
Tumblr media
Postgres when installed by brew uses a launch daemon.  The launch deamon controls the Postgres process and it automatically restarts Postgres after it is killed.  To find the daemon, try: 
$ sudo launchctl list | fgrep postg
Then you will get something like:
Tumblr media
Which is the name of the daemon.  
Then use:
$ sudo launchctl stop <name> 
...and replace <name> with the output from the command above.
Kill All Postgres Servers On Your Mac
Kill All Postgres Servers On Your Mac
Using Brew Services
An article about using Brew services to control postgresql can be found here.
0 notes
maryhola-blog · 8 years ago
Text
Tools I use for Ruby development, 2017
This is both a guide for others and a useful reminder for me about how the tools I use to do my ruby development work, beyond Ruby itself. It's specific to OS X / MacOS, but most of it applies to Linux as well (just skip the homebrew part). This guide assumes minor familiarity with terminals to execute commands.
Homebrew
Homebrew is what I use to install a lot of software and packages on my computer from the command line. It's fairly standard amongst developers using MacOS, though it's not necessary, since there are other ways of installing the programs I'll be talking about. That said, I will not cover those other ways of installing things.
To install Homebrew, just follow the instructions from the Homebrew homepage. (Note that you shouldn't normally execute a command that downloads the code it runs from the web, like how Homebrew's install command works. But in this case it's OK, as the Homebrew project can be trusted.)
To use Homebrew, you can install things by typing brew install PACKAGENAME (and replacing PACKAGENAME with the name of the package you're installing). Like other package managers, it'll figure out what dependencies are needed and install them automatically to get your package working. Nice and easy. There's more it can do, like searching for packages, uninstalling packes, upgrading packages, and so on, but that's outside of the scope of this post. We'll just be using it to install some things.
Now that we can install things easily with Homebrew, let's start install some more things!
Rbenv & Ruby-build
We could just use the version of Ruby that comes with MacOS, but Ruby is being actively developed, so the built in Ruby version on your computer is probably already somewhat outdated. Rbenv lets us switch between multiple versions of Ruby, while Ruby-build is a Rbenv plugin to make installing different Ruby versions easy. Using these programs, we'll be able to use the specific Ruby versions that we want to use for each app we make.
To install Rbenv (and Ruby-build) with Homebrew, just run brew install rbenv in the terminal and it installs them both by default. Then type rbenv init and it'll set itself up.
To use Rbenv, just run rbenv install VERSIONNAME replacing VERSIONNAME with whatever version you want to install. To see the available versions, run rbenv install -l. It's a big list, since it includes standard Ruby, Jruby, Mruby, Rbx and some others, but to stick with standard Ruby, choose one of the version numbers that doesn't have a prefix (like 2.4.1, instead of rbx-2.4.1). To choose the newest stable version, choose the highest number that doesn't have a suffix (like 2.4.1, instead of 2.5.0-dev). To see all the versions you have installed, run rbenv versions. To see what version will be used in the current directory, run rbenv version.
Once you've installed a Ruby version, you can set it to be the global default with rbenv global VERSIONNAME. That means if you're in a directory where you haven't set the Ruby version, it'll use the global verison. To set a local version for the current directory and all children directories (that don't specify their own version), run rbenv local VERSIONNAME. It's handy to do this for each project you work on.
Now that we can install and set different Ruby versions, let's look into Rubygems a bit.
Gems
Gems are packages for Ruby, and Rubygems is the package manager (installed by default with Ruby) for gems. Often gems are simple libraries that you need to require in Ruby programs in order to use their functionality, but the libraries can also have command line programs. (This is why installing the Rails gem lets you run the rails command.) The libraries can require other libraries in order to run, and the Rubygems program will install any required gems when it installs the gem you want, just like any package manager.
To install a gem with Rubygems, run gem install GEMNAME replacing GEMNAME with the name of the desired gem. One neat thing about Rbenv is that it makes it so gems you install are only for the current Ruby version you have set. That means you can have a completely different set of gems (or gem versions), depending on which of your Ruby versions you're using. One note: after installing a gem with an executable, you'll want to run rbenv rehash to ensure it'll work right.
To view a list of installed gems, run gem list -l. To search for gems, run gem search SEARCHTERM. To find out where a gem was installed to, run gem which GEMNAME. And for more information about gems and how to use Rubygems (there's a lot more it can do!), see the guides on the Rubygems site.
Now that we know a bit about dealing with gems manually, let's see how we can manage a project's gems more automatically with Bundler.
Bundler
Bundler tracks and installs gems for a project. If you have multiple projects going, you might forget which one uses which gems, and this is where Bundler helps. Bundler also helps you share and deploy your projects, since you'll have a record of exactly which gems (and which versions) are needed. It helps automatically get everyone on the same page, so to speak, even if 'everyone' is just past-you, current-you, and future-you.
To install Bundler, run gem install bundler.
Bundler keeps track of gems in a file name Gemfile. You can generate a basic one that's great for starting with by running bundle init in the root directory of your project. Now, to add a gem to your project, you simply add a line to the Gemfile about it (like gem 'rails') and then run bundle install. Bundler will then gem install any gems mentioned in the Gemfile that aren't already installed.
Another useful command is bundle exec COMMAND in order to execute a command in the context of a bundle. If you had Bundler install your gems somewhere special (for instance, vendor/bundle/ is common with Rails apps), bundle exec lets the command be able to easily require those gems.
Bundler can be used in more complex ways than just what I've mentioned, so be sure to check out the Bundler documentation to learn more.
Other Stuff
If you're just getting into development, I suggest you find a nice text editor that you like. I highly recommend either Vim or Emacs. Once you learn how to use one of those (and you do need to learn a bit) you'll likely never go back to a different text editor since they're both so powerful and extensible and easy to use (once you know how), and they've been around forever and many people use them so there's loads of support. I can't sing their praises highly enough. More about Vim/Emacs is a topic for another post (and many others have already written about them), but I'll just note that I'm a Vim guy myself, and I like using MacVim when I'm not just using normal Vim in the terminal.
There are, of course, many different tools to do Ruby development on a Mac, and there is no single right way. These are just the ones I use, and it's possible it may change a bit (I'll make a new post if that happens). I also neglected some tools, like Git. If I went into talking about Git, I'd get very very sidetracked.
0 notes
jpweb12 · 8 years ago
Text
My Journey Of Learning Programming Through Flatiron School #33
My name is Mason Ellwood, and I’m currently working on Flatiron School’s Online Full Stack Web Development Program. Each week, I’ll be writing about my experience, what I’m learning, and tips on learning to code.
It is official! I have passed the Ruby CLI Gem Final project. If you would like to try out my gem for yourself please visit https://rubygems.org/gems/masonellwood_cli_app_two. Rubygems.com is basically the https://www.npmjs.com/ for the Ruby community. If you do not know what NPM is either then RubyGems is basically a place where you can find, install, and publish Ruby Gems that you build yourself. It is an open source project, kind of like Github, where users can store and use other people’s gems for their own purposes.
“RubyGems is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries (in a self-contained format called a “gem“), a tool designed to easily manage the installation of gems, and a server for distributing them.” wikipedia
Thank you, Wikipedia.
If you would like to view the screen share of me running through my application, check out the video below.
Sorry for me speaking so quietly in the video, I was in the waiting room at MD Anderson, so I had to keep it down.
To install my Gem to your Gemfile please type into your terminal
gem install masonellwood_cli_app_two
Depending on if you have your local environment setup because most likely you will receive an error of “YOU ARE NOT ALLOWED TO WRITE TO THIS FILE” or something along those lines. This is a mac default so unless you set up RVM or some alternative type of Gem management tool, then most likely you should not change the reader writer permissions of your lab without knowing what you are going.
If you would like to set up your own local environment, you should seriously check out the setup instructions that The Flatiron School has published, they are awesome and they are the same ones that I used to set up my machine. (https://learn.co/manual_setup)
If the file installed without a hiccup then simply type into your terminal
Weather-channel
And this should prompt you to my program which will run through a CLI or command line interface.
Through this project I have learned to speak and present code to a viewer, refactor code on the spot, build a ruby gem, publish a ruby gem, change file permissions, create a local development environment, use bundler to create a base template for gem construction, deeper understanding of debugging, require and use other gems to build projects from, and to be able to approach a problem and think through the steps to solve that problem.
The Professors that have worked with me to complete this project have been amazing, and I thank them for their time and patience with anything that I needed help with. Throughout this project I was finally able to see that I can do this, I can be the developer I want to become, and this decision to become a developer is not over my head.
Read More at My Journey Of Learning Programming Through Flatiron School #33
from IT Feed https://webdesignledger.com/my-journey-of-learning-programming-through-flatiron-school-33/
0 notes
regulardomainname · 8 years ago
Text
My Journey Of Learning Programming Through Flatiron School #33
My name is Mason Ellwood, and I’m currently working on Flatiron School’s Online Full Stack Web Development Program. Each week, I’ll be writing about my experience, what I’m learning, and tips on learning to code. It is official! I have passed the Ruby CLI Gem Final project. If you would like to try out my gem for yourself please visit https://rubygems.org/gems/masonellwood_cli_app_two. Rubygems.com is basically the https://www.npmjs.com/ for the Ruby community. If you do not know what NPM is either then RubyGems is basically a place where you can find, install, and publish Ruby Gems that you build yourself. It is an open source project, kind of like Github, where users can store and use other people’s gems for their own purposes. “RubyGems is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries (in a self-contained format called a “gem“), a tool designed to easily manage the installation of gems, and a server for distributing them.” wikipedia Thank you, Wikipedia. If you would like to view the screen share of me running through my application, check out the video below. Sorry for me speaking so quietly in the video, I was in the waiting room at MD Anderson, so I had to keep it down. To install my Gem to your Gemfile please type into your terminal gem install masonellwood_cli_app_two Depending on if you have your local environment setup because most likely you will receive an error of “YOU ARE NOT ALLOWED TO WRITE TO THIS FILE” or something along those lines. This is a mac default so unless you set up RVM or some alternative type of Gem management tool, then most likely you should not change the reader writer permissions of your lab without knowing what you are going. If you would like to set up your own local environment, you should seriously check out the setup instructions that The Flatiron School has published, they are awesome and they are the same ones that I used to set up my machine. (https://learn.co/manual_setup) If the file installed without a hiccup then simply type into your terminal Weather-channel And this should prompt you to my program which will run through a CLI or command line interface. Through this project I have learned to speak and present code to a viewer, refactor code on the spot, build a ruby gem, publish a ruby gem, change file permissions, create a local development environment, use bundler to create a base template for gem construction, deeper understanding of debugging, require and use other gems to build projects from, and to be able to approach a problem and think through the steps to solve that problem. The Professors that have worked with me to complete this project have been amazing, and I thank them for their time and patience with anything that I needed help with. Throughout this project I was finally able to see that I can do this, I can be the developer I want to become, and this decision to become a developer is not over my head. Read More at My Journey Of Learning Programming Through Flatiron School #33 http://dlvr.it/PXQ6Z8 www.regulardomainname.com
0 notes