Azure SDK Ruby Set Container ACL - ruby-on-rails

I'm using https://github.com/Azure/azure-sdk-for-ruby in my Rails application. I need to set the container policy but I don't know how to create a Signed Identifier instance for the set_container_acl method.
The comments say to pass in an array of "Azure::Entity::SignedIdentifier instances" but when I try to create an instance I get "uninitialized constant Azure::Storage::Entity". Scoured the net/documentation can't find anything about it.

After digging around in the azure gem files I was able to find a signed identifier file in the service directory. It's not loaded with azure for some reason so you have to require it.
require 'azure'
require 'azure/service/signed_identifier'
def some_method
# Some code here. Create blobs instance.
# blobs = Azure::Blob::BlobService.new
sas = Azure::Service::SignedIdentifier.new
sas.id = identifier
policy = sas.access_policy
policy.start = (Time.now - 5 * 60).utc.iso8601
policy.expiry = (Time.now + 1.years).utc.iso8601
policy.permission = "r"
identifiers = [sas]
options = { timeout: 60, signed_identifiers: identifiers }
container, signed = blobs.set_container_acl(container_name, "", options)
end

Related

image_path returns bad url if called from within renderer within redis job

Rails 6 app with ActiveStorage has a application helper that returns the profile image of a user.
module ApplicationHelper
def profile_picture_with_class user, css_class, shape, width = 100
if (shape == :square)
placeholder_pic = "blank_profile_square.jpg"
else
placeholder_pic = "blank_profile_round.png"
end
image_path = user.profile_image.present? ? user.profile_image : placeholder_pic
image_tag(image_path, width: width, class: css_class)
end
end
When this applicationHelper is called from within the app it works fine. If however a redis job uses this applicationHelper the returned image_tag is going to instead of my apps actual host (localhost:3000 in dev) and this creates a very broken link.
I'm guessing since this is firing off inside a redis job it doesnt know what the host is so its prepending something else. How can I get it to understand to use the correct host and active storage type (disk vs blob)
Thanks.
Comparison of the two created links:
Example:
Redis returns this:
http://example.org/rails/active_storage/blobs/<big_long_chunk>/new-chimney-crown.jpg
When run within a view it returns this:
http://localhost:3000/rails/active_storage/disk/<big_long_chunk>/new-chimney-crown.jpg?content_type=image%2Fjpeg&disposition=inline%3B+filename%3D%22new-chimney-crown.jpg%22%3B+filename%2A%3DUTF-8%27%27new-chimney-crown.jpg
Edit: I'm pretty sure it has something to do with this:
https://evilmartians.com/chronicles/new-feature-in-rails-5-render-views-outside-of-actions
This is the redis job, I believe that when image_path is called from within ChatMessagesController.render it is not understanding the current host.
def perform(chat_message)
ActionCable.server.broadcast "stream:#{chat_message.stream.id}", {
chat_message: ChatMessagesController.render(chat_message)
}
def perform(chat_message)
# ChatMessagesController.renderer.defaults[:http_host] = ENV['RAILS_APPLICATION_URL'].presence || 'http://localhost:3000'
renderer = ChatMessagesController.renderer.new(
http_host: ENV['RAILS_APPLICATION_URL'].presence || 'http://localhost:3000' ,
https: Rails.env.production?
)
ActionCable.server.broadcast "stream:#{chat_message.stream.id}", {
chat_message: renderer.render(chat_message)
}
end
So the defaults for whatever reason are reset to example.com while its running. If I create a renderer and specifically set its host (and ssl) it seems to work fine. SSL must be set correctly as well or it won't work.

delete files from Google Drive Service account with the google drive gem

We created a synchronizer plugin for discourse and it should synchronize backups to the google drive service account.
In order to keep the admin's google drive account clutterfree, we built a method at the end of the synchronizer, that is called remove_old_files
def remove_old_files
google_files = session.files
sorted = google_files.sort_by {|x| x.created_time}
keep = sorted.take(SiteSetting.discourse_sync_to_googledrive_quantity)
trash = google_files - keep
trash.each { |d| d.delete(true) }
end
we memoized the session object before in an instance variable. So with session.files we get an array of all the files in our google drive account. In the sorted variable we sort them from new to old. In the keep variable we take the first few (the admin hat to set the quantity) and in the trash variable we define the rest. Until here the method works in the console.
Now in the documentation for the Google Drive gem there is a delete method for the File object:
File 'lib/google_drive/file.rb', line 182
def delete(permanent = false)
if permanent
#session.drive.delete_file(id)
else
#session.drive.update_file(id, { trashed: true }, {})
end
nil
end
But when I try to delete via the rails console, it tells me Success - nil as if everything went fine. But ! the files are still there when I count them or call them. Even though I tried to delete them setting permanent = true :/
Why doesn't google delete the old backup files?

How to compile custom format ini file with redirects?

