How to use Koala Facebook Graph API? - ruby
I am a Rails newbie. I want to use Koala's Graph API.
In my controller
#graph = Koala::Facebook::API.new('myFacebookAccessToken')
#hello = #graph.get_object("my.Name")
When I do this, I get something like this
{
"id"=>"123456",
"name"=>"First Middle Last",
"first_name"=>"First",
"middle_name"=>"Middle",
"last_name"=>"Last",
"link"=>"http://www.facebook.com/MyName",
"username"=>"my.name",
"birthday"=>"12/12/1212",
"hometown"=>{"id"=>"115200305133358163", "name"=>"City, State"}, "location"=>{"id"=>"1054648928202133335", "name"=>"City, State"},
"bio"=>"This is my awesome Bio.",
"quotes"=>"I am the master of my fate; I am the captain of my soul. - William Ernest Henley\r\n\r\n"Don't go around saying the world owes you a living. The world owes you nothing. It was here first.\" - Mark Twain",
"work"=>[{"employer"=>{"id"=>"100751133333", "name"=>"Company1"}, "position"=>{"id"=>"105763693332790962", "name"=>"Position1"}, "start_date"=>"2010-08", "end_date"=>"2011-07"}],
"sports"=>[{"id"=>"104019549633137", "name"=>"Sport1"}, {"id"=>"103992339636529", "name"=>"Sport2"}],
"favorite_teams"=>[{"id"=>"105467226133353743", "name"=>"Fav1"}, {"id"=>"19031343444432369133", "name"=>"Fav2"}, {"id"=>"98027790139333", "name"=>"Fav3"}, {"id"=>"104055132963393331", "name"=>"Fav4"}, {"id"=>"191744431437533310", "name"=>"Fav5"}],
"favorite_athletes"=>[{"id"=>"10836600585799922", "name"=>"Fava1"}, {"id"=>"18995689436787722", "name"=>"Fava2"}, {"id"=>"11156342219404022", "name"=>"Fava4"}, {"id"=>"11169998212279347", "name"=>"Fava5"}, {"id"=>"122326564475039", "name"=>"Fava6"}],
"inspirational_people"=>[{"id"=>"16383141733798", "name"=>"Fava7"}, {"id"=>"113529011990793335", "name"=>"fava8"}, {"id"=>"112032333138809855566", "name"=>"Fava9"}, {"id"=>"10810367588423324", "name"=>"Fava10"}],
"education"=>[{"school"=>{"id"=>"13478880321332322233663", "name"=>"School1"}, "type"=>"High School", "with"=>[{"id"=>"1401052755", "name"=>"Friend1"}]}, {"school"=>{"id"=>"11482777188037224", "name"=>"School2"}, "year"=>{"id"=>"138383069535219", "name"=>"2005"}, "type"=>"High School"}, {"school"=>{"id"=>"10604484633093514", "name"=>"School3"}, "year"=>{"id"=>"142963519060927", "name"=>"2010"}, "concentration"=>[{"id"=>"10407695629335773", "name"=>"c1"}], "type"=>"College"}, {"school"=>{"id"=>"22030497466330708", "name"=>"School4"}, "degree"=>{"id"=>"19233130157477979", "name"=>"c3"}, "year"=>{"id"=>"201638419856163", "name"=>"2011"}, "type"=>"Graduate School"}],
"gender"=>"male",
"interested_in"=>["female"],
"relationship_status"=>"Single",
"religion"=>"Religion1",
"political"=>"Political1",
"email"=>"somename#somecompany.com",
"timezone"=>-8,
"locale"=>"en_US",
"languages"=>[{"id"=>"10605952233759137", "name"=>"English"}, {"id"=>"10337617475934611", "name"=>"L2"}, {"id"=>"11296944428713061", "name"=>"L3"}],
"verified"=>true,
"updated_time"=>"2012-02-24T04:18:05+0000"
}
How do I show this entire hash in the view in a good format?
This is what I did from what ever I learnt..
In my view
<% #hello.each do |key, value| %>
<li><%=h "#{key.to_s} : #{value.to_s}" %></li>
<% end %>
This will get the entire thing converted to a list... It works awesome if its just one key.. but how to work with multiple keys and show only the information... something like
when it outputs hometown : City, State rather than something like
hometown : {"id"=>"115200305133358163", "name"=>"City, State"}
Also for education if I just say education[school][name] to display list of schools attended?
The error i get is can't convert String into Integer
I also tried to do this in my controller, but I get the same error..
#fav_teams = #hello["favorite_teams"]["name"]
Also, how can I save all these to the database.. something like just the list of all schools.. not their id no's?
Update:
The way I plan to save to my database is.. lets say for a user model, i want to save to database as :facebook_id, :facebook_name, :facebook_firstname, ...., :facebook_hometown .. here I only want to save name... when it comes to education.. I want to save.. school, concentration and type.. I have no idea on how to achieve this..
Looking forward for help! thanks!
To show the hash in a pretty-printed way, use the gem 'awesome_print'.
Add this to your Gemfile:
gem 'awesome_print'
And then run:
bundle install
And then, in your view, you can add:
<%= ap #hello %>
The question of how to store in the database requires a little more information on what you plan to do with it, but at minimum you could create a model, add a 'facebook_data' (type would be 'text') on that model, and then serialize it (add this line near the top of your model file: serialize :facebook_data). Then you could assign the hash (#hello in this case) to the model's 'facebook_data' property, and then save the model. But you won't be able to query your database for individual attributes of this facebook data very easily this way.
you can just do #hello["name"] then it will give you the value of the name
Your #hello object should be of the class Koala::Facebook::API::GraphCollection or something similar. You should be able to loop through this object, like your question demonstrates. As for what code to put inside your loop that will help you save records to the database, assuming your rails user model class name is User, try something like this:
#hello.each do |h|
u = User.where(:facebook_id => h["id"]).first_or_initialize
u.update_attributes(
:name => h["name"],
:first_name => h["first_name"],
:hometown_city => h["hometown"]["name"].split(",").first,
:hometown_state => h["hometown"]["name"].split(",").last.strip
# ETC, ETC
)
end
In the case of the hometown and education fields, you're just going to have to traverse the ruby hash the proper way. See the docs for more info.
Related
rails string substitution or similar solution in controller
I'm building a site with users in all 50 states. We need to display information for each user that is specific to their situation, e.g., the number of events they completed in that state. Each state's view (a partial) displays state-specific information and, therefore, relies upon state-specific calculations in a state-specific model. We'd like to do something similar to this: ##{user.state} = #{user.state.capitalize}.new(current_user) in the users_controller instead of #illinois = Illinois.new(current_user) if (#user.state == 'illinois') .... [and the remaining 49 states] #wisconsin = Wisconsin.new(current_user) if (#user.state == 'wisconsin') to trigger the Illinois.rb model and, in turn, drive the view defined in the users_controller by def user_state_view #user = current_user #events = Event.all #illinois = Illinois.new(current_user) if (#user.state == 'illinois') end I'm struggling to find a better way to do this / refactor it. Thanks!
I would avoid dynamically defining instance variables if you can help it. It can be done with instance_variable_set but it's unnecessary. There's no reason you need to define the variable as #illinois instead of just #user_state or something like that. Here is one way to do it. First make a static list of states: def states %{wisconsin arkansas new_york etc} end then make a dictionary which maps those states to their classes: def state_classes states.reduce({}) do |memo, state| memo[state] = state.camelize.constantize memo end end # = { 'illinois' => Illinois, 'wisconsin' => Wisconsin, 'new_york' => NewYork, etc } It's important that you hard-code a list of state identifiers somewhere, because it's not a good practice to pass arbitrary values to contantize. Then instantiating the correct class is a breeze: #user_state = state_classes[#user.state].new(current_user) there are definitely other ways to do this (for example, it could be added on the model layer instead)
How to use polymorphism to remove a switch statement which compares strings?
I am new to Ruby, so let me describe the context of my problem first: I have a json as input which has the following key / value pair: { "service": "update" } The value has many different values for example: insert,delete etc. Next there is a method x which handles the different requests: def x(input) case input[:service] services = GenericService.new when "update" result = services.service(UpdateService.new,input) when "insert" result = services.service(InsertService.new,input) when "delete" result = services.service(DeleteService.new,input) .... .... else raise "Unknown service" end puts JSON.pretty_generate(result) end What is bothering me is that I still need to use a switch statement to check the String values (reminds me of 'instance of' ugh..). Is there a cleaner way (not need to use a switch)? Finally I tried to search for an answer to my question and did not succeed, if however I missed it feel free to comment the related question. Update: I was thinking to maybe cast the string to the related class name as follows: How do I create a class instance from a string name in ruby? and then call result = services.services(x.constantize.new,input) , then the class names ofcourse needs to match the input of the json.
You can try something like: def x(input) service_class_name = "#{input[:service].capitalize}Service" service_class = Kernel.const_get(service_class_name) service_class.new(input).process end In addition you might want to check if this is a valid Service class name at all. I don't understand why you want to pass the service to GenericService this seems strange. let the service do it's job.
If you're trying to instatiate a class by it's name you're actually speaking about Reflection rather than Polymorphism. In Ruby you can achieve this in this way: byName = Object.const_get('YourClassName') or if you are in a Rails app byName= 'YourClassName'.constantize Hope this helps
Just first thoughts, but you can do: eval(services.service("#{input[:service].capitalize}Service.new, #{input})") if valid_service? input[:service] def valid_service? w%(delete update insert).include? input[:service] end As folks will no doubt shout, eval needs to be used with alot of care
How to return Hash FQL Query from Ruby
I am new here and really new to Ruby so I will do my best to ask a good question. Basically I am trying to write an app that returns Facebook events a user has been invited to. Pretty simple. The issue is it keeps returning everything like this [{"name"=>"SPB Presents An Evening With Demetri Martin"}], [{"name"=>"Say \"Pi\" to Passover!"}] I just want the value of the key. I've tried to use ["name"] and look at basic Ruby tutorial but I got a myriad of errors each time. Here is my code: HomeController: def index if session["fb_access_token"].present? #fql = Koala::Facebook::API.new(session["fb_access_token"]) #invites = #fql.fql_query("SELECT eid FROM event_member WHERE uid = me()") end def names(eid) if session["fb_access_token"].present? #fql = Koala::Facebook::API.new(session["fb_access_token"]) #fql.fql_query("SELECT name FROM event WHERE eid = #{eid}") end end Home View <% if #invites %> <% for invite in #invites %> <p><%=h names(h invite["eid"])%></p> <% end > <% end > Thank you so much in advance for your help! Also if anyone has a better way to build this so I don't have to do so much work in the views or how to do a better for each loop in the controller, that'd be nice too!
I figured out my problem if anyone is curious. The fql query stores everything in an array with one hash of "location" in it, so each time I displayed the query it displayed the array. This was my fix: def names(eid) if session["fb_access_token"].present? #fql = Koala::Facebook::API.new(session["fb_access_token"]) #hashmap = #fql.fql_query("SELECT name FROM event WHERE eid = #{eid}") #name = #hashmap[0]["name"] Now I pull the first object in the array at index 0 and the use the key "name." Worked like a charm!
Sinatra can't convert Symbol into Integer when making MongoDB query
This is a sort of followup to my other MongoDB question about the torrent indexer. I'm making an open source torrent indexer (like a mini TPB, in essence), and offer both SQLite and MongoDB for backend, currently. However, I'm having trouble with the MongoDB part of it. In Sinatra, I get when trying to upload a torrent, or search for one. In uploading, one needs to tag the torrent — and it fails here. The code for adding tags is as follows: def add_tag(tag) if $sqlite unless tag_exists? tag $db.execute("insert into #{$tag_table} values ( ? )", tag) end id = $db.execute("select oid from #{$tag_table} where tag = ?", tag) return id[0] elsif $mongo unless tag_exists? tag $tag.insert({:tag => tag}) end return $tag.find({:tag => tag})[:_id] #this is the line it presumably crashes on end end It reaches line 105 (noted above), and then fails. What's going on? Also, as an FYI this might turn into a few other questions as solutions come in. Thanks! EDIT So instead of returning the tag result with [:_id], I changed the block inside the elsif to: id = $tag.find({:tag => tag}) puts id.inspect return id and still get an error. You can see a demo at http://torrent.hypeno.de and the source at http://github.com/tekknolagi/indexer/
Given that you are doing an insert(), the easiest way to get the id is: id = $tag.insert({:tag => tag}) id will be a BSON::ObjectId, so you can use appropriate methods depending on the return value you want: return id # BSON::ObjectId('5017cace1d5710170b000001') return id.to_s # "5017cace1d5710170b000001" In your original question you are trying to use the Collection.find() method. This returns a Mongo::Cursor, but you are trying to reference the cursor as a document. You need to iterate over the cursor using each or next, eg: cursor = $tag.find_one({:tag => tag}) return cursor.next['_id']; If you want a single document, you should be using Collection.find_one(). For example, you can find and return the _id using: return $tag.find_one({:tag => tag})['_id']
I think the problem here is [:_id]. I dont know much about Mongo but `$tag.find({:tag => tag}) is probably retutning an array and passing a symbol to the [] array operator is not defined.
Can I avoid transposing an array in Ruby on Rails?
I have a Rails app that has a COUNTRIES list with full country names and abbreviations created inside the Company model. The array for the COUNTRIES list is used for a select tag on the input form to store abbreviations in the DB. See below. VALID_COUNTRIES is used for validations of abbreviations in the DB. FULL_COUNTRIES is used to display the full country name from the abbreviation. class Company < ActiveRecord::Base COUNTRIES = [["Afghanistan","AF"],["Aland Islands","AX"],["Albania","AL"],...] COUNTRIES_TRANSFORM = COUNTRIES.transpose VALID_COUNTRIES = COUNTRIES_TRANSPOSE[1] FULL_COUNTRIES = COUNTRIES_TRANSPOSE[0] validates :country, inclusion: { in: VALID_COUNTRIES, message: "enter a valid country" } ... end On the form: <%= select_tag(:country, options_for_select(Company::COUNTRIES, 'US')) %> And to convert back the the full country name: full_country = FULL_COUNTRIES[VALID_COUNTRIES.index(:country)] This seems like an excellent application for a hash, except the key/value order is wrong. For the select I need: COUNTRIES = {"Afghanistan" => "AF", "Aland Islands" => "AX", "Albania" => "AL",...} While to take the abbreviation from the DB and display the full country name I need: COUNTRIES = {"AF" => "Afghanistan", "AX" => "Aland Islands", "AL" => "Albania",...} Which is a shame, because COUNTRIES.keys or COUNTRIES.values would give me the validation list (depending on which hash layout is used). I'm relatively new to Ruby/Rails and am looking for the more Ruby-like way to solve the problem. Here are the questions: Does the transpose occur only once, and if so, when is it executed? Is there a way to specify the FULL_ and VALID_ lists that do not require the transpose? Is there a better or reasonable alternate way to do this? For instance, VALID_COUNTRIES is COUNTRIES[x][1] and FULL_COUNTRIES is COUNTRIES[x][0], but VALID_ must work with the validation. Is there a way to make a hash work with just one hash rather then one for the select_tag and one for converting the abbreviations in the DB back to full names for display?
1) Does the transpose occur only once, and if so, when is it executed? Yes at compile time because you are assigning to constants if you want it to be evaluated every time use a lambda FULL_COUNTRIES = lambda { COUNTRIES_TRANSPOSE[0] } 2) Is there a way to specify the FULL_ and VALID_ lists that do not require the transpose? Yes use a map or collect (they are the same thing) VALID_COUNTRIES = COUNTRIES.map &:first FULL_COUNTRIES = COUNTRIES.map &:last 3) Is there a better or reasonable alternate way to do this? For instance, VALID_COUNTRIES is COUNTRIES[x][1] and FULL_COUNTRIES is COUNTRIES[x][0], but VALID_ must work with the validation. See Above 4) Is there a way to make the hash work? Yes I am not sure why a hash isn't working as the rails docs say options_for_select will use hash.to_a.map &:first for the options text and hash.to_a.map &:last for the options value so the first hash you give should be working if you can clarify why it is not I can help you more.