Variant product_id with API Ruby gem - api

I'm using the Shopify_api gem to grab products and variants for an app. When grabbing a variant, I can't seem to get the product_id, even though the API Doc says it should be available. Anyone know how I can get the product_id from the variant?

http://docs.shopify.com/api/product_variant does say product_id should be in the response, and if you inspect the actual response it is. The problem is that ActiveResource doesn't seem to save this as a normal attribute, if you look at https://github.com/Shopify/shopify_api/blob/master/lib/shopify_api/resources/variant.rb, it uses this as a prefix option. The only way I figure out how to do it right now is
variant.prefix_options[:product_id]
This is also reported in ActiveResource here https://github.com/rails/rails/issues/3917

Related

Rails ActsAsTaggable - Retrieve most_used tag of posts from user

With the ActsAsTaggableOn gem I would like to retrieve the most used tags used for posts by a certain user. Currently I have a user.rb model and a post.rb model which belongs_to the user. What I can do is this:
ActsAsTaggableOn::Tag.most_used
Which does show me the most used tags used overall. However, I would like to filter that down to only show me the most used tags by the current_user. I was thinking of something like:
ActsAsTaggableOn::Tag.most_used.joins(:posts).where(user_id: current_user.id)
which does not work since there is no connection established and therefore i cant join the models. How can I access the most used tags in the posts by the current_user?
ActsAsTaggableOn::Tag.joins(:taggable).where(taggable: current_user.posts).distinct.most_used(5)
After tinkering around with a lot of queries I got the solution now which might be a bit tricky.
Basically what ActsAsTaggableOn does is just assigning tags to the respective model. The tags however are in no way really related to anything else. What is possible is to assign tag ownership.
What I do now could be considered as bad practice: First the Post is assigned a tag which is stored in the tag_list, then I am going to trigger an after save action inside the posts.rb which grabs the last tag of the post object (only one tag can be assigned in my app) and assigns it to the user via
#user.tag(#post, with: "Some tag", on: :posts)
It would have been way easier to just code the whole thing myself and not rely on a gem, but its okay for now.

Passing a variable from the store to my API

Each category page on the Bigcommerce store has a unique id: %%GLOBAL_CatId%%
I need to pass this variable, which I cannot possibly know before hand, to my API script. I'm using an include for my script so I've tried several variations of the following but I can't get anything to work:
%%Include.http://example.com/api.php?catid=%%GLOBAL_CatId%%%%
Is this possible and how can I accomplish this?
I was informed by API support that this is NOT possible.

How to batch rename Tumblr tags?

I have tagged over 700 blog posts with tags containing hyphens, and these tags suddenly stopped working in 2011, because Tumblr decided (without any notice) to forbid hyphens in tags (I guess hyphens are blocked now, because spaces in tags (which are allowed) get changed to hyphens.). Unfortunately, Tumblr is not willing to globally rename all tags containg hyphens (although these tags are of no use anymore → 404).
Now I want to rename my tags myself.
I tried to do it with the "Mass Post Editor" (tumblr.com/mega-editor), but it's not possible to select posts by tag. I'd have to manually select post after post and look if a certain tag was used, and if so, delete it and add a new one instead. This would be a huge job (700 tagged posts, but more than 1000 in total).
So I thought that the Tumblr API might help me. I'm no programmer, but I'd be willing to dig into it, if I could get some help here as a starting point.
I think I need the following process:
select all posts that are tagged with x (= a tag containing hyphens)
tag all these posts with y (= a tag without hyphens)
delete the tag x on all these posts
I'd start this process for every affected tag manually.
I see that the method (or whatever you call it) /post knows the request parameter tag:
Limits the response to posts with the specified tag
(I guess I can only hope that this works for tags containing hyphens, too.)
After that I'd need a way to add and remove tags from that result set. /post/edit doesn't say anything about tags. Did I miss something? Isn't it possible to add/remove tags with the API?
Have you an idea how I could "easily" rename my tags?
Is it possible with the API? Could you give me a starting point, tip etc. how I could manage to do it?
I don't know if this might be helpful, but I noticed that the search function is still able to find posts "tagged" with tags that contain hyphens.
Example: let's say I have the tag foo-bar. It is linked with /tagged/foo-bar (→ 404). I can find the posts with /search/foo-bar (but this is of course not ideal because it might also find posts that contain (in the body text) words similar/equal to the tag name).
I tried to encode the hyphen (/tagged/foo%2Dbar), but no luck.
just for the record, because this is a popular google search: i've done it! you can use it at http://dev.goose.im/tags/.
i used a combo of PHP and jquery, basing my jquery off of a previous tumblr api script i wrote a year or two ago, and used this tumblr php oauth script for the authentication. if anyone wants me to put up the source code, i'd be happy to.
If you aren't a programmer, how much is your time is worth to you? As they say, time is money. Not only do you have to figure out how to use the API, but choose a language and learn to write in it. That's no small task. You could higher a freelancer for $50 for an hour worth of work.
To answer your question, yes it is possible to do this with the API. It mentions "These parameters are used for /post, /post/edit and /post/reblog methods." and tags is mentioned as a string of comma separated words.
What you want to do is get a listing of every single blog post using the /posts method. You'll want to look at the "Request" section to figure out the criteria to pass to this URL. You want it to be as general as possible to get a complete listing of all your posts.
After you get a listing of posts you'll want to iterate over it and modify the tags parameter provided in the response for each post. You'll want to use the id paramater along with /post/edit, which again takes tags as a string.
The simplest language you can use for this task is PHP. You'll want to look at the curl extension to make your requests. You'll want to read up on arrays as you'll be using them a lot. You'll also need to look at explode, implode, str_replace (for the dashes), and foreach for iterating over the result.
When you do this I would highly recommend you use break at the end of your foreach loop so it only affects one post at first. Testing it first will be important, as you don't want to accidentally erase your tags/posts. print and var_dump are good ways to help you debug the code. xdebug is a nice extension that allows you to step through the code line by line as it runs. Netbeans is an IDE that has good xdebug support.
There's also a nice page here to get you started with PHP. You'll need to install PHP on your machine. You don't need to install a web server - for this PHP-CLI (command line) sapi is good enough.

How do I update a variant price using shopify gem in rails 3.2.2?

Rails 3.2.2 app, using the most recent shopify gem on github.
I've looked at other threads on the shopify google group that suggested switching
ShopifyAPI::Base to use xml, but that had no effect.
Simple script, updates a product's variant prices. This used to work,
not sure when it stopped working, but script is identical to the old
working version.
Essentially, if I set a variant price and compare_at_price, and then
save, shopify does not reflect a change. The method returns true,
however. Originally, the script simply called product.save at the end,
but I've also tried saving the variants individually and no luck.
Thanks for any help.
I wasn't able to reproduce the problem.
If you provide us with a request id then we could lookup the request in our logs. You can obtain the request id using the following code just after save that isn't working.
ShopifyAPI::Base.connection.response['x-request-id']
What I can tell you is that:
You shouldn't need to use xml format (JSON tends to be faster)
You should be able to save changes on a variant or product
This was a bug in Shopify, which has now been fixed.
It affected product updates through the API for shops that had inventory management enabled.
Please retest and confirm that it has been fixed.

Rails 3 - exist any gem for counting page views?

I wanna to add to my blog a counter for displaying, how many times was an article displayed. I would like to solve it through ruby (not GA or something like that).
Is available any gem that do this task (also with checking IP address and the time of latest reading of article) or I have to implement by myself?
There's one used by thousands of people at http://www.seologic.com/webmaster-tools/counters. Source code is available at https://github.com/ivanoblomov/hit_counter.