These are chat archives for opal/opal

13th
Nov 2015
Brady Wied
@wied03
Nov 13 2015 05:49
@ryanstout - another twist I've been working on opal-rspec that might be of use to you - https://github.com/wied03/karma-opal-rspec. Main aim is to speed up debugging and take advantage of how many things hook into Karma (and Karma's convenient spin up/throw away browser stuff).
Yaroslav Kukharuk
@brbrr
Nov 13 2015 07:56

Hi guys! I'm looking for a way to use external gems with opal i.e. rest-client. Im quite sure that I requiring that gem incorrectly:

require 'rest-client'
Opal.append_path '/Users/brbrr/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/rest-client-1.8.0/lib'

Another question is how to make compiled js to run in console(node/browser)

Martin Becker
@Thermatix
Nov 13 2015 10:51
if you want it to work for opal you'll need to port it to opal compatible ruby, some of the code won't be compatible; for example dynamic requires won't work so will have to be changed to require_tree directive.
second of all if you want to make HTTP requests I'd recommend using the HTTP client that comes with opal-browser it's super easy to use, here, in fact I'll post you my request module that I built on top of it:
require 'browser/http'
class Request
  class << self

    %w{get post delete put patch head}.each do |http_verb|
      define_method http_verb do |*args,&block|
        self.make(http_verb,*args,&block)
      end
    end

    def make(type,url,payload=nil, &block)
      # data_hash = {dataType: 'json'}
      # data_hash.merge!({payload: payload.to_json})if payload
      Browser::HTTP.send type, url, payload.to_json do |request|
        request.content_type 'application/json'
        if block_given?
          block.call(*request)
        else
          request
        end
      end
    end
  end
end
Why did I build this? Because I want to ensure anything is correctly formatted and I can do that here so now every method will share any current and future formatting requirements.
Yaroslav Kukharuk
@brbrr
Nov 13 2015 11:01
@Thermatix isn't require 'browser/http' is dynamic require? actually rest-client was used as an example, and Im just playing around with opal, not trying to solve something. In what way I could just use gem as-is?
Martin Becker
@Thermatix
Nov 13 2015 12:42
no, dynamic require means for example iterating over a Dir['.'] blob and requiring each one
Yaroslav Kukharuk
@brbrr
Nov 13 2015 12:45
anyway generated js is not works for me:
require 'rest-client'
Opal.compile("RestClient.get('http://goo.gl'")
Martin Becker
@Thermatix
Nov 13 2015 12:46
try:
script = <<- OPAL
  require 'rest-client
   RestClient.get('http://goo.gl'
OPAL
Opal.compile(script)
Though, I'm not sure that's the best way to do it, have you tried starting an opal server like in the example and then loading the scripts as assets and then lettings sprockets handle file processing and serving?
Yaroslav Kukharuk
@brbrr
Nov 13 2015 13:03
ok, now how could I execute that js with opal included? sorry about my kinda silly questions. I suppose I need to include opal.js lib in to js repl on machine where RestClient is reachable. so using opalrb.org/try is not an option
Martin Becker
@Thermatix
Nov 13 2015 13:08

you might want to try following this example to set a basic opal environment up:
https://github.com/opal/opal/tree/master/examples/sinatra

use this to append your paths to the opal server:

opal = Opal::Server.new {|s|
  s.append_path 'assets/javascripts/'
  s.main = 'application'
}

set up opal variables:

sprockets   = opal.sprockets
prefix      = '/assets/javascripts'
maps_prefix = '/__OPAL_SOURCE_MAPS__'
maps_app    = Opal::SourceMapServer.new(sprockets, maps_prefix)

# Monkeypatch sourcemap header support into sprockets
::Opal::Sprockets::SourceMapHeaderPatch.inject!(maps_prefix)

set up the opal header

opal_header = ::Opal::Sprockets.javascript_include_tag('application', sprockets: sprockets, prefix: prefix, debug: true)

and then set up a basic sinatra root:

get '/' do
  <<-HTML
    <!doctype html>
    <html>
      <head>
        <meta charset="utf-8">
        #{opal_header}
      </head>
      <body>
      </body>
    </html>
  HTML
end
Martin Becker
@Thermatix
Nov 13 2015 13:28
how do I push a value from embedded javascript to an opal function and or object?
Martin Becker
@Thermatix
Nov 13 2015 13:55
nvm figured it out
Yaroslav Kukharuk
@brbrr
Nov 13 2015 14:10
ah, I didnt look in to config.ru. Thanks for pointing me!
giuan
@giuan
Nov 13 2015 14:48
@elia, @meh
the problem with require is only on Mac. Linux and windows are ok.
The file ./main.rb require ./lib/hello.rb
running with opal main.rb
Dany M
@dany-on-demand
Nov 13 2015 15:12
This message was deleted
`user = "Dongs all over!"`

puts `user`

`array = ["dongs", "dings"]`

puts `array`
array = `array`
array.push("mobs")
puts array
opal is nice!
Martin Becker
@Thermatix
Nov 13 2015 15:36
lol
Ryan Stout
@ryanstout
Nov 13 2015 20:22
@wied03 thanks for the info. I haven't used karma, does it use webdriver under the hood?
Brady Wied
@wied03
Nov 13 2015 22:05
@ryanstout - No. It's not an integration test tool. It's solely for JS unit testing. Since it's good at spinning up browsers and several tools I use can hook into it, I'm exploring using it for debugging instead of the opal-rspec rake task or opal-rspec browser runner
You pick what it should run tests in (Chrome, Phantom, Firefox, etc.). A lot of people use it to run their Jasmine tests
Ryan Stout
@ryanstout
Nov 13 2015 22:11
cool
so it doesn't use webdriver under the hood?
MichaƂ Kalbarczyk
@fazibear
Nov 13 2015 22:23
Why #1188 fails ? How can I fix it ?
meh.
@meh
Nov 13 2015 23:32
fazibear, I can't even see the build log, travis doesn't load
Brady Wied
@wied03
Nov 13 2015 23:36
@ryanstout - No it doesn't. As far as I know, webdriver would be useful if you needed capybara type stuff to manipulate web pages, but that would be integration testing
meh.
@meh
Nov 13 2015 23:37
ryanstout, found the issue with allowing multiple thens
trace won't work anymore
and I like trace
Ryan Stout
@ryanstout
Nov 13 2015 23:37
@meh what's trace?
meh.
@meh
Nov 13 2015 23:38
ryanstout, a method on Promise
it allows you to fetch every resolution backwards
so you can do something like a map through promises
Ryan Stout
@ryanstout
Nov 13 2015 23:39
@meh humm
@meh why doesn't it work with multiple thens?
meh.
@meh
Nov 13 2015 23:40
ryanstout, because it uses @prev
so the promises have to be linear
and not a tree
with multiple @next it becomes a tree
and @prev is non-obvious
because it will become a reverse Promise::When
Ryan Stout
@ryanstout
Nov 13 2015 23:43
can you make it work with .then! and not with .then :-)
@meh I guess I don't quite understand, what's your use case for trace?
@meh did you get it working with multiple .then's? (if so, can I just run my own version of Promise in volt with multiple then's and you can leave opal's the same?)
meh.
@meh
Nov 13 2015 23:46
ryanstout, When is broken too with multiple thens
I'm still trying to fix it
Ryan Stout
@ryanstout
Nov 13 2015 23:49
thanks