Can you get legendary weapons in Vending Machines?
Very similar to some vending machines in Borderlands 1 (There was a "secret" vending machine in New Haven that would have very high legendary spawn rates), can vending machines in Borderlands 2 spawn Legendary weapons?
What affects their spawn rate? Level? Class?
Solution 1:
Yes, you can buy legendary (orange) weapons in normal vending machines. I can not find any written souce, but I've personally purchased a Shredifier from a gun machine at Raiders HQ in Sanctuary.
I'd assume any legendary that is marked as "drops from any suitable loot source" in the wiki can spawn in a vending machine. Level of the item is equal to the level of vending machine, which is equal to area level - if you go to Southern Shelf on Normal, you'll forever see level 4-6-something items there. As for "class", the type of item is rolled first, than it has the chance to become legendary - no specific rules.
Also, as Dave mentions, there are Torgue machines with legendaries for Torgue tokens, and Seraph vendor with seraph equipment for Seraph crystals.
Solution 2:
In Mr. Torgue's Campgain of Carnage, there are vending machines where you can buy weapons with Torgue Tokens. The item of the day in these machines are always legendary. They are quite expensive considering that Torgue Tokens are hard to acquire, however.