These are chat archives for opal/opal

5th
Nov 2015
Michał Kalbarczyk
@fazibear
Nov 05 2015 10:44
Inesita's livereload works! https://www.youtube.com/watch?v=7h6wn8tF9Rw
Needs lot of cleanups but it works :tada:
Vais Salikhov
@vais
Nov 05 2015 11:09
@fazibear sweet!
Elia Schito
@elia
Nov 05 2015 11:09
@fazibear retweeted!
Martin Becker
@Thermatix
Nov 05 2015 12:21
whats inestia?
Martin Becker
@Thermatix
Nov 05 2015 12:31
is there an opal library for encrypting data before transport via ajax?
meh.
@meh
Nov 05 2015 12:31
@/alll need opinions on opal/opal#1181
Martin Becker
@Thermatix
Nov 05 2015 12:31
that also matches equivalent ruby gem library?
meh.
@meh
Nov 05 2015 12:32
Thermatix, not that I know of
Martin Becker
@Thermatix
Nov 05 2015 12:32
sigh
can you point me in the right direction for encryption/decryption?
meh.
@meh
Nov 05 2015 12:33
what kind of encryption do you need?
Martin Becker
@Thermatix
Nov 05 2015 12:35
I want to encrypt form data before sending to the back end
I also want to know of a good js password hasing library
Vais Salikhov
@vais
Nov 05 2015 12:41
@Thermatix what's wrong with using https? Why encrypt in JS? This is generally considered a Bad Idea AFAIK
Martin Becker
@Thermatix
Nov 05 2015 12:44
does sinatra/padrino suppourt https out of the box?
Vais Salikhov
@vais
Nov 05 2015 13:12
@Thermatix yes and no. HTTPS is usually handled by the web server (apache, nginx) that proxies to your application server (webrick, thin, puma). If you expose your application server directly to the interwebs (without a front proxy), you can make webrick/thin/puma handle https for you. This is not how it's usually done for production apps, but each of these rack servers supports https.
Martin Becker
@Thermatix
Nov 05 2015 13:13
I c so beyond changing the protocol to https, I don't have to do anything else?
how do I ensure that the protocol is set to http when doing an ajax request in opal?
I'm using the jquery library for ajax requests
Elia Schito
@elia
Nov 05 2015 13:17
@Thermatix you still need the certificate, I suggest finding a tutorial of some kind depending on what you need (e.g. https://duckduckgo.com/?q=https+ruby+server+setup&t=osx&ia=qa)
Vais Salikhov
@vais
Nov 05 2015 13:31
@Thermatix correct, once https is set up on the server side, all you have to do it use https:// urls instead of http://. Opal does not care about that, and neither does jquery. If you load the initial html page over https, you can make ajax calls to relative urls (no need to specify https etc), and these requests will be going through https. Hope this makes sense. In addition to what @elia said, you can try to use a self-signed SSL certificate to test with. The browser will complain that the connection is not secure (since your self-signed certificate does not come from a "trusted" source), but that's ok for testing purposes.
Martin Becker
@Thermatix
Nov 05 2015 13:34
can I use opal to then ensure that the protocol is set to https in the first place?
meh.
@meh
Nov 05 2015 13:34
just do not expose a clear-text port
Vais Salikhov
@vais
Nov 05 2015 13:42
I think you can check this client-side, but a better approach is what @meh suggested: either do not expose a clear-text port (http port 80) at all, or check on the server-side if a request is using SSL and if not, redirect to the https URL.
@Thermatix I think the general theme here is: do not make encryption a client-side concern in any way :smile:
Martin Becker
@Thermatix
Nov 05 2015 13:47
ok
Vais Salikhov
@vais
Nov 05 2015 14:04
@elia re the opal -R server my_code.rb trick that you mentioned, I am getting Empty reply from server for http://localhost:3000 What am I missing?
Elia Schito
@elia
Nov 05 2015 14:05
@vais not sure, I used it with a pipe: pbpaste | opal -R server, does it work that way?
Vais Salikhov
@vais
Nov 05 2015 14:07
let me try...
Vais Salikhov
@vais
Nov 05 2015 14:14
@elia my bad, the issue seems to be with my vagrant forwarded port (I decided to use Vagrant running Ubuntu even on my Mac, so I have the same environment everywhere. The jury is still out on whether this was a good idea :wink2:)
As they say, you can always add another layer of indirection.
Elia Schito
@elia
Nov 05 2015 14:15
@vais go docker!
and eventually dind!
Ilya Bylich
@iliabylich
Nov 05 2015 14:16
@elia Is it possible to run generated tmp/mspec_node.rb this way? As I know, it uses some node-specific stuff...
Vais Salikhov
@vais
Nov 05 2015 14:16
@elia :weary:
@iliabylich maybe try mspec_phantom instead :wink:
Elia Schito
@elia
Nov 05 2015 14:19
@iliabylich rake should output the actual commands it runs and it should be fairly easy to reuse them adding that option (and probably remove the node/phantom specific -r requires)
Ilya Bylich
@iliabylich
Nov 05 2015 14:24
@elia wow. I'm so tired of debugging through node debug .... Thanks you so much :)
Vais Salikhov
@vais
Nov 05 2015 14:31
@elia hmm, so it's not my vagrant port forwarding, because other web things work (even just ruby -run -e httpd . -p 3000) It's gotta be something in that server cli runner, but wth? I'm not having a good day today, one mystery after another :smile: Thought maybe it's thin, but the same result with webrick. Better luck tomorrow perhaps.
Elia Schito
@elia
Nov 05 2015 14:39
@iliabylich the only regret is that I didn't publicize it more :)
@vais does it work from inside the machine? (pseudocode: vagrant ssh curl localhost:3000)
Vais Salikhov
@vais
Nov 05 2015 14:48
@elia yep, it does with curl inside the ssh session, but not from the outside (outside Chrome shows me the empty reply error).
I have used this vagrant setup with rails and sinatra before, using thin and webrick, so I have no idea what gives in this case.
Martin Becker
@Thermatix
Nov 05 2015 14:52
how do I do a setTimeout in opal without using native javascript?
Elia Schito
@elia
Nov 05 2015 14:55
@Thermatix you can use native (which is already required if you're using opal-jquery) in this way: $$.setTimeout(-> { callback code }, 500)
Martin Becker
@Thermatix
Nov 05 2015 14:56
thanks that worked
Ilya Bylich
@iliabylich
Nov 05 2015 15:08
@Thermatix As I remember, there is a method delay in opal-browser that takes a timeout and a block
Martin Becker
@Thermatix
Nov 05 2015 15:09
thanks but I don't really want to install an entire gem just for a single function
although...
it might be worth using over the jquery library
do I need jquery included for opal-browser?
Ilya Bylich
@iliabylich
Nov 05 2015 15:11
I don't think so, there is no opal-browser dependency in opal-jquery's gemspec
Martin Becker
@Thermatix
Nov 05 2015 15:12
I mean do I need the jquery library to use opal-browser
Ilya Bylich
@iliabylich
Nov 05 2015 15:13
No, opal-browser is a wrapper for a native browser functionality
Martin Becker
@Thermatix
Nov 05 2015 15:13
AH ok, thats what I wanted to know, thanks.
in that case I think I'm going to ditch the jquery library and switch to opal-browser
meh.
@meh
Nov 05 2015 15:19
Thermatix, opal-browser is cherry-pickable
if you just need delay, just require 'browser/delay'
and it will just be one class
Thermatix, if you find it's not cherry-pickable enough and it's bringing in stuff you don't want, open an issue and I can make it more granular
Martin Becker
@Thermatix
Nov 05 2015 15:21
... it's saying that Browser::HTTP doesn't exist...
meh.
@meh
Nov 05 2015 15:21
because it's cherry-pickable
require 'browser/http'
Martin Becker
@Thermatix
Nov 05 2015 15:21
i'm requiring 'browser' for the moment
meh.
@meh
Nov 05 2015 15:22
yes
require 'browser'; require 'browser/http'
Martin Becker
@Thermatix
Nov 05 2015 15:22
I c, so I can't just require 'browser'
meh.
@meh
Nov 05 2015 15:22
'browser' requires only the basic browser stuff
events, window, dom and cssom
if you need additional stuff you also have to require it
Martin Becker
@Thermatix
Nov 05 2015 15:23
I seee
and browser/delay is not a basic thing?
meh.
@meh
Nov 05 2015 15:23
nope
you have to require it too
Martin Becker
@Thermatix
Nov 05 2015 15:23
gotcha
hmmm I don't think it mentions that in the readme for opal-browser
meh.
@meh
Nov 05 2015 15:25
doubt it does
Martin Becker
@Thermatix
Nov 05 2015 15:25
shouldn't it?
meh.
@meh
Nov 05 2015 15:25
probably
:D
Martin Becker
@Thermatix
Nov 05 2015 15:36
why doesn't object.inspect return the object and it's attributes? only the object itself?
meh.
@meh
Nov 05 2015 15:36
what object?
Martin Becker
@Thermatix
Nov 05 2015 15:38

lets say I'm doing:

  Request.get("/api/forms/tree/#{id}") do |result|
        result.on :success do |response|
          puts response.inspect
        end
end

it only displays #<Request:0x2d6> and nothing else I have to do puts response.instance_variables to see anything and I'd still have to iterate over each one to find the value of each attribute (if they are a public attribute at all).

meh.
@meh
Nov 05 2015 15:38
Thermatix, because inspect isn't implemented for it, pull request welcome
Martin Becker
@Thermatix
Nov 05 2015 15:39
isn't .inspect a basic ruby object method that standardly returns an object and it's instance variables and there values?
Ilya Bylich
@iliabylich
Nov 05 2015 15:45
@Thermatix Not for every single object. For example, for Object - yes, for String - no :)
Martin Becker
@Thermatix
Nov 05 2015 15:46
This message was deleted
This message was deleted
lets try that again
I'm having some trouble sending JSON data to my server.
here's my request class that makes the request:
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, *data_hash do |response|
        puts response.inspect
        # results = [response.status_code,response.json]
        if block_given?
          block.call(*response)
        else
          response
        end
      end
    end
  end
end
and I'm using it like so:
  def handle_validation event
      value = event.target.value
      ::Request.post("/api/forms/validate/#{Form.tree[:meta][:id]}",format_validation(event)) do |result|
        result.on :success do |response|
          self.tree = response.json
          set_pair_validation_field_value true, value
          set_validation_messages 'correct', false
        end
        result.on :failure do |response|
          set_pair_validation_field_value false
          set_validation_messages response.json[:data][:message], true
        end
      end
    end
the problem is that the server is receiving the data like this and erroring:
unexpected token at 'dataType=json&payload=%7B%22meta%22%3A%7B%22context%22%3A%22form.validation.check%22%2C%20%22target%22%3A%22form.account%22%7D%2C%20%22data%22%3A%7B%22field%22%3A%22user%20name%22%2C%20%22value%22%3A%22%22%7D%7D':
all format_validation does is just make sure it's in the correct hash structure, nothing more
Ilya Bylich
@iliabylich
Nov 05 2015 15:55
meh.
@meh
Nov 05 2015 15:56
Thermatix, the problem is HTTP::Request is not a normal object
most of the stuff that you care about isn't in instance variables, but on the underlying native object
Ilya Bylich
@iliabylich
Nov 05 2015 15:58
When you pass a Hash to the send method, Browser::HTTP::Request encodes it as "key1=value1&key2=value2" (while you need json). The solution is: encode your data with to_json manually before passing it to Browser::HTTP.send
Martin Becker
@Thermatix
Nov 05 2015 15:59
I see
does it pick up on JSON automaticly so you don't need to tell it that your sending JSON?
automatically*
Ilya Bylich
@iliabylich
Nov 05 2015 15:59
@Thermatix The only http-related example in readme is a simple http get (without any parameters), so start reading the source :)
meh.
@meh
Nov 05 2015 15:59
Thermatix, nope
Martin Becker
@Thermatix
Nov 05 2015 16:00
so how do you tell it the content type? because hash[:payload] is a JSON string
meh.
@meh
Nov 05 2015 16:01
well, first of all you have to use post
Martin Becker
@Thermatix
Nov 05 2015 16:01
wait, I think I get it
meh.
@meh
Nov 05 2015 16:01
so just HTTP.post(url, hash)
Thermatix, there is documentation
almost everything has docs
Martin Becker
@Thermatix
Nov 05 2015 16:09
how do I set the content_type header on the request object when Browser::HTTP.post returns a promise and not the request object?
It seems strange that Browser::HTTP.post only takes a url and the payload but not the payload type, I don't see how to set the content_type when I don't get access to the request object directly nor can you pass it into the request object.
meh.
@meh
Nov 05 2015 16:10
Thermatix, read the docs
you just pass a block to post
it yields the Request object
Martin Becker
@Thermatix
Nov 05 2015 16:11
It doesn't, it returns a promise
meh.
@meh
Nov 05 2015 16:11
yields, not returns
HTTP.post(url, data) { content_type 'application/json' }
that will also return a promise
Martin Becker
@Thermatix
Nov 05 2015 16:12

