Using Garb with GA Management API v3 - ruby-on-rails

I installed Sija's fork of garb and am having some issues. The documentation appears to be a bit outdated as some things have been deprecated.
I have the following code (ignore the fact that it's horribly unsecure):
extend Garb::Model
metrics :pageviews
dimensions :page_path
Garb::Session.login('XXXXXX#gmail.com', 'mypassword')
profile = Garb::Management::Profile.all.detect { |p| p.web_property_id == 'UA-XXXXX-1' }
puts profile.visits
When I run this, I get undefined method visits. I also tried this code on StackOverview, and it returned undefined method results. I'm guessing these are due to the new GA Management API v3 changes, but does anyone know the new way to access pageviews/visits?
I'm trying to query pageviews by date in the end.
Thanks for any help!

You need to create a class extending Garb::Model (https://github.com/Sija/garb#define-a-report-class). Btw, documentation has been updated to work with the newest version of the gem.

Here is an example:
class Report
extend Garb::Model
metrics :pageviews
dimensions :pagePath
end
Edit: Thanks for the edit! That was my first ever post :)

Related

Shopify API How to update Inventory Level via the Shopify API Gem

Anyone know how to update a variants inventory level via the Shopify API Gem?
I've tried everything I can think of, including checking out the Gem specs and no luck.
I have the correct location_id as well as inventory_item_id AND the read_inventory, write_inventory scopes. As well as a valid session
I've tried the following with no luck
payload = {"location_id":xxxxxx, "inventory_item_id":xxxxxx, "available":144}
Then:
level = ShopifyAPI::InventoryLevel.find(:all, params: payload)
level.set
level.connect
level.adjust
level.save
ShopifyAPI::InventoryLevel.set(payload)
ShopifyAPI::InventoryLevel.new(payload)
ShopifyAPI::InventoryLevel.create(payload)
ShopifyAPI::InventoryLevel.adjust(payload)
ShopifyAPI::InventoryLevel.update(payload)
Where did I go wrong?
Whelp, I figured it out. Would have been nice if it was documented somewhere.. A boy can dream
But you first have to create an instance of InventoryLevel like so:
payload = {"location_id":xxx, "inventory_item_id":xxx}
level = ShopifyAPI::InventoryLevel.new(payload)
And then set the inventory on that instance like this:
level.set(69)

Display places 2 weeks or older with Ruby on Rails + PostgresQL

This topic has been covered before, but I am new and trying to understand. My app is designed to store the locations of an item and since time matters, I wanted to create a link for a view that would ONLY show places that are two weeks or older. I understand I should create a method in the model similar to what's below, but I am just confused on this code. I have been trying to find material to read to understand better. The homepage shows all the places, but I want a link to also show 2 weeks or older.
Along with help, I would love any good reads to help me understand better. Thanks so much SO!
def self.recent_places
Place.select("p.*, COUNT(v.id) AS count").where("post.created_at >= 2.week.ago.utc")
end
This is a link to the GitHub.
https://github.com/Mbartlett413/DumpStack
I would recommend using scope over class method though two are mostly the same:
scope :older_than, ->(time) { where("created_at < ?", time) }
Using:
Post.older_than(2.weeks.ago)
Post.older_than(1.month.ago)
Here you go: http://guides.rubyonrails.org/active_record_querying.html#passing-in-arguments
I actually just solved this for my own app, this is the method that I used inside my model:
scope :recent_places, -> {
where(Place.arel_table[:created_at].lt(2.weeks.ago))
}

Rails TMDB API Browse to Find

TMDB.org recently made a change to their API which removes the capability to browse their database.
My Rails app used to use the tmdb-ruby gem to browse the TMDB database, but this gem only worked with v2.0 of the API, which is now defunct.
TMDB.org recommends using this gem, and since it is forked from the gem I previously used, it makes it a bit easier.
My PostgreSQL database is already populated with data imported from TMDB when v2.0 was still extant and when I could use the browse feature.
How can I now use the find feature (ie: #movie = TmdbMovie.find(:title => "Iron Man", :limit => 1) ) to find a random movie, without supplying the title of the Movie.
This is my rake file which worked with the older gem.
I would like to know how to have it work the same way but whilst using the find instead of the browse.
Thanks
I don't think find is what you need in order to get what you want (getting the oldest movies in the database and working its way up to the newest movie). Looking at the TMDb API documentation, it looks like they now have discover that may have replaced the browse that you used to use.
I don't see discover anywhere in Irio's ruby-tmdb fork, but it looks like most of the specific methods they have (like TmdbMovie.find) call a generic method Tmdb.api_call.
You should be able to use the generic method to do something like:
api_return = Tmdb.api_call(
"discover/movie",
{
page: 1,
sort_by: 'release_date.asc',
query: '' # Necessary because Tmdb.api_call throws a nil error if you don't specify a query param value
},
"en"
)
results = api_return["results"]
results.flatten!(1)
results.uniq!
results.delete_if &:nil?
results.map!{|m| TmdbMovie.new(m, true)} # `true` tells TmdbMovie.new to expand results
If this works, you could even fork Irio's fork, implement a TmdbMovie.discover method supporting all the options and handling edge cases like TmdbMovie.find does, and send them a pull request since it just looks like they haven't gotten around to implementing this yet and I'm sure other people would like to have this method as well :)

Built-in way to convert xml to an ActiveRecord Object in Rails 3?

In Rails 3, is there a way to produce an ActiveRecord object from xml in a controller without writing code yourself to explicitly parse it? Say for example, could a controller receive xml like
<user>
<first_name>Bob</first_name>
<last_name>Smith</last_name>
</user>
and have it produce a proper User object similar to User.new(params[:user])? This is for an api.
Yes, you can do it like this:
#user = User.new
#user.from_xml(xml_data)
Update
On overriding you can do something like this:
#user.rb
def from_xml(xml_data)
book = Book.new
book.from_xml(extract_xml_from(xml_data))
self.books << book
super(xml_data)
save
book.save
end
Please note that the most important line in the overriding is the super(xml_data) which will take care on calling the original from_xml(xml_data) of the ActiveRecord model.
So you can customize the rest as needed, but this line is neede if you want to get the original functionality as well.
Let me know if something is not clear.
I've created a gem, xml_active that might help you with this without having to write a lot of code. You can check it out at https://rubygems.org/gems/xml_active.
To get it to create one object with associations just do the following:
book = Book.one_from_xml xml_data
You can also get xml_active to create many objects from xml along with associations. There are more features but probably not in the scope of this answer. You can check them out on the home page for the gem.
UPDATE
xml_active has now been officially retired and development is now focused on data_active (see https://github.com/michael-harrison/data_active) which has the functionality of xml_active but in future releases I will be working to support other formats

ruby handsoap wiredump

How can see the wiredump of a soap using the 'handsoap' library?
I use the on_before_dispatch hook, and right now I am looking at the SoapService variables to see where such a request might be stored.
Hmm.. I should also check out invoke to see which var. is using..
Do you have a quick solution? :D
Thanks
You can dump http-activity by setting the class variable $logger, as in:
Example::FooService.logger = $stdout
This will dump out the http-request and response, nicely formatted. Note that this is not 100% what goes over the wire, since the underlying http-client implementation may add some headers etc. For most uses, ths doesn't matter, but if you're tracing down a bug, you might want to employ wireshark.
I used the hook
def on_before_dispatch
puts ##document
end
and document holds the doc variable.
##document = nil
def on_create_document(doc)
##document = doc
...
On a more abstract (none ruby specific) note, try wireshark. Been using it for wiredumps for all kinds of apps for years.

Resources