Twitter4J 400 "{"errors":[{"message":"Bad Authentication data","code":215}]} - twitter
I'm starting to use twitter4J 3.0.4-SNAPSHOT but got the error with the simple code:
Twitter twitter = TwitterFactory.getSingleton();
try {
Status status = twitter.updateStatus("test");
} catch (TwitterException e) {
e.printStackTrace();
}
OAuth Setting(consumerKey,consumerSecret,accessToken and accessTokenSecret) is written in twitter4j.properties in the classpath and these values are surly read because when I set them explicitly like twitter.setOAuthConsumer(...) I was told that they were already set. And Setting them exlicitly within the code doesn't change the situation.
What can I do? The values are just mistaken or other reason?
I tried several time in no results...
Here is a header info.
[Wed Jan 23 08:41:51 JST 2013]debug: true
[Wed Jan 23 08:41:51 JST 2013]userAgent: twitter4j http://twitter4j.org/ /3.0.4-SNAPSHOT(build: 741de654255dfd4f0b154533bbb4fa76a88f6ab3)
[Wed Jan 23 08:41:51 JST 2013]user: null
[Wed Jan 23 08:41:51 JST 2013]password: null
[Wed Jan 23 08:41:51 JST 2013]useSSL: false
[Wed Jan 23 08:41:51 JST 2013]prettyDebug: false
[Wed Jan 23 08:41:51 JST 2013]gzipEnabled: true
[Wed Jan 23 08:41:51 JST 2013]httpProxyHost: null
[Wed Jan 23 08:41:51 JST 2013]httpProxyUser: null
[Wed Jan 23 08:41:51 JST 2013]httpProxyPassword: null
[Wed Jan 23 08:41:51 JST 2013]httpProxyPort: -1
[Wed Jan 23 08:41:51 JST 2013]httpConnectionTimeout: 20000
[Wed Jan 23 08:41:51 JST 2013]httpReadTimeout: 120000
[Wed Jan 23 08:41:51 JST 2013]httpStreamingReadTimeout: 40000
[Wed Jan 23 08:41:51 JST 2013]httpRetryCount: 0
[Wed Jan 23 08:41:51 JST 2013]httpRetryIntervalSeconds: 5
[Wed Jan 23 08:41:51 JST 2013]maxTotalConnections: 20
[Wed Jan 23 08:41:51 JST 2013]defaultMaxPerRoute: 2
[Wed Jan 23 08:41:51 JST 2013]oAuthConsumerKey: u3T6gYq17fBjQ1xrnm8g
[Wed Jan 23 08:41:51 JST 2013]oAuthConsumerSecret: *******************************************
[Wed Jan 23 08:41:51 JST 2013]oAuthAccessToken: 6588392-YmeaLWSLJcDqswQDL8sdqrnoBcbr0Ht7CSLYDiRmpg
[Wed Jan 23 08:41:51 JST 2013]oAuthAccessTokenSecret: ******************************************
[Wed Jan 23 08:41:51 JST 2013]oAuthRequestTokenURL: http://api.twitter.com/oauth/request_token
[Wed Jan 23 08:41:51 JST 2013]oAuthAuthorizationURL: http://api.twitter.com/oauth/authorize
[Wed Jan 23 08:41:51 JST 2013]oAuthAccessTokenURL: http://api.twitter.com/oauth/access_token
[Wed Jan 23 08:41:51 JST 2013]oAuthAuthenticationURL: http://api.twitter.com/oauth/authenticate
[Wed Jan 23 08:41:51 JST 2013]restBaseURL: http://api.twitter.com/1.1/
[Wed Jan 23 08:41:51 JST 2013]streamBaseURL: https://stream.twitter.com/1.1/
[Wed Jan 23 08:41:51 JST 2013]userStreamBaseURL: https://userstream.twitter.com/1.1/
[Wed Jan 23 08:41:51 JST 2013]siteStreamBaseURL: https://sitestream.twitter.com/1.1/
[Wed Jan 23 08:41:51 JST 2013]dispatcherImpl: twitter4j.internal.async.DispatcherImpl
[Wed Jan 23 08:41:51 JST 2013]loggerFactory: null
[Wed Jan 23 08:41:51 JST 2013]asyncNumThreads: 1
[Wed Jan 23 08:41:51 JST 2013]contributingTo: -1
[Wed Jan 23 08:41:51 JST 2013]includeRTsEnabled: true
[Wed Jan 23 08:41:51 JST 2013]includeEntitiesEnabled: true
[Wed Jan 23 08:41:51 JST 2013]includeMyRetweetEnabled: true
[Wed Jan 23 08:41:51 JST 2013]jsonStoreEnabled: false
[Wed Jan 23 08:41:51 JST 2013]mbeanEnabled: false
[Wed Jan 23 08:41:51 JST 2013]userStreamRepliesAllEnabled: false
[Wed Jan 23 08:41:51 JST 2013]stallWarningsEnabled: true
[Wed Jan 23 08:41:51 JST 2013]mediaProvider: TWITTER
[Wed Jan 23 08:41:51 JST 2013]mediaProviderAPIKey: null
[Wed Jan 23 08:41:51 JST 2013]mediaProviderParameters: null
[Wed Jan 23 08:41:51 JST 2013]clientVersion: 3.0.4-SNAPSHOT(build: 741de654255dfd4f0b154533bbb4fa76a88f6ab3)
[Wed Jan 23 08:41:51 JST 2013]clientURL: http://twitter4j.org/en/twitter4j-3.0.4-SNAPSHOT(build: 741de654255dfd4f0b154533bbb4fa76a88f6ab3).xml
[Wed Jan 23 08:41:51 JST 2013]DALVIK: twitter4j.dalvik
[Wed Jan 23 08:41:51 JST 2013]GAE: twitter4j.gae
[Wed Jan 23 08:41:51 JST 2013]DEFAULT_OAUTH_REQUEST_TOKEN_URL: http://api.twitter.com/oauth/request_token
[Wed Jan 23 08:41:51 JST 2013]DEFAULT_OAUTH_AUTHORIZATION_URL: http://api.twitter.com/oauth/authorize
[Wed Jan 23 08:41:51 JST 2013]DEFAULT_OAUTH_ACCESS_TOKEN_URL: http://api.twitter.com/oauth/access_token
[Wed Jan 23 08:41:51 JST 2013]DEFAULT_OAUTH_AUTHENTICATION_URL: http://api.twitter.com/oauth/authenticate
[Wed Jan 23 08:41:51 JST 2013]DEFAULT_REST_BASE_URL: http://api.twitter.com/1.1/
[Wed Jan 23 08:41:52 JST 2013]DEFAULT_STREAM_BASE_URL: https://stream.twitter.com/1.1/
[Wed Jan 23 08:41:52 JST 2013]DEFAULT_USER_STREAM_BASE_URL: https://userstream.twitter.com/1.1/
[Wed Jan 23 08:41:52 JST 2013]DEFAULT_SITE_STREAM_BASE_URL: https://sitestream.twitter.com/1.1/
[Wed Jan 23 08:41:52 JST 2013]IS_DALVIK: false
[Wed Jan 23 08:41:52 JST 2013]IS_GAE: false
[Wed Jan 23 08:41:52 JST 2013]serialVersionUID: -6610497517837844232
[Wed Jan 23 08:41:52 JST 2013]dalvikDetected: false
[Wed Jan 23 08:41:52 JST 2013]gaeDetected: false
[Wed Jan 23 08:41:52 JST 2013]requestHeaders: {X-Twitter-Client-URL=http://twitter4j.org/en/twitter4j-3.0.4-SNAPSHOT(build: 741de654255dfd4f0b154533bbb4fa76a88f6ab3).xml, X-Twitter-Client=Twitter4J, Accept-Encoding=gzip, User-Agent=twitter4j http://twitter4j.org/ /3.0.4-SNAPSHOT(build: 741de654255dfd4f0b154533bbb4fa76a88f6ab3), X-Twitter-Client-Version=3.0.4-SNAPSHOT(build: 741de654255dfd4f0b154533bbb4fa76a88f6ab3)}
[Wed Jan 23 08:41:52 JST 2013]instances: [ConfigurationBase{debug=true, userAgent='twitter4j http://twitter4j.org/ /3.0.4-SNAPSHOT(build: 741de654255dfd4f0b154533bbb4fa76a88f6ab3)', user='null', password='null', useSSL=false, prettyDebug=false, gzipEnabled=true, httpProxyHost='null', httpProxyUser='null', httpProxyPassword='null', httpProxyPort=-1, httpConnectionTimeout=20000, httpReadTimeout=120000, httpStreamingReadTimeout=40000, httpRetryCount=0, httpRetryIntervalSeconds=5, maxTotalConnections=20, defaultMaxPerRoute=2, oAuthConsumerKey='u3T6gYq17fBjQ1xrnm8g', oAuthConsumerSecret='5GLiKBRtILkqIWAA305yJk6lfF4PEdqtzW0ZOKOo1i8', oAuthAccessToken='6588392-YmeaLWSLJcDqswQDL8sdqrnoBcbr0Ht7CSLYDiRmpg ', oAuthAccessTokenSecret='EaMAvSCrQl7qOwJkABZyKCLcabZZ5INd0KB1JKqEes', oAuthRequestTokenURL='http://api.twitter.com/oauth/request_token', oAuthAuthorizationURL='http://api.twitter.com/oauth/authorize', oAuthAccessTokenURL='http://api.twitter.com/oauth/access_token', oAuthAuthenticationURL='http://api.twitter.com/oauth/authenticate', restBaseURL='http://api.twitter.com/1.1/', streamBaseURL='https://stream.twitter.com/1.1/', userStreamBaseURL='https://userstream.twitter.com/1.1/', siteStreamBaseURL='https://sitestream.twitter.com/1.1/', dispatcherImpl='twitter4j.internal.async.DispatcherImpl', loggerFactory='null', asyncNumThreads=1, contributingTo=-1, includeRTsEnabled=true, includeEntitiesEnabled=true, includeMyRetweetEnabled=true, jsonStoreEnabled=false, mbeanEnabled=false, userStreamRepliesAllEnabled=false, stallWarningsEnabled=true, mediaProvider='TWITTER', mediaProviderAPIKey='null', mediaProviderParameters=null, clientVersion='3.0.4-SNAPSHOT(build: 741de654255dfd4f0b154533bbb4fa76a88f6ab3)', clientURL='http://twitter4j.org/en/twitter4j-3.0.4-SNAPSHOT(build: 741de654255dfd4f0b154533bbb4fa76a88f6ab3).xml', IS_DALVIK=false, IS_GAE=false, requestHeaders={X-Twitter-Client-URL=http://twitter4j.org/en/twitter4j-3.0.4-SNAPSHOT(build: 741de654255dfd4f0b154533bbb4fa76a88f6ab3).xml, X-Twitter-Client=Twitter4J, Accept-Encoding=gzip, User-Agent=twitter4j http://twitter4j.org/ /3.0.4-SNAPSHOT(build: 741de654255dfd4f0b154533bbb4fa76a88f6ab3), X-Twitter-Client-Version=3.0.4-SNAPSHOT(build: 741de654255dfd4f0b154533bbb4fa76a88f6ab3)}}]
[Wed Jan 23 08:41:52 JST 2013]Request:
[Wed Jan 23 08:41:52 JST 2013]POST http://api.twitter.com/1.1/statuses/update.json
[Wed Jan 23 08:41:52 JST 2013]OAuth base string: POST&http%3A%2F%2Fapi.twitter.com%2F1.1%2Fstatuses%2Fupdate.json&include_entities%3D1%26include_rts%3D1%26oauth_consumer_key%3Du3T6gYq17fBjQ1xrnm8g%26oauth_nonce%3D3206276498%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1358898112%26oauth_token%3D6588392-YmeaLWSLJcDqswQDL8sdqrnoBcbr0Ht7CSLYDiRmpg%2520%26oauth_version%3D1.0%26status%3Dtest
[Wed Jan 23 08:41:52 JST 2013]OAuth signature: ppnBeFh/FkC257KfuAseMUSz2yE=
[Wed Jan 23 08:41:52 JST 2013]Authorization: ***********************************************************************************************************************************************************************************************************************************************************************************
[Wed Jan 23 08:41:52 JST 2013]X-Twitter-Client-URL: http://twitter4j.org/en/twitter4j-3.0.4-SNAPSHOT(build: 741de654255dfd4f0b154533bbb4fa76a88f6ab3).xml
[Wed Jan 23 08:41:52 JST 2013]X-Twitter-Client: Twitter4J
[Wed Jan 23 08:41:52 JST 2013]Accept-Encoding: gzip
[Wed Jan 23 08:41:52 JST 2013]User-Agent: twitter4j http://twitter4j.org/ /3.0.4-SNAPSHOT(build: 741de654255dfd4f0b154533bbb4fa76a88f6ab3)
[Wed Jan 23 08:41:52 JST 2013]X-Twitter-Client-Version: 3.0.4-SNAPSHOT(build: 741de654255dfd4f0b154533bbb4fa76a88f6ab3)
[Wed Jan 23 08:41:52 JST 2013]Post Params: status=test&include_entities=1&include_rts=1
[Wed Jan 23 08:41:52 JST 2013]Response:
[Wed Jan 23 08:41:52 JST 2013]HTTP/1.1 400 Bad Request
[Wed Jan 23 08:41:52 JST 2013]Date: Tue, 22 Jan 2013 23:41:52 UTC
[Wed Jan 23 08:41:52 JST 2013]Content-Length: 86
[Wed Jan 23 08:41:52 JST 2013]Content-Encoding: gzip
[Wed Jan 23 08:41:52 JST 2013]Content-Type: application/json; charset=utf-8
[Wed Jan 23 08:41:52 JST 2013]Server: tfe
[Wed Jan 23 08:41:52 JST 2013]{"errors":[{"message":"Bad Authentication data","code":215}]}
The problem is solved.
I changed the code to
Twitter twitter = new TwitterFactory().getInstance();
and successfully a message is tweeted in my timeline.
Related
Restrict my website to one per one user without authentification - compare date in array
How can I restrict a page only one by one user without authentification and with a delay of 3min between the first and second user. If anyone have a clue or a solution ? Here is what I try to do : A unique User land in my page and fill a form and as I want to pick him by making a User.last I need him to be uniq on my page (to not disturb the registration and to be sure that i'm picking the right user with the User.last) and to wait 3 min for the second user to access this page. Like I have 10 peoples trying to access my website, 1 in filling the form, the 9 others are standing in a queue and waiting to access to my page. Actualluy try to do this in Ruby on Rails. My view with the field looks like this (need to allow only one by one user on this page) : <div class="container"> <center><h1>New Connection </h1></center> <%= form_with model: #email, local: true do |form|%> <% if #email.errors.any? %> <div id="error explanation" class="alert alert-danger"> <p>Erreur(s) : </p> <ul> <% #email.errors.full_messages.each do |message|%> <li><%=message%></li> <% end %> </ul> </div> <% end %> <div class="form-group"> <%= label_tag :email %> : <%= form.email_field :email, placeholder: "Email", class: "form-control" %> </div> <div class="form-group"> <%= form.submit class: "btn btn-primary btn-lg btn-block", value: "Se connecter" %> </div> <%end%> </div> My email controller : class EmailsController < ApplicationController def index redirect_to root_path end def new #email = Email.new sessions = ActiveRecord::SessionStore::Session.pluck(:updated_at) #return an array like this : [Tue, 21 Jan 2020 13:27:03 UTC +00:00, Tue, 21 Jan 2020 13:27:09 UTC +00:00, Tue, 21 Jan 2020 13:27:42 UTC +00:00, Tue, 21 Jan 2020 13:31:33 UTC +00:00, Tue, 21 Jan 2020 13:36:21 UTC +00:00, Tue, 21 Jan 2020 13:42:02 UTC +00:00, Tue, 21 Jan 2020 13:42:02 UTC +00:00, Tue, 21 Jan 2020 13:42:03 UTC +00:00, Tue, 21 Jan 2020 13:42:03 UTC +00:00, Tue, 21 Jan 2020 13:42:04 UTC +00:00, Tue, 21 Jan 2020 13:42:04 UTC +00:00, Tue, 21 Jan 2020 13:44:47 UTC +00:00, Tue, 21 Jan 2020 13:44:47 UTC +00:00, Tue, 21 Jan 2020 13:46:24 UTC +00:00, Tue, 21 Jan 2020 13:49:10 UTC +00:00, Tue, 21 Jan 2020 13:49:38 UTC +00:00, Tue, 21 Jan 2020 13:50:54 UTC +00:00, Tue, 21 Jan 2020 13:50:54 UTC +00:00, Tue, 21 Jan 2020 14:03:42 UTC +00:00, Tue, 21 Jan 2020 14:07:11 UTC +00:00, Tue, 21 Jan 2020 14:07:11 UTC +00:00, Tue, 21 Jan 2020 14:07:12 UTC +00:00, Tue, 21 Jan 2020 14:07:12 UTC +00:00, Tue, 21 Jan 2020 14:36:39 UTC +00:00, Tue, 21 Jan 2020 14:36:48 UTC +00:00, Tue, 21 Jan 2020 14:36:57 UTC +00:00, Tue, 21 Jan 2020 14:42:02 UTC +00:00, Tue, 21 Jan 2020 14:42:03 UTC +00:00, Tue, 21 Jan 2020 14:44:16 UTC +00:00, Tue, 21 Jan 2020 14:52:46 UTC +00:00, Tue, 21 Jan 2020 14:52:47 UTC +00:00, Tue, 21 Jan 2020 14:52:47 UTC +00:00, Tue, 21 Jan 2020 14:54:48 UTC +00:00, Wed, 22 Jan 2020 08:09:47 UTC +00:00, Wed, 22 Jan 2020 08:09:48 UTC +00:00, Wed, 22 Jan 2020 08:12:06 UTC +00:00, Wed, 22 Jan 2020 08:12:07 UTC +00:00, Wed, 22 Jan 2020 08:12:16 UTC +00:00, Wed, 22 Jan 2020 08:12:17 UTC +00:00, Wed, 22 Jan 2020 08:12:17 UTC +00:00, Wed, 22 Jan 2020 08:12:29 UTC +00:00, Wed, 22 Jan 2020 08:12:39 UTC +00:00, Wed, 22 Jan 2020 08:12:40 UTC +00:00, Wed, 22 Jan 2020 08:13:07 UTC +00:00, Wed, 22 Jan 2020 08:13:07 UTC +00:00, Wed, 22 Jan 2020 08:13:08 UTC +00:00, Wed, 22 Jan 2020 08:15:34 UTC +00:00, Wed, 22 Jan 2020 08:22:38 UTC +00:00, Wed, 22 Jan 2020 08:25:28 UTC +00:00, Wed, 22 Jan 2020 08:25:29 UTC +00:00, Wed, 22 Jan 2020 08:25:29 UTC +00:00, Wed, 22 Jan 2020 08:29:24 UTC +00:00, Wed, 22 Jan 2020 08:36:15 UTC +00:00, Wed, 22 Jan 2020 08:36:16 UTC +00:00, Wed, 22 Jan 2020 08:36:16 UTC +00:00, Wed, 22 Jan 2020 08:36:17 UTC +00:00, Wed, 22 Jan 2020 08:36:17 UTC +00:00, Wed, 22 Jan 2020 08:36:18 UTC +00:00, Wed, 22 Jan 2020 08:36:18 UTC +00:00, Wed, 22 Jan 2020 08:36:30 UTC +00:00, Wed, 22 Jan 2020 08:40:30 UTC +00:00, Wed, 22 Jan 2020 08:40:30 UTC +00:00, Wed, 22 Jan 2020 08:41:14 UTC +00:00, Wed, 22 Jan 2020 08:50:25 UTC +00:00, Wed, 22 Jan 2020 08:50:26 UTC +00:00, Wed, 22 Jan 2020 08:50:27 UTC +00:00, Wed, 22 Jan 2020 08:50:37 UTC +00:00, Wed, 22 Jan 2020 08:50:37 UTC +00:00, Wed, 22 Jan 2020 08:50:38 UTC +00:00, Wed, 22 Jan 2020 08:50:48 UTC +00:00, Wed, 22 Jan 2020 08:50:48 UTC +00:00, Wed, 22 Jan 2020 08:50:49 UTC +00:00, Wed, 22 Jan 2020 08:57:58 UTC +00:00, Wed, 22 Jan 2020 08:57:59 UTC +00:00, Wed, 22 Jan 2020 09:04:29 UTC +00:00, Wed, 22 Jan 2020 09:04:29 UTC +00:00, Wed, 22 Jan 2020 09:04:30 UTC +00:00, Wed, 22 Jan 2020 09:04:39 UTC +00:00, Wed, 22 Jan 2020 09:04:39 UTC +00:00, Wed, 22 Jan 2020 09:04:40 UTC +00:00, Wed, 22 Jan 2020 09:04:49 UTC +00:00, Wed, 22 Jan 2020 09:04:49 UTC +00:00, Wed, 22 Jan 2020 09:04:50 UTC +00:00, Wed, 22 Jan 2020 09:13:21 UTC +00:00, Wed, 22 Jan 2020 09:17:48 UTC +00:00, Wed, 22 Jan 2020 09:20:03 UTC +00:00, Wed, 22 Jan 2020 09:27:54 UTC +00:00, Wed, 22 Jan 2020 09:27:54 UTC +00:00, Wed, 22 Jan 2020 09:28:04 UTC +00:00, Wed, 22 Jan 2020 09:28:05 UTC +00:00, Wed, 22 Jan 2020 09:35:42 UTC +00:00, Wed, 22 Jan 2020 09:35:43 UTC +00:00, Wed, 22 Jan 2020 09:35:58 UTC +00:00] #How can I say that if a session is in the same minute than the previous session, redirect the one last one to another url? end def create #email = Email.create(email_params) respond_to do |format| if#email.persisted? format.html {redirect_to invoice_index_path, notice: 'Email Validated '} else format.html{render :new} end end end private def email_params params.require(:email).permit(:email, :id_user) end end EDIT : As suggested in the comment part, I tried to use the session, I can access to the session made by rails with request.session_options[:id] but how can I count them ? EDIT 2 : As suggested by ARK in the comment section, I try to use the Session created by Rails, I edited my controller part . How can I say that if a session is in the same minute than the previous session, redirect the one last one to another url ?
Ruby on Rails array group sort by value
I've got an array with with arrays, containing a key and a timestamp. ["kacec6ybetpjdzlfgnnxya", Fri, 12 May 2017 22:00:51 CEST +02:00], ["kacec6ybetpjdzlfgnnxya", Fri, 12 May 2017 22:00:32 CEST +02:00], ["kacec6ybetpjdzlfgnnxya", Fri, 12 May 2017 21:58:33 CEST +02:00], ["kacec6ybetpjdzlfgnnxya", Fri, 12 May 2017 21:58:01 CEST +02:00], ["kacec6ybetpjdzlfgnnxya", Fri, 12 May 2017 21:58:51 CEST +02:00], ["3wyadsrrdxtgieyxx_lgka", Sat, 13 May 2017 01:09:01 CEST +02:00], ["y-5he42vlloggjb_whm8jw", Sat, 22 Apr 2017 22:48:31 CEST +02:00], ["oaxej30u9we17onlug4orw", Sun, 23 Apr 2017 01:46:48 CEST +02:00], ["oaxej30u9we17onlug4orw", Sun, 23 Apr 2017 02:06:56 CEST +02:00], ["rqjwg1ka43mvri0dmrdxvg", Sun, 23 Apr 2017 17:23:34 CEST +02:00], ["ok8nq6tg-kor9jglsuhoyw", Tue, 25 Apr 2017 13:02:16 CEST +02:00], ["riwfm0m-0rmbb6e9kyug2g", Sat, 06 May 2017 06:12:27 CEST +02:00], ["riwfm0m-0rmbb6e9kyug2g", Sat, 06 May 2017 06:17:01 CEST +02:00], ["riwfm0m-0rmbb6e9kyug2g", Sat, 06 May 2017 06:18:04 CEST +02:00], ["gbqfn3_d_tritqoey5khjw", Sat, 06 May 2017 14:14:55 CEST +02:00], ["j___x1oap-veh0u1fo_oua", Sun, 07 May 2017 14:22:37 CEST +02:00], ... I received this list by ActiveRecord. MyModel.all.pluck(:token, :created_at) The Model containing some uniq tokens and some duplicates. The duplicates are interesting. I want to group the timestaps by the key and look for the first and the last timestamp for each key. So I grouped the array as following: grp = arr.group_by { |key, ts| key} Now I receive a list like this: "vwfv8n5obwqmaw8r9fj-yq"=>[ ["vwfv8n5obwqmaw8r9fj-yq", Thu, 11 May 2017 10:24:42 CEST +02:00] ], "kacec6ybetpjdzlfgnnxya"=> [ ["kacec6ybetpjdzlfgnnxya", Fri, 12 May 2017 22:00:31 CEST +02:00], ["kacec6ybetpjdzlfgnnxya", Fri, 12 May 2017 22:01:43 CEST +02:00], ["kacec6ybetpjdzlfgnnxya", Fri, 12 May 2017 21:58:17 CEST +02:00], ["kacec6ybetpjdzlfgnnxya", Fri, 12 May 2017 21:59:05 CEST +02:00], ["kacec6ybetpjdzlfgnnxya", Fri, 12 May 2017 21:59:59 CEST +02:00] ], ... Is it possible to sort the dates to get the first and the last date easily? Am I too complicated? I think there should be an easier way to handle the raw data.
To get a a hash with the token as the key and the timestamps as values: # this gives the same MIN and MAX if there is only one created_at in the group rows = MyModel.group(:token) .pluck("token, MIN(created_at), MAX(created_at)") # loop though rows and create a hash rows.each_with_object({}) do |(token, *t), hash| hash[token] = t.uniq # removes dupes end { "rqjwg1ka43mvri0dmrdxvg"=>[2017-04-23 15:23:34 UTC], "riwfm0m-0rmbb6e9kyug2g"=>[2017-05-06 04:12:27 UTC, 2017-05-06 04:18:04 UTC] # ... } If you are simply looking for the records which have duplicates you can just use a WHERE clause that counts the records: MyModel.where("(SELECT COUNT(*) FROM things t WHERE t.token = things.token) > 1")
You could do this: # you already have this bit grp = arr.group_by { |key, ts| key} # get the minmax values for each group grp.map { |k, values_array| { k => values_array.minmax } }.reduce Hash.new, :merge This should yield something that looks like: { "vwfv8n5obwqmaw8r9fj-yq"=>[ [Thu, 11 May 2017 10:24:42 CEST +02:00, Thu, 11 May 2017 10:24:42 CEST +02:00] ], "kacec6ybetpjdzlfgnnxya"=> [ [Fri, 12 May 2017 21:58:17 CEST +02:00, Fri, 12 May 2017 22:01:43 CEST +02:00] ], ... }
try something like this: MyModel.order(:created_at).pluck(:token, :created_at).group_by { |key, ts| key }.flat_map{ |k, v| { k => [v.first, v.last] } }
Issue with Hashes and Arrays of Datetimes in Ruby
I am creating a list of Datetimes for available booking in a Rails app. I wrote a quick function to group the Datetimes by day in order to organize them for display in my view. My Code: def self.sort_by_day(times) day = times[0].strftime("%d") array = [] days_hash = Hash.new counter = 0 while counter < times.size if times[counter].strftime("%d") == day array.push(times[counter]) counter += 1 else days_hash[times[counter].strftime("%d")] = array logger.debug "#{days_hash}" array.clear array << times[counter] day = times[counter].strftime("%d") counter += 1 end end days_hash end {"18"=>[Fri, 29 Jan 2016 08:00:00 -0500, Fri, 29 Jan 2016 09:00:00 -0500, Fri, 29 Jan 2016 10:00:00 -0500, Fri, 29 Jan 2016 11:00:00 -0500, Fri, 29 Jan 2016 12:00:00 -0500, Fri, 29 Jan 2016 13:00:00 -0500, Fri, 29 Jan 2016 14:00:00 -0500, Fri, 29 Jan 2016 15:00:00 -0500, Fri, 29 Jan 2016 16:00:00 -0500, Fri, 29 Jan 2016 17:00:00 -0500], "19"=>[Fri, 29 Jan 2016 08:00:00 -0500, Fri, 29 Jan 2016 09:00:00 -0500, Fri, 29 Jan 2016 10:00:00 -0500, Fri, 29 Jan 2016 11:00:00 -0500, Fri, 29 Jan 2016 12:00:00 -0500, Fri, 29 Jan 2016 13:00:00 -0500, Fri, 29 Jan 2016 14:00:00 -0500, Fri, 29 Jan 2016 15:00:00 -0500, Fri, 29 Jan 2016 16:00:00 -0500, Fri, 29 Jan 2016 17:00:00 -0500], "20"=>[Fri, 29 Jan 2016 08:00:00 -0500, Fri, 29 Jan 2016 09:00:00 -0500, Fri, 29 Jan 2016 10:00:00 -0500, Fri, 29 Jan 2016 11:00:00 -0500, Fri, 29 Jan 2016 12:00:00 -0500, Fri, 29 Jan 2016 13:00:00 -0500, Fri, 29 Jan 2016 14:00:00 -0500, Fri, 29 Jan 2016 15:00:00 -0500, Fri, 29 Jan 2016 16:00:00 -0500, Fri, 29 Jan 2016 17:00:00 -0500], "21"=>[Fri, 29 Jan 2016 08:00:00 -0500, Fri, 29 Jan 2016 09:00:00 -0500, Fri, 29 Jan 2016 10:00:00 -0500, Fri, 29 Jan 2016 11:00:00 -0500, Fri, 29 Jan 2016 12:00:00 -0500, Fri, 29 Jan 2016 13:00:00 -0500, Fri, 29 Jan 2016 14:00:00 -0500, Fri, 29 Jan 2016 15:00:00 -0500, Fri, 29 Jan 2016 16:00:00 -0500, Fri, 29 Jan 2016 17:00:00 -0500], "22"=>[Fri, 29 Jan 2016 08:00:00 -0500, Fri, 29 Jan 2016 09:00:00 -0500, Fri, 29 Jan 2016 10:00:00 -0500, Fri, 29 Jan 2016 11:00:00 -0500, Fri, 29 Jan 2016 12:00:00 -0500, Fri, 29 Jan 2016 13:00:00 -0500, Fri, 29 Jan 2016 14:00:00 -0500, Fri, 29 Jan 2016 15:00:00 -0500, Fri, 29 Jan 2016 16:00:00 -0500, Fri, 29 Jan 2016 17:00:00 -0500], "24"=>[Fri, 29 Jan 2016 08:00:00 -0500, Fri, 29 Jan 2016 09:00:00 -0500, Fri, 29 Jan 2016 10:00:00 -0500, Fri, 29 Jan 2016 11:00:00 -0500, Fri, 29 Jan 2016 12:00:00 -0500, Fri, 29 Jan 2016 13:00:00 -0500, Fri, 29 Jan 2016 14:00:00 -0500, Fri, 29 Jan 2016 15:00:00 -0500, Fri, 29 Jan 2016 16:00:00 -0500, Fri, 29 Jan 2016 17:00:00 -0500], "25"=>[Fri, 29 Jan 2016 08:00:00 -0500, Fri, 29 Jan 2016 09:00:00 -0500, Fri, 29 Jan 2016 10:00:00 -0500, Fri, 29 Jan 2016 11:00:00 -0500, Fri, 29 Jan 2016 12:00:00 -0500, Fri, 29 Jan 2016 13:00:00 -0500, Fri, 29 Jan 2016 14:00:00 -0500, Fri, 29 Jan 2016 15:00:00 -0500, Fri, 29 Jan 2016 16:00:00 -0500, Fri, 29 Jan 2016 17:00:00 -0500], "26"=>[Fri, 29 Jan 2016 08:00:00 -0500, Fri, 29 Jan 2016 09:00:00 -0500, Fri, 29 Jan 2016 10:00:00 -0500, Fri, 29 Jan 2016 11:00:00 -0500, Fri, 29 Jan 2016 12:00:00 -0500, Fri, 29 Jan 2016 13:00:00 -0500, Fri, 29 Jan 2016 14:00:00 -0500, Fri, 29 Jan 2016 15:00:00 -0500, Fri, 29 Jan 2016 16:00:00 -0500, Fri, 29 Jan 2016 17:00:00 -0500], "27"=>[Fri, 29 Jan 2016 08:00:00 -0500, Fri, 29 Jan 2016 09:00:00 -0500, Fri, 29 Jan 2016 10:00:00 -0500, Fri, 29 Jan 2016 11:00:00 -0500, Fri, 29 Jan 2016 12:00:00 -0500, Fri, 29 Jan 2016 13:00:00 -0500, Fri, 29 Jan 2016 14:00:00 -0500, Fri, 29 Jan 2016 15:00:00 -0500, Fri, 29 Jan 2016 16:00:00 -0500, Fri, 29 Jan 2016 17:00:00 -0500], "28"=>[Fri, 29 Jan 2016 08:00:00 -0500, Fri, 29 Jan 2016 09:00:00 -0500, Fri, 29 Jan 2016 10:00:00 -0500, Fri, 29 Jan 2016 11:00:00 -0500, Fri, 29 Jan 2016 12:00:00 -0500, Fri, 29 Jan 2016 13:00:00 -0500, Fri, 29 Jan 2016 14:00:00 -0500, Fri, 29 Jan 2016 15:00:00 -0500, Fri, 29 Jan 2016 16:00:00 -0500, Fri, 29 Jan 2016 17:00:00 -0500], "29"=>[Fri, 29 Jan 2016 08:00:00 -0500, Fri, 29 Jan 2016 09:00:00 -0500, Fri, 29 Jan 2016 10:00:00 -0500, Fri, 29 Jan 2016 11:00:00 -0500, Fri, 29 Jan 2016 12:00:00 -0500, Fri, 29 Jan 2016 13:00:00 -0500, Fri, 29 Jan 2016 14:00:00 -0500, Fri, 29 Jan 2016 15:00:00 -0500, Fri, 29 Jan 2016 16:00:00 -0500, Fri, 29 Jan 2016 17:00:00 -0500], "31"=>[Fri, 29 Jan 2016 08:00:00 -0500, Fri, 29 Jan 2016 09:00:00 -0500, Fri, 29 Jan 2016 10:00:00 -0500, Fri, 29 Jan 2016 11:00:00 -0500, Fri, 29 Jan 2016 12:00:00 -0500, Fri, 29 Jan 2016 13:00:00 -0500, Fri, 29 Jan 2016 14:00:00 -0500, Fri, 29 Jan 2016 15:00:00 -0500, Fri, 29 Jan 2016 16:00:00 -0500, Fri, 29 Jan 2016 17:00:00 -0500]} My code is overwriting the hash values of previous days each time a new key/value pair is assigned.I previously got similar results using arrays. What am I doing wrong here and how is the hash overwriting previous values?
If you have an array of Rails DateTime object, and you want to group them by day of month and have the resultant hash ordered by day value, then, you can do the following: ary.sort_by(&:day).group_by(&:day) Working example for illustration is provided below: require "pp" require "active_support/core_ext/date_time" ary = [ (DateTime.civil_from_format :local, 2015, 12, 13), (DateTime.civil_from_format :local, 2015, 10, 10), (DateTime.civil_from_format :local, 2015, 11, 10), (DateTime.civil_from_format :local, 2015, 11, 11), (DateTime.civil_from_format :local, 2015, 11, 12), (DateTime.civil_from_format :local, 2015, 12, 13), ] pp ary.sort_by(&:day).group_by(&:day) #=> {10=>[Sat, 10 Nov 2015 00:00:00 +0530, Sat, 10 Oct 2015 00:00:00 +0530], # 11=>[Wed, 11 Nov 2015 00:00:00 +0530], # 12=>[Thu, 12 Nov 2015 00:00:00 +0530], # 13=>[Sun, 13 Dec 2015 00:00:00 +0530, Sun, 13 Dec 2015 00:00:00 +0530]}
Timezone offset rails
In rails how to get the exect day after adding offset irb(main):066:0> DateTime.now.utc => Sat, 29 Mar 2014 19:12:57 +0000 after adding 5.30 hours it should show 30 march. why it is 29 again .anything I missed here irb(main):065:0> DateTime.now.utc.change(:offset => "+0530") => Sat, 29 Mar 2014 19:09:03 +0530
Change a hash into an array
I have an Rails 4 application that collects attendance at church services. Some weeks there are two services and some weeks there is only one. I need to get the total attendance for each week and show it as a graph. By calling: Stat.calculate(:sum, :attendance, group: :date) in the console I have been able to collect the data in a hash like this: {Sun, 06 Jan 2013=>66, Sun, 13 Jan 2013=>65, Sun, 20 Jan 2013=>60, Sun, 27 Jan 2013=>67, Sun, 03 Feb 2013=>60, Sun, 10 Feb 2013=>76, Sun, 17 Feb 2013=>65, Sun, 24 Feb 2013=>52, Sun, 03 Mar 2013=>52, Sun, 10 Mar 2013=>45, Sun, 17 Mar 2013=>56, Sun, 24 Mar 2013=>134, Sun, 31 Mar 2013=>76, Sun, 07 Apr 2013=>88, Sun, 14 Apr 2013=>87, Sun, 28 Apr 2013=>93, Sun, 05 May 2013=>93, Sun, 12 May 2013=>95, Sun, 19 May 2013=>90, Sun, 26 May 2013=>87, Sun, 02 Jun 2013=>71, Sun, 09 Jun 2013=>86, Sun, 16 Jun 2013=>109, Sun, 23 Jun 2013=>80, Sun, 30 Jun 2013=>68, Sun, 07 Jul 2013=>75, Sun, 14 Jul 2013=>73} But What I need for my chart is an array of hashes in the form of: {date: "Sun, 23 Jun 2013", attendance: 80}, {date: "Sun, 30 Jun 2013", attendance: 68"} So I am trying to figure out how to convert the first form into the second form. I'm sure its something pretty easy, but my limited rails knowledge is hitting a wall.
.collect{|key,value| {:date => key, :attendance => value} } loop through and create a new hash where the original key becomes the value for date and the original value becomes the value for attendance. These new hashes are collected into an array.
You can think as below: h = {"Sun, 06 Jan 2013"=>66, "Sun, 13 Jan 2013"=>65, "Sun, 20 Jan 2013"=>60 } h.map{|k,v| Hash[:date,k,:attend,v]} # => [{:date=>"Sun, 06 Jan 2013", :attend=>66}, # {:date=>"Sun, 13 Jan 2013", :attend=>65}, # {:date=>"Sun, 20 Jan 2013", :attend=>60}]