<FromGitter>
<noaheverett_twitter> I'm trying to download a large binary file via HTTP::Client and save chunks of that file as they are received to disk. I understand I can do this by juts passing the responses body_io to a File.write call, but for my use-case I have to do it manually since I need to manipulate the data chunks as they come in.
<FromGitter>
<noaheverett_twitter> I've got it kind of working, but my data ends up mangled
<FromGitter>
<noaheverett_twitter> I believe my issue is, my buf is a set size and the data read bytes is different sometimes yet I'm saving the entire buf byte array each time. How do I save only the bytes to disk received? or am I way off? Thanks in advance!
<FromGitter>
<tenebrousedge> @noaheverett_twitter can you read either line-by-line, or using `gets(1024)` ?
<FromGitter>
<tenebrousedge> and what exactly is that bit-munging intended to do?
<FromGitter>
<noaheverett_twitter> @tenebrousedge I'll try gets / read by line, but my assumption is that since it's a binary file those wouldn't work? the bit-munging is basically sending the bytes back out to a different HTTP connection plus some reading on the initial bits to see what kind of data it is
djuber has quit [Remote host closed the connection]
djuber has joined #crystal-lang
djuber has quit [Ping timeout: 240 seconds]
<alexherbo2>
is there a way to write in append mode?
<alexherbo2>
File.open(get_history_path, 'a') do |file|
<alexherbo2>
file.puts program
<alexherbo2>
end
<FromGitter>
<Blacksmoke16> Yes like that
<alexherbo2>
how to do lazy chaining? list.sort.uniq
<alexherbo2>
it seems list.each.sort.uniq does not work
<FromGitter>
<Blacksmoke16> Paging @tenebrousedge
<FromGitter>
<Blacksmoke16> Would probably need an iterator
<FromGitter>
<tenebrousedge> :D iterators
<FromGitter>
<tenebrousedge> @noaheverett_twitter `gets` for a set number of bytes will work
<FromGitter>
<tenebrousedge> and you shouldn't need to do `to_unsafe` to inspect the initial bits
<FromGitter>
<tenebrousedge> alexherbo2, how is lazy sorting supposed to work?
<FromGitter>
<tenebrousedge> I think you want to simply use a `Set`
<FromGitter>
<noaheverett_twitter> Ok thank you for the help, greatly appreciated!
<FromGitter>
<tenebrousedge> @noaheverett_twitter let us know if you have any other issues with that
ur5us has quit [Ping timeout: 240 seconds]
<FromGitter>
<watzon> I can't find away to check if a constant is an annotation
<FromGitter>
<Blacksmoke16> hm?
<FromGitter>
<watzon> Second thought, I think I'm doing this wrong
<FromGitter>
<Blacksmoke16> π
<FromGitter>
<watzon> Basically the idea I'm having right now is to allow a handler to export a list of annotations (will probably just be one in most cases). Then those annotations will be looped over and the logic for it automatically generated based on the handler class that's exporting it.
<FromGitter>
<watzon> I don't know that this will be necessary, but it doesn't look like this works https://carc.in/#/r/8le2
<FromGitter>
<watzon> Hmm interesting. More than anything I just wanted to know if it was possible, just in case I do need to do that
<FromGitter>
<watzon> I do have a need to store a list of annotations in an array though to keep track of which annotations have been registered by a handler already, but I can't do `annotations = [] of Annotation`
<FromGitter>
<watzon> I guess I could use the string representation
<FromGitter>
<Blacksmoke16> along with what it can be added to
<FromGitter>
<watzon> Hmm yeah I'm looking at them
<FromGitter>
<Blacksmoke16> so its just a type so you can do `instanceof` etc, and the constructor just gets an assoc array of arguments
<FromGitter>
<watzon> I do like implementations where an annotation is just a class or a function that wraps something
<FromGitter>
<watzon> Python does the same
<FromGitter>
<watzon> And so does TypeScript
<FromGitter>
<Blacksmoke16> thats why this is a bit more troublesome
<FromGitter>
<Blacksmoke16> otherwise we could add a method to iterate over all annotations on a property/method, and do like `if ann.is_a? Constraint`
<FromGitter>
<watzon> Right?
<FromGitter>
<Blacksmoke16> but since there isnt that bond between the logic of the annotation and the annotation itself have to get a bit more creative
<FromGitter>
<watzon> That would be so nice
<FromGitter>
<Blacksmoke16> options atm are like
<FromGitter>
<Blacksmoke16> 1) Use an annotation to store the metadata of the constraint β 2) Use a constant to store the annotation it maps to β 3) Do something with the name of the constraint, like map `BlankConstraint` to `Blank` annotation
<FromGitter>
<Blacksmoke16> and also how to best map a constraint to the type it should be validated by
<FromGitter>
<watzon> Do we know if the maintainers are open to completely changing the implementation?
<FromGitter>
<watzon> Something makes me doubt it
<FromGitter>
<watzon> But man would it be nice
<FromGitter>
<Blacksmoke16> probably one of those just needs done?
<FromGitter>
<Blacksmoke16> i.e. someone to implement it
<FromGitter>
<ImAHopelessDev_gitlab> i think that's either 1) beautiful, or 2) a monstrosity . i'm going to go with: a beautiful monstrosity
<FromGitter>
<watzon> Yeah basically lol
<FromGitter>
<watzon> I know there's some cleanup I could do
<FromGitter>
<ImAHopelessDev_gitlab> i just alt-tabbed to gitter and was like O_o
<FromGitter>
<ImAHopelessDev_gitlab> going to work for 3 days, not coding for another 3 bcz busy doing other stuff, then coming back to your code: wtf i wrote all this?? when? ππ
<FromGitter>
<watzon> If anyone wants to help me clean it up I'd be more than open to suggestions. I hate building a proc from a string, but it seemed like the most concise way to do it.
<FromGitter>
<shaurya-xyz> Hi, Does anyone know of good resource to understand how create URI Scheme ? Thanks β β I am looking at a custom URI scheme such as this custom://{resource} β β Server would serve using the above endpoint and client would have to connect the custom endpoint [https://gitter.im/crystal-lang/crystal?at=5e4e8896dafa3029f640f524]
<FromGitter>
<shaurya-xyz> Any documentation or resource in any language would be helpful
<FromGitter>
<tenebrousedge> certificate unknown suggests that it wasn't signed by any trusted authority
<FromGitter>
<shaurya-xyz> @mavu the error doesnβt seem to descriptive but usually i have seen this on golang when the certificate authority is unknown or the cert has expeired
<FromGitter>
<Blacksmoke16> er, but your issue is more so using diff types
<FromGitter>
<Blacksmoke16> hmm
<FromGitter>
<mavu> @tenebrousedge Ah, that makes sense, as I'm using a self signed cert. β is there a way to suppress this message? I don't want it to spam my logfiles, and for this software there is no way to use a real certificate
<FromGitter>
<tenebrousedge> `//` is int division, `/` is float division, π is pizza division
<FromGitter>
<shaurya-xyz> @Blacksmoke16 yeah typeof as crystal docs
<FromGitter>
<noaheverett_twitter> Anyone know why this form of file writing doesn't work as expected? The first block of data is always the only one written
<FromGitter>
<Blacksmoke16> versus the method above it, which is the same thing just without the `ensure f.close`
<FromGitter>
<noaheverett_twitter> ah ok
<FromGitter>
<Blacksmoke16> id try closing the file, or `flush` maybe
<FromGitter>
<Blacksmoke16> closing it would inherently flush, or really just use the block version if you dont need the file for anything else
<FromGitter>
<noaheverett_twitter> ahhh you're right, I needed to do `f.close` after I was done writing :facepalm:
<FromGitter>
<noaheverett_twitter> thank you!
<FromGitter>
<Blacksmoke16> np, again the block version is better if thats all you need to do
<FromGitter>
<noaheverett_twitter> out of curiosity, why is the block preferred?
<FromGitter>
<noaheverett_twitter> is there a performance gain?
<FromGitter>
<Blacksmoke16> less chance of you forgetting to close the file
<FromGitter>
<tenebrousedge> ^
<FromGitter>
<noaheverett_twitter> oh haha
<FromGitter>
<noaheverett_twitter> good to know thank you
<FromGitter>
<Blacksmoke16> which could lead to too many open files error
<FromGitter>
<wout> I have a class which includes `JSON::Serializable` and created an instance of it using `.from_json`. What would be the best way to update the same instance, having a new json string with the same keys?
<FromGitter>
<Blacksmoke16> not sure i follow
<FromGitter>
<tenebrousedge> I would create a new struct, preferably
<FromGitter>
<Blacksmoke16> oh so like given a new JSON string update the values of the obj?
<FromGitter>
<tenebrousedge> unless there were a really good reason to use a mutable class, I would avoid that
<FromGitter>
<Blacksmoke16> since you check explicitly for `proc`
<FromGitter>
<watzon> Ahh yeah
<FromGitter>
<Blacksmoke16> could prob clean it up by doing like `for handler in Tourmaline::Handler.subclasses.select { |h| h.has_constant? :ANNOTATION }`
<FromGitter>
<watzon> Ahh yeah, good idea
<FromGitter>
<Blacksmoke16> @tenebrousedge I do have a use case where i need something like that
<FromGitter>
<Blacksmoke16> i.e. need to load a record from the db, apply the properties of a new property, and return the original from the db
<FromGitter>
<tenebrousedge> @Blacksmoke16 I believe you
<FromGitter>
<Blacksmoke16> would be nicer if the serializer could handle providing the db obj and then just calls the setter for each property on it, using the getter for the other obj
<FromGitter>
<noaheverett_twitter> Ok one more question: I have a Byte array that I'd like to send to a user's browser as binary however when I'm sending (via Kemal) it comes out as `Bytes[255, 216, 255, 224 ...`
<FromGitter>
<noaheverett_twitter> Sending with `env.response.print buf[0, bytes_read]`
<FromGitter>
<noaheverett_twitter> how do I convert that Byte array to binary representation that the user's browser can read as the file I'm sending?
<FromGitter>
<noaheverett_twitter> ah ok yeah I can probably pull what I need from that, thanks! I didn't know if there was a way to convert Byte array (Slice) to some version to be sent to browser
<FromGitter>
<noaheverett_twitter> thanks again
<FromGitter>
<Blacksmoke16> np
<FromGitter>
<noaheverett_twitter> ah ok figured it out to send the data and not convert with "to_s" is `env.response.write` instead of `env.response.print`
<FromGitter>
<Blacksmoke16> right, which is what i linked, must have missed it :p
<FromGitter>
<noaheverett_twitter> you're right I did miss it, sorry...I feel like a novice today! I appreciate everyone's help tremendously...coming from the ruby world and re-building parts of a web app in Crystal. Truly wonderful language!
<FromGitter>
<tenebrousedge> ^_^
<FromGitter>
<Blacksmoke16> athena is where its at π
_ht has joined #crystal-lang
<FromGitter>
<watzon> I know there's a "destructor" type thing for Crystal, but I can't remember what it is
<FromGitter>
<watzon> `finalize`?
<FromGitter>
<watzon> Does everything get garbage collected on SIGINT?
alexherbo2 has joined #crystal-lang
<FromGitter>
<watzon> I just need a good way of making sure something runs before the program exits
<FromGitter>
<eliasjpr> Im going to try to bind one of those libs to crystal
<FromGitter>
<tenebrousedge> I'd probably give some serious thought to just wrapping `exiftool`
<FromGitter>
<tenebrousedge> but I'm lazy like that
<FromGitter>
<eliasjpr> I think I can relate
<FromGitter>
<eliasjpr> Thanks for the link very helpful
_ht has quit [Quit: _ht]
sagax has quit [Ping timeout: 272 seconds]
dostoyevsky has quit [Ping timeout: 240 seconds]
<FromGitter>
<watzon> Is there still no way to export a C interface from Crystal?
<FromGitter>
<watzon> I've got someone wanting to make a metacall (https://metacall.io/) loader for Crystal, but I don't know enough about making shared libraries with Crystal, or if it's even possible. I wish Crystal's compiler was more up to speed with `rustc`
<FromGitter>
<eliasjpr> @watzon I think I saw a shard that tried to automatically do that
<FromGitter>
<watzon> Well there is a shard that allows you to generate C bindings, as in C->Crystal, but afaik there's nothing that allows you to export a C interface *from* Crystal.
<FromGitter>
<Blacksmoke16> got myself a jetbrains license for athena dev :p