when I do:

Request.get("/api/forms/tree/#{id}") do |result|  
  puts result
end

I get a promise not a request object is what I meant

meh.
@meh
Nov 05 2015 16:13
Thermatix, `HTTP.get("/api/forms/tree/#{id}").then { |result| }
for the post
HTTP.post(url, hash) { content_type 'application/json' }.then { |result }
Martin Becker
@Thermatix
Nov 05 2015 16:16
I realised what I was doing wrong, but It's now saying NoMethodError: undefined method 'content_type=' for #<Request:0x2d6>
meh.
@meh
Nov 05 2015 16:17
it's content_type not content_type=
it's made so you don't have to provide an argument
it's either HTTP.post(..) { |r| r.content_type 'application/json' } or `HTTP.post(..) { content_type 'application/json' }
Martin Becker
@Thermatix
Nov 05 2015 16:18
got it, thanks for the help.. I don't think I'm firing on all cylinders atm
meh.
@meh
Nov 05 2015 16:21
Thermatix, no worries, it ain't your fault, I suck at explaining things :P
Vais Salikhov
@vais
Nov 05 2015 16:41
@meh I think you're doing great :+1:
meh.
@meh
Nov 05 2015 16:49
@elia, @vais, I still think we're fucked one way or the other, I didn't consider the fact $$s is currently used for the inheret self of a block
this means we have to figure something else out
actually, I think it's easy
basically $$s remains, but instead of the common check being self = $$s || this, it becomes self = $$m.self || $$s || this
that way we still DO NOT have to ensure the caller try/catches it to reset it properly, and we get all the benfits
that should work
Vais Salikhov
@vais
Nov 05 2015 16:57
@meh sorry, what is $$m.self?
meh.
@meh
Nov 05 2015 17:10
vais, the new metadata stuff
it will probably be $m or $meta or $metadata in the prelude
Martin Becker
@Thermatix
Nov 05 2015 17:20
does opal support prepend ?
meh.
@meh
Nov 05 2015 17:20
nope
not yet
Martin Becker
@Thermatix
Nov 05 2015 17:21
damn
would have been awesome, oh well
would easily allow me to add method extentions
Martin Becker
@Thermatix
Nov 05 2015 17:28
umm question are cookies sent with every ajax request?
meh.
@meh
Nov 05 2015 17:28
yeah
by the browser
Martin Becker
@Thermatix
Nov 05 2015 17:28
kk
Elia Schito
@elia
Nov 05 2015 23:13

@/all 0.9.0.beta2 is out! here's the new stuff from beta1: https://gist.github.com/elia/72872e47b23423228f09

(would post the rubygems tweet but apparently I have some connection issues 😕 )

George Plymale II
@ylluminarious
Nov 05 2015 23:25
@elia my connection seems fine, so here's the tweet: https://twitter.com/rubygems/status/662404555448164352