Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Grabbers::Twitter < Grabbers::Base
- def build_client(access_token, secret_key)
- @api_client ||= Twitter::Client.new(oauth_token: access_token, oauth_token_secret: secret_key)
- end
- def grab_endpoint(endpoint, options={})
- result = nil
- endpoint_params = build_endpoint_params(endpoint)
- begin
- options = prepare_options(endpoint, endpoint_params, options)
- result = self.send(endpoint_params['method'], options)
- rescue Twitter::Error::Unauthorized => error
- tokens_error(endpoint_params['method'], error)
- rescue Twitter::Error::BadRequest => error
- if error.to_s == 'Bad Authentication data'
- tokens_error(endpoint_params['method'], error)
- else
- ErrorsService.log(error, title: "Error on Grab endpoint for #{service}", social_profile: social_profile,
- data: {service: service, endpoint: endpoint, options: options, access_token: @access_token, secret_key: @secret_key})
- common_error(endpoint_params['method'], error)
- end
- rescue Twitter::Error::TooManyRequests => error
- rate_limit_error(endpoint_params['method'], error)
- rescue Twitter::Error::ServerError => error
- rate_limit_error(endpoint_params['method'], error, 10.minutes.to_i)
- rescue JSON::ParserError => error
- rate_limit_error(endpoint_params['method'], error, 15.seconds.to_i)
- rescue Exception => error
- common_error(endpoint_params['method'], error, options)
- end
- result
- end
- protected
- def prepare_options(endpoint, endpoint_params, options)
- result = {}
- result[:count] = per_process_limit(options)
- result[:max_id] = options[:until] if options[:until].present?
- result[:since_id] = options[:since] if options[:since].present?
- result
- end
- def service
- 'twitter'
- end
- def user(options)
- [@api_client.user.attrs]
- end
- def friends(options)
- begin
- options[:count] = 200 #set manually for obtain maximum friends.
- build_data_by_attributes @api_client.friends(options) # Will raised error if user has more than 3000 friends
- rescue Exception => error
- return []
- end
- end
- def mentions_timeline(options)
- build_data_by_attributes @api_client.mentions_timeline(options)
- end
- def ะต(options)
- build_data_by_attributes @api_client.home_timeline(options)
- end
- def user_timeline(options)
- build_data_by_attributes @api_client.user_timeline(options)
- end
- def build_data_by_attributes(data)
- result = []
- data.each do |resource|
- result << resource.attrs
- end
- result
- end
- def endpoints_with_pagination
- %w(home feed)
- end
- end
Add Comment
Please, Sign In to add comment