I'm working with an application that has 3 ini files in a somewhat irritating custom format. I'm trying to compile these into a 'standard' ini file.
I'm hoping for some inspiration in the form of pseudocode to help me code some sort of 'compiler'.
Here's an example of one of these ini files. The less than/greater than indicates a redirect to another section in the file. These redirects could be recursive.. i.e. one redirect then redirects to another. It could also mean a redirect to an external file (3 values are present in that case). Comments start with a # symbol
[PrimaryServer]
name = DEMO1
baseUrl = http://demo1.awesome.com
[SecondaryServer]
name = DEMO2
baseUrl = http://demo2.awesome.com
[LoginUrl]
# This is a standard redirect
baseLoginUrl = <PrimaryServer:baseUrl>
# This is a redirect appended with extra information
fullLoginUrl = <PrimaryServer:baseUrl>/login.php
# Here's a redirect that points to another redirect
enableSSL = <SSLConfiguration:enableSSL>
# This is a key that has mutliple comma-separated values, some of which are redirects.
serverNames = <PrimaryServer:name>,<SecondaryServer:name>,AdditionalRandomServerName
# This one is particularly nasty. It's a redirect to another file...
authenticationMechanism = <Authenication.ini:Mechanisms:PrimaryMechanism>
[SSLConfiguration]
enableSSL = <SSLCertificates:isCertificateInstalled>
[SSLCertificates]
isCertificateInstalled = true
Here's an example of what I'm trying to achieve. I've removed the comments for readability.
[PrimaryServer]
name = DEMO1
baseUrl = http://demo1.awesome.com
[SecondaryServer]
name = DEMO2
baseUrl = http://demo2.awesome.com
[LoginUrl]
baseLoginUrl = http://demo1.awesome.com
fullLoginUrl = http://demo1.awesome.com/login.php
enableSSL = true
serverNames = DEMO1,DEMO2,AdditionalRandomServerName
authenticationMechanism = valueFromExternalFile
[SSLConfiguration]
enableSSL = <SSLCertificates:isCertificateInstalled>
[SSLCertificates]
isCertificateInstalled = true
I'm looking at using ini4j (Java) to achieve this, but am by no means fixed on using that language.
My main questions are:
1) How can I handle the recursive redirects
2) How am I best to handle the redirects that have an additional string, e.g. serverNames
3) Bonus points for any suggestions about how to handle the external redirects. No big deal if that part isn't working just yet.
So far, I'm able to parse and tidy up the file, but I'm struggling with these redirects.
Once again, I'm only hoping for pseudocode. Perhaps I need more coffee, but I'm really puzzled by this one.
Thanks in advance for any suggestions.

Setting the Notebook to post Notes within in Evernote w/ RoR

How do you make sure a Note is created in a specific NoteBook that is not the default?
In the client sample code that Evernote provides they show this
client = EvernoteOAuth::Client.new(token: authToken)
# List all of the notebooks in the user's account
note_store = client.note_store
notebooks = note_store.listNotebooks(authToken)
puts "Found #{notebooks.size} notebooks:"
defaultNotebook = notebooks.first
notebooks.each do |notebook|
puts " * #{notebook.name}"
end
puts
puts "Creating a new note in the default notebook: #{defaultNotebook.name}"
puts
But the variable defaultNotebook does not get used again. The note get's posted at the end with
createdNote = note_store.createNote(authToken, note)
I guess that in the demo the defaultNotebook is assumed for any notes that get posted.
There isn't a setNotebook method, but there is a getNotebook one. Which the code demo for that is as follows.
client = EvernoteOAuth::Client.new(token: authtoken)
note_store = client.note_store
# We can ignore the creation here
notebook = Evernote::EDAM::Type::Notebook.new
notebook.name = "Notebook 1402371893"
created_notebook = note_store.createNotebook(notebook)
# Here we see it is retrieved with the guid
note_store.getNotebook(created_notebook.guid)
So does the getNotebook method act as setting the notebook on the note_store instance which I can then create Notes within? I have a feeling it's that simple. I'll give it a try.
EDIT
Here are more details on the way I'm trying to implement the usage of setting the Notebook that receives the Notes:
client = EvernoteOAuth::Client.new( token: user.evernote_token )
noteStore = client.note_store
if user.evernote_notebook.length.zero?
notebook = Evernote::EDAM::Type::Notebook.new()
notebook.name = "MyBook"
user.update_attribute( :evernote_notebook,
noteStore.createNotebook( user.evernote_token, notebook ).guid )
else
noteStore.getNotebook( user.evernote_notebook )
end
createdNote = noteStore.createNote( user.evernote_token, note )
image.update_attribute( :evernote_guid, createdNote.guid )
The purpose is to know the Notebook is set and the Note will be placed there.
NOTE THIS CODE WORKS --BUT-- The Note's in the wrong folder. It's in default and not the one I selected.
Here's the working answer
note = Evernote::EDAM::Type::Note.new()
client = EvernoteOAuth::Client.new( token: user.evernote_token )
noteStore = client.note_store
if user.evernote_notebook.length.zero?
notebook = Evernote::EDAM::Type::Notebook.new()
notebook.name = "MyBook"
user.update_attribute( :evernote_notebook,
noteStore.createNotebook( user.evernote_token, notebook ).guid )
end
note.notebookGuid = user.evernote_notebook
createdNote = noteStore.createNote( user.evernote_token, note )
image.update_attribute( :evernote_guid, createdNote.guid )
How do you make sure a Note is created in a specific NoteBook that is not the default?
If you want to create a note in a specific notebook, just set the 'notebookGuid' attribute of the note.
note.notebookGuid = created_notebook.guid
If you don't explicitly set the notebookGuid attribute, the note will be created in the default notebook of the account.
The getNotebook method only retrieves the notebook you specified.

ThinkingSphinx config match_mode and distributed index problems

Have been struggling with ThinkingSphinx 3.0.5 for a couple of days. basically,
we use distributed indexes, so for one model 4 indexes were defined.
index post_core
{
type = distributed
local = post_core_i0
local = post_core_i1
local = post_core_i2
local = post_core_i3
}
how to make TS search to point to this distributed index? :index => 'post_core' doesn't seem to work.
We need a custom parameter like dictionary = /home/user1/../my.txt in the final config file -- how to make rake ts:configure to keep it?
how to set match mode to any? :match_mode => :any doesn't work
Thanks!

Resources