mercredi 23 février 2022

Ruby RSpec for checking exception not passing

I have an rspec where I am testing if an exception thrown from method1 is rescued and raised again in method2 (method2 calls method1). But the RSpec seems to fail. I have debugged through the code and can see the exception being raised.


  it 'should check for exceptions raised' do
      allow(TestClass).to receive(:method1) do
        raise ::Test::CustomException.new(1, 'test.csv.gz', StandardError.new('test exception'))
      end
      expect(@test_class.method2).to raise_error(Test::CustomException)
  end


  method2
   tester["key_list"].each do |key|
        begin
        TestClass.method1(key, self.id) do |row|
          #do something
        end
        rescue Test::CustomException => exception
          raise ::Test::CustomException.new(self.id, key, exception)
        end
  end

  method1
     begin
         #do something
     rescue => exc
        raise ::Test::CustomException.new(id, key, exc)
     end
  end

CustomException as the name says is a custom defined exception that takes in 3 parameters

Error:

Failures:

  1. should check for exceptions raised Failure/Error: expect(@test_class.method2).to raise_error

    Test::CustomException: true test_class.rb:146:in `rescue in block method'

vendredi 18 février 2022

LMS Integration (Moodle) in ruby

I want to integrate LMS in our application, as I am new to this. it would be great if some one share the knowledge of LMS architecture.

Likeour customer want to access our content via LMS/LTI how we can proceed and what key features we need to consider and what are the challenges.

how to check if a method is called or not in rspec

as i try to check internally my method was calling or not in rspec but it got the following errors


context "#Meeting_schedule" do
    let(:meeting_schedule) { FactoryGirl.create(:meeting_schedule,:time=>"morning",:schedule_name=>"planned_meet", :schedule_info=>[{ "from"=>"00:00", "to"=>"00:01"}]) }

    it "if the same schedule was created again dont save it again" do 
      schedule.save
      params = {:time=>"morning",:schedule_name=>"planned_meet", :schedule_info=>[{ "from"=>"00:00", "to"=>"00:01"}]}
      meeting_schedule.create_or_update_meeting_schedule(params)
      expect(meeting_schedule).to receive(:updating_the_user)
    end
  end


i got the following error

Failure/Error: expect(meeting_schedule.create_or_update_meeting_schedule(params)).to receive(:updating_the_user)
       (#<Meeting_schedule:0x0055dbaf0da710>).updating_the_user(*(any args))
           expected: 1 time with any arguments
           received: 0 times with any arguments
     # ./spec/models/meeting_schedule_spec.rb:122:in `block (4 levels) 

so what was wrong in my code?

my method

def create_or_update_meeting_schedule(params)
   self.attributes = params
   if self.changed and self.save
     updating_the_user
   end
   self
end

can anyone help me out

jeudi 17 février 2022

SSL_connect returned=1 errno=0 state=error: certificate verify failed in Mac

I have a rails app integrated with a third party web service. It has been working perfectly since the initial development for years. For some unexpected reason, suddenly it has stopped working. I would say we haven´t changed anything in our code. The only thing that it can be related is the fact that our SSL certificate expired and we renewed it.

The fact is that I´m getting this error when calling the web service request:

