Using gem Curb (curl) to download file - ruby

I have understood that I could use the RubyGem Curb to fulfill my needs of curl'ing down a file to my computer. But I cannot figure out how to actually "touch" the file. This is what I have got so far
include 'rubygems'
include 'curb'
curl = Curl::Easy.new('http://wordpress.org/latest.zip')
curl.perform
I think I understand that perform actually downloads the file. But how can I interact with the file afterwards? Where is it downloaded to?
Regards,
Mattias

The file can be accessed with the body_str method.
puts curl.body_str

Check the docs at http://rdoc.info/gems/curb/0.7.15/.
After perform, the content is in e.g. curl.body_str.

Related

Avoid Ruby open-uri cache

I am trying to run a script from my Github Gist page but OpenURI or something else seems to cache it on they way and it never actually updates. I am using the code below on Ubuntu 16.04 and Ruby 2.3.1. I just want it to fetch the script from the url every time instead of using a cached version.
#!/usr/bin/ruby -w
require "open-uri"
url = "https://gist.githubusercontent.com/*/*/raw/*/*.rb"
code_from_url = open(url) {|f| f.read }
eval(code_from_url)
Nevermind, there was no cache. It was just an incorrect link to a revision specific gist. The correct way to get the latest version of the first file in a gist is https://gist.githubusercontent.com/{username}/{gist}/raw

RubyGems - require, file location and (load error) complications

very new to coding so, having exhausted Google and Stack Overflow, would really appreciate some advice...
I am currently building a web-scraper to get familiar with CMD vs Sublime Text, feeling Ruby in action; So i am working my way through this tutorial
After having actioned in CMD
C:\gem install HTTParty
SUBLIME TEXT - starts with this code:
require_relative 'HTTParty'
require_relative 'Nokogiri'
etc
But before i can get to anything more from CMD, i hit web_scraper.rb and it returns with:
C:/Users/ATH18/Desktop/nokogiri_tutorial/web_scraper.rb:1:in `require_relative': cannot load such file -- C:/Users/ATH18/Desktop/nokogiri_tutorial/httparty (LoadError)
from C:/Users/ATH18/Desktop/nokogiri_tutorial/web_scraper.rb:1:in `<main>'
[Finished in 0.1s with exit code 1]
I think this has to be due to one of the following:
i) maybe gems have to have their actual files dragged into whatever folder you're creating a new program in?
ii) i'm missing another piece of information that would let it run properly?
iii) perhaps there's another way to tell CMD/ruby that the "require"d gem is not in the current folder (I read this somewhere but their advice didnt seem to work either).
NOTE - i have done gem install xxxxxx in both the C:\ directory and C:\users\desktop\projectFolder\
Help?
You have to use require instead of require_relative. The difference between both a is explained here: https://stackoverflow.com/a/3672600/92049
Use require 'GEMNAME' for gems installed with gem install GEMNAME; use require_relative 'PATH' to require a file relative to the file containing require_relative. (Most often you will find yourself using require.)
To come back to your question: As it says in the tutorial, you have to write require 'HTTParty' instead of require_relative 'HTTParty'.
Does this answer your original question?

How to find the directory of an app that is using your gem?

I'm writing a gem that will access a configuration yaml file in the ruby app that the gem is required in. I've already checked out Dir.pwd but that only gives the directory that I am running the script from. Am I thinking about this the wrong way? Here's sort of what I'm talking about.
The gem would have a constant which could be accessed by Gem::CONSTANT. The code that instantiates this constant would be
module Gem
config_file = #code to retrieve the yaml file.
CONSTANT = config_file[:constant]
end
Then other methods would perform operations on these constants. Any help would be appreciated let me know if I need to be more specific.
Look at File.dirname($0).
$0 is the absolute path/name of the currently running script.

uninitialized constant Twitter (NameError)

Hey guys i have a problem i am facing with the twitter gem. I have a file (twitter.rb) with this content
require "rubygems"
require "twitter"
puts Twitter.user_timeline("roykasa").first.text
puts Twitter.user("roykasa").location
search = Twitter::Search.new
search.containing("hate").to("StewieJokess").
result_type("recent").each do |r| puts r.text end
When i run the file i get this error :
uninitialized constant Twitter (NameError)
I read somewhere on SO where a user had a similar problem and he solved it by installing a new version of ruby and rubygems but the problem i am having is am running suse 12.1 and am running the latest versions of both ruby and ruby gems. No rpms can be found from 3rd parties anywhere. Atleast i have searched. Does anyone know another way round this?
If you are running Ruby 1.8.x you should be able to solve your problem by renaming your own script to anything different than twitter.rb.
This is because the main file in the twitter gem is named exactly like this and your file probably overrides it in combined virtual file system that the $LOAD_PATH order creates. Before Ruby 1.9.x, require did not only load from library directories, but preferred to load files relative to the current working directory of your process, in this case, the directory where your script lies in.
Do not name your file as twitter.rb, also make sure there is no other file in the same directory with the name twitter.rb

How can I require libraries in your Rails directory from the console?

I have a script called query.rb in my lib directory. I want to require it so I can run it from the Rails console. I wonder if this is possible. The script work fine from within the application, so I know it's well formed and functional.
For Rails 3+, use load "#{Rails.root}/lib/your_lib_file.rb"
load works like require, but allows you to re-load files as you edit them (unlike require, which you cant run again). See https://stackoverflow.com/a/6361502/513739
require "#{RAILS_ROOT}/lib/query"
For Rails 4+:
require "#{Rails.root}/lib/query"
>> $:.unshift 'lib'
>> require 'query'

Resources