ruby - Storing data from Twitter API in Rails -
Before I start, I'm still learning the entire MVC framework then understand in its explanation.
I am calling Twitter API using Twitter Jam. Im collecting all their followers and genderize.io using their ruby gem extension to return their name and gender. I am displaying the name and percentage of "male, female, unknown" gender on my index footing. I
The code of my last code breaks into the JSON object. {\ "Count \": 59, \ "percent \": 0.46}, \ "female \": {\ "counting \ unknown gender \ : "0.24}, \" "31, \" percent \ ": {\" count \ ": 38, \" percent \ ": 0.3}}"
My question is that How do I do this long index method refactor and break this so that the model data that I am getting back from APIs can do and break the penis? In other words, I need to store the screen name (screen_name), the list of followers (@username_name), and gender percentages (@gend_preview).
If anyone knows how to start it down, and please tell me to store the data
My code is displayed here .
Class Twitter Controller & lt; ApplicationController requires 'Twitter' 'json' def index client = twitter: REST :: Client.new do | Config | Config.consumer_key = '***********' config.consumer_secret = '***********' config.access_token = '********* *** 'config.access_token_secret =' *********** 'end SCREEN_NAME =' myscreenname 'follower_ids = client.follower_ids (SCREEN_NAME) user_name = [] follower_ids.each_slice (100). Piece | Start customer (piece) .each. User | User_name & lt; & Lt; User.name End End =user_name = user_name c = user_name.map {| Str | Str.split.first} Jean = [] GIR = Lingmap IORB.New Ceich do. Reserve | Res res = gir.info_for_name (res) General & lt; & Lt; "# {Res: [Result]. Gender)" Rescue Res = "GenderizeIoRb :: Errors :: Nominal Fond" General " & Lt; "UK gender" and end hash = hash.new 0 gene.each | MFG | Hash [mfg] + = 1 end count = hash To_a sum = 0 count.each {| _, V | Sum + = v} Ling_preissage = [] count.each do | K, v | Gender_profit & lt; & Lt; [K, (v.to_f / sum) * 100). Round] End = gender_percentage = gender_change total = count.map {| Elt | Elt [1]} .reduce {| X, y | X + y} hsh = {} count.each do | Label, n. HSH [label] = {} HSH [label] ['count'] = nhsh [label] ['percent'] = (n.to_f / total.to_f) .round (2) end hsh.to_json end end
Comments
Post a Comment