E, [2022-02-17T19:53:25.385435 #32501] ERROR -- : SSL_connect returned=1 errno=0 state=error: certificate verify failed
E, [2022-02-17T19:53:25.385876 #32501] ERROR -- : /Users/Rober/.rvm/gems/ruby-2.4.9/gems/httpi-2.4.4/lib/httpi/adapter/httpclient.rb:28:in `rescue in request'
E, [2022-02-17T19:53:25.386103 #32501] ERROR -- : /Users/Rober/.rvm/gems/ruby-2.4.9/gems/httpi-2.4.4/lib/httpi/adapter/httpclient.rb:24:in `request'
E, [2022-02-17T19:53:25.386358 #32501] ERROR -- : /Users/Rober/.rvm/gems/ruby-2.4.9/gems/httpi-2.4.4/lib/httpi.rb:161:in `request'
E, [2022-02-17T19:53:25.386658 #32501] ERROR -- : /Users/Rober/.rvm/gems/ruby-2.4.9/gems/httpi-2.4.4/lib/httpi.rb:127:in `get'
E, [2022-02-17T19:53:25.386909 #32501] ERROR -- : /Users/Rober/.rvm/gems/ruby-2.4.9/gems/wasabi-3.5.0/lib/wasabi/resolver.rb:43:in `load_from_remote'
E, [2022-02-17T19:53:25.387150 #32501] ERROR -- : /Users/Rober/.rvm/gems/ruby-2.4.9/gems/wasabi-3.5.0/lib/wasabi/resolver.rb:33:in `resolve'
E, [2022-02-17T19:53:25.387349 #32501] ERROR -- : /Users/Rober/.rvm/gems/ruby-2.4.9/gems/wasabi-3.5.0/lib/wasabi/document.rb:142:in `xml'
E, [2022-02-17T19:53:25.387606 #32501] ERROR -- : /Users/Rober/.rvm/gems/ruby-2.4.9/gems/wasabi-3.5.0/lib/wasabi/document.rb:160:in `parse'
E, [2022-02-17T19:53:25.387887 #32501] ERROR -- : /Users/Rober/.rvm/gems/ruby-2.4.9/gems/wasabi-3.5.0/lib/wasabi/document.rb:147:in `parser'
E, [2022-02-17T19:53:25.388162 #32501] ERROR -- : /Users/Rober/.rvm/gems/ruby-2.4.9/gems/wasabi-3.5.0/lib/wasabi/document.rb:64:in `soap_actions'
E, [2022-02-17T19:53:25.388432 #32501] ERROR -- : /Users/Rober/.rvm/gems/ruby-2.4.9/gems/savon-2.12.0/lib/savon/operation.rb:22:in `ensure_exists!'
E, [2022-02-17T19:53:25.388696 #32501] ERROR -- : /Users/Rober/.rvm/gems/ruby-2.4.9/gems/savon-2.12.0/lib/savon/operation.rb:15:in `create'
E, [2022-02-17T19:53:25.388955 #32501] ERROR -- : /Users/Rober/.rvm/gems/ruby-2.4.9/gems/savon-2.12.0/lib/savon/client.rb:32:in `operation'
E, [2022-02-17T19:53:25.389214 #32501] ERROR -- : /Users/Rober/.rvm/gems/ruby-2.4.9/gems/savon-2.12.0/lib/savon/client.rb:36:in `call'

I´m not skilled at all with certificates. So, I found this post: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed and basically I´m trying almost everything that I see, but nothing is working. I have the issue in development on Mac running ruby 2.4.9p362 (2019-10-02 revision 67824) [x86_64-darwin19] and Rails 5.2.4 and also in production in Ubuntu. But, I will try to fix it in development first.

dimanche 13 février 2022

rails active storage file uploading

my files were uploading fine and suddenly i am encountering this issue

Errno::EISDIR (Is a directory @ rb_sysopen - /home/dev/Desktop/project/storage/):

I have no idea what is happening

mercredi 9 février 2022

split a row or column or cell in ruby spreadsheet gems

How do I split a row or column or cell in ruby spreadsheet gems? sheet.split_cells(start_row, start_col, end_row, end_col)

I tried this getting undefined method error

how to change a profile pic hash value in ruby on rails

when we change the profile pic the hash key value is not changing remaining same for all the changed images, due to this cache issue profile pic is not getting change immediately.

def link_to_profile_header(user)
  if user.avatar.exists?
    image_tag(user.avatar.url(:medium), :alt => user.display_name, :class=>"img-circle nav-profile-pic")
  else
    content_tag(:div, user.first_name[0,1].capitalize + "" + user.last_name[0,1].capitalize, :class=>"circle-new #{random_class_name_for_user_default_image}")
  end
end

mardi 8 février 2022

Issue running my Rails server: Error in reactor loop escaped: System error: Success - 0

When I launch my rail server and then go to localhost:3000, My localhost just keeps loading without any error messages. If I try to reload the page, i get Error in reactor loop escaped: System error: Success - 0 (Puma::MiniSSL::SSLError) What is this error and how can i fix it?

lundi 7 février 2022

rails console cannot load such file but with one letter capitalized it works?

ruby version: 2.7.3

Hi! I am very puzzled...

I have a file path company/service/v1/my_proto_pb.rb. When I tried to require it in rails console it gives the cannot load such file error:

2.7.3 :005 > require "company/service/v1/my_proto_pb"
Traceback (most recent call last):
/usr/local/rvm/gems/ruby-2.7.3/gems/bootsnap-1.9.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:34:in `require': cannot load such file -- company/service/v1/my_proto_pb (LoadError)

HOWEVER when I capitalize the letter v to V it works:

2.7.3 :006 > require "company/service/V1/my_proto_pb"
 => true

But the .rb and .rbi file both exist in the v1 folder. Why??? I cannot change the letter in code as it's auto-generated. Where exactly does the require look for and how can i check if what's being required exists or not?

Thank you!

Ruby POST request with an external file attached

Since a week ago, I've been facing this issue, but I've not found the solution. When I send a POST request to an API, it returns

{"errors":[],"message":"A file or fileKey is required","status":400,"type":"BadRequestError"}

This is what I am trying to do:

    url = URI("https://xxx.xxx/v2/attachments")

    https = Net::HTTP.new(url.host, url.port)
    https.use_ssl = true

    request = Net::HTTP::Post.new(url)
    request["X-Application"] = "IDENTIFICATION"
    form_data = [['name', 'my-file.pdf'],['parcelId', '123'],['file', Net::HTTP.get(URI("http://www.africau.edu/images/default/sample.pdf"))]]
    request.set_form form_data, 'multipart/form-data'
    response = https.request(request)
    puts response.read_body

Is something wrong with the way I attach the file?

dimanche 6 février 2022

Add label to select_tag ruby

i have using label like this in the first input

f.input :title, label: 'Título

enter image description here

in the second input i have

select_tag 'front_dashboard_formula[layout_type]', options_for_select(FrontDashboardFormula.layouts), :include_blank => true

how to use label, equal to the first input

Rails : ActionController::UnknownFormat when adding image in ajax

hi i am trying to upload a form via ajax , the form works fine when i send without a attachment but when i attach it gives ActionController::UnknownFormat , I am using activestorage migration for this

@items_list= Item.where(:category_id => params[:item][:category_id])
  respond_to do |f|
    f.js
  end```

Ruby Mastermind White Pegs Count

I'm a newbie to Ruby programming. It is my second OOP project. I have trouble with counting white pegs under certain condition. And Wikipedia says that:

-A black key peg is placed for each code peg from the guess which is correct in both colour and position.
-A white key peg indicates the existence of a correct colour code peg placed in the wrong position.

I believe that my black peg condition is true, but I can not count my white pegs correctly. Here is the function code piece:

def check_guess(code, guess)
    @white_peg = 0
    @black_peg = 0
    index = 0
    while index < code.length
      if code[index] == guess[index]
        @black_peg += 1
        puts "Black => #{@black_peg}"
      elsif guess.any? { |c| code.include?(c) } && code[index] != guess[index]
        @white_peg += 1
        puts "White => #{@white_peg}"
      end
      index += 1
    end
    puts "There are #{@white_peg} white peg and #{@black_peg} black peg"
    is_win?
  end
end

I'm invoking above function inside this condition

  def codebreaker_attempt
    guess_array = @codebreaker.guess_code
     @codebreaker.check_guess(@codemaker.code, guess_array)
  end

And lastly my game method is here for stop the game

def play
    codemaker_choose
    (1..12).each do |i|
      puts "#{i}. GUESS\n"
     #puts "Guess array is #{guess}"
      break if codebreaker_attempt == true
    end

I'm leaving here repl.it repo of all my codes if you want to inspect. https://replit.com/@Burakkepuc/Mastermind#main.rb