birdsong 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '03486db7a9efbfce2f7d423f25e33506332bad5a7ceb481da98e68c81b0a75fa'
4
+ data.tar.gz: 7a46972f68f413a3d3e0cb81b06ece6b9a3a8f9593be5a68664ce0364ab366ec
5
+ SHA512:
6
+ metadata.gz: 68eb9d33bac12dc765b776ad32024992640b1363612f99b2cc0b500c6000fd3ef7351d8cc70218d2d21bb681ec40b233b24fdd7062694426891ce3fa22088e62
7
+ data.tar.gz: 8ceddb1eb88a57dec49116653f0be80be47d29d395894ea35a28aafa84eee72d5aa13deca419e569861f715f5cceff93732473fe5cc9e32ed29ed0cd4ae0eb4b
@@ -0,0 +1,22 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+
6
+ jobs:
7
+ build:
8
+ environment: CI
9
+ runs-on: ubuntu-latest
10
+ env:
11
+ TWITTER_BEARER_TOKEN: '${{ secrets.TWITTER_BEARER_TOKEN }}'
12
+ steps:
13
+ - uses: actions/checkout@v2
14
+ - name: Set up Ruby
15
+ uses: ruby/setup-ruby@v1
16
+ with:
17
+ ruby-version: 3.1.2
18
+ - name: Run the default task
19
+ run: |
20
+ gem install bundler -v 2.2.14
21
+ bundle install
22
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .env
10
+ .byebug_history
data/.rubocop.yml ADDED
@@ -0,0 +1,67 @@
1
+ require:
2
+ - rubocop-rails
3
+
4
+ inherit_gem:
5
+ rubocop-rails_config:
6
+ - config/rails.yml
7
+
8
+ AllCops:
9
+ Exclude:
10
+ - db/schema.rb
11
+ - 'node_modules/**/*'
12
+ - 'redis-stable/**/*'
13
+ - 'bin/**/*'
14
+ - 'vendor/**/*'
15
+ TargetRubyVersion: 3.0
16
+
17
+ # Rails generates this file
18
+ Style/BlockComments:
19
+ Exclude:
20
+ - 'db/seeds.rb'
21
+
22
+ # This sets us to use the standard Rails format instead of Rubocop's
23
+ # opinionated Ruby style.
24
+ Style/FrozenStringLiteralComment:
25
+ Enabled: false
26
+
27
+ # This sets us to use the standard Rails format instead of Rubocop's
28
+ # opinionated Ruby style.
29
+ Style/ClassAndModuleChildren:
30
+ Enabled: false
31
+
32
+ # Rails generates this file
33
+ Layout/IndentationStyle:
34
+ Exclude:
35
+ - 'db/seeds.rb'
36
+
37
+ # Temporarily turn this off
38
+ Metrics/AbcSize:
39
+ Enabled: false
40
+
41
+ Metrics/ClassLength:
42
+ Enabled: false
43
+
44
+ Lint/RescueException:
45
+ Enabled: true
46
+
47
+ Lint/Debugger:
48
+ Enabled: true
49
+
50
+ Rails/HasManyOrHasOneDependent:
51
+ Enabled: true
52
+
53
+ Rails/HasAndBelongsToMany:
54
+ Enabled: true
55
+
56
+ Style/NumericPredicate:
57
+ Enabled: true
58
+
59
+ # This sets us to use the standard Rails format instead of Rubocop's
60
+ # opinionated Ruby style.
61
+ Layout/EmptyLinesAroundAccessModifier:
62
+ Enabled: true
63
+ EnforcedStyle: 'around'
64
+
65
+ # We're not a Rails app, just using their style sheet
66
+ Rails/AssertNot:
67
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.4
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-04-27
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at cguess@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in birdsong.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,133 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ birdsong (0.1.0)
5
+ oauth (~> 0.5.6)
6
+ typhoeus (~> 1.4.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actionpack (7.0.1)
12
+ actionview (= 7.0.1)
13
+ activesupport (= 7.0.1)
14
+ rack (~> 2.0, >= 2.2.0)
15
+ rack-test (>= 0.6.3)
16
+ rails-dom-testing (~> 2.0)
17
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
18
+ actionview (7.0.1)
19
+ activesupport (= 7.0.1)
20
+ builder (~> 3.1)
21
+ erubi (~> 1.4)
22
+ rails-dom-testing (~> 2.0)
23
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
24
+ activesupport (7.0.1)
25
+ concurrent-ruby (~> 1.0, >= 1.0.2)
26
+ i18n (>= 1.6, < 2)
27
+ minitest (>= 5.1)
28
+ tzinfo (~> 2.0)
29
+ ast (2.4.2)
30
+ builder (3.2.4)
31
+ byebug (11.1.3)
32
+ concurrent-ruby (1.1.9)
33
+ crass (1.0.6)
34
+ dotenv (2.7.6)
35
+ erubi (1.10.0)
36
+ ethon (0.15.0)
37
+ ffi (>= 1.15.0)
38
+ ffi (1.15.5)
39
+ i18n (1.9.1)
40
+ concurrent-ruby (~> 1.0)
41
+ loofah (2.13.0)
42
+ crass (~> 1.0.2)
43
+ nokogiri (>= 1.5.9)
44
+ method_source (1.0.0)
45
+ minitest (5.15.0)
46
+ nokogiri (1.13.1-arm64-darwin)
47
+ racc (~> 1.4)
48
+ nokogiri (1.13.1-x86_64-darwin)
49
+ racc (~> 1.4)
50
+ nokogiri (1.13.1-x86_64-linux)
51
+ racc (~> 1.4)
52
+ oauth (0.5.8)
53
+ parallel (1.21.0)
54
+ parser (3.1.0.0)
55
+ ast (~> 2.4.1)
56
+ racc (1.6.0)
57
+ rack (2.2.3)
58
+ rack-test (1.1.0)
59
+ rack (>= 1.0, < 3)
60
+ rails-dom-testing (2.0.3)
61
+ activesupport (>= 4.2.0)
62
+ nokogiri (>= 1.6)
63
+ rails-html-sanitizer (1.4.2)
64
+ loofah (~> 2.3)
65
+ railties (7.0.1)
66
+ actionpack (= 7.0.1)
67
+ activesupport (= 7.0.1)
68
+ method_source
69
+ rake (>= 12.2)
70
+ thor (~> 1.0)
71
+ zeitwerk (~> 2.5)
72
+ rainbow (3.1.1)
73
+ rake (13.0.6)
74
+ regexp_parser (2.2.0)
75
+ rexml (3.2.5)
76
+ rubocop (1.25.1)
77
+ parallel (~> 1.10)
78
+ parser (>= 3.1.0.0)
79
+ rainbow (>= 2.2.2, < 4.0)
80
+ regexp_parser (>= 1.8, < 3.0)
81
+ rexml
82
+ rubocop-ast (>= 1.15.1, < 2.0)
83
+ ruby-progressbar (~> 1.7)
84
+ unicode-display_width (>= 1.4.0, < 3.0)
85
+ rubocop-ast (1.15.1)
86
+ parser (>= 3.0.1.1)
87
+ rubocop-minitest (0.17.1)
88
+ rubocop (>= 0.90, < 2.0)
89
+ rubocop-packaging (0.5.1)
90
+ rubocop (>= 0.89, < 2.0)
91
+ rubocop-performance (1.13.2)
92
+ rubocop (>= 1.7.0, < 2.0)
93
+ rubocop-ast (>= 0.4.0)
94
+ rubocop-rails (2.9.1)
95
+ activesupport (>= 4.2.0)
96
+ rack (>= 1.1)
97
+ rubocop (>= 0.90.0, < 2.0)
98
+ rubocop-rails_config (1.8.1)
99
+ railties (>= 5.0)
100
+ rubocop (>= 1.19)
101
+ rubocop-ast (>= 1.0.1)
102
+ rubocop-minitest (~> 0.15)
103
+ rubocop-packaging (~> 0.5)
104
+ rubocop-performance (~> 1.11)
105
+ rubocop-rails (~> 2.0)
106
+ ruby-progressbar (1.11.0)
107
+ thor (1.2.1)
108
+ typhoeus (1.4.0)
109
+ ethon (>= 0.9.0)
110
+ tzinfo (2.0.4)
111
+ concurrent-ruby (~> 1.0)
112
+ unicode-display_width (2.1.0)
113
+ zeitwerk (2.5.4)
114
+
115
+ PLATFORMS
116
+ arm64-darwin-21
117
+ arm64-darwin-22
118
+ x86_64-darwin-20
119
+ x86_64-linux
120
+
121
+ DEPENDENCIES
122
+ birdsong!
123
+ byebug
124
+ dotenv
125
+ minitest
126
+ rake
127
+ rubocop
128
+ rubocop-performance
129
+ rubocop-rails
130
+ rubocop-rails_config
131
+
132
+ BUNDLED WITH
133
+ 2.3.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Christopher Guess
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Birdsong
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/birdsong`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'birdsong'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install birdsong
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/birdsong. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/birdsong/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Birdsong project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/birdsong/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "birdsong"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/birdsong.gemspec ADDED
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/birdsong/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "birdsong"
7
+ spec.version = Birdsong::VERSION
8
+ spec.authors = ["Christopher Guess"]
9
+ spec.email = ["cguess@gmail.com"]
10
+
11
+ spec.summary = "A gem to interface with Twitter's API V2"
12
+ # spec.description = "TODO: Write a longer description or delete this line."
13
+ # spec.homepage = "TODO: Put your gem's website or public repo URL here."
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
18
+
19
+ # spec.metadata["homepage_uri"] = spec.homepage
20
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
21
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # Prod dependencies
33
+ spec.add_dependency "typhoeus", "~> 1.4.0"
34
+ spec.add_dependency "oauth", "~> 0.5.6"
35
+
36
+ # Dev dependencies
37
+ spec.add_development_dependency "byebug"
38
+ spec.add_development_dependency "rake"
39
+ spec.add_development_dependency "minitest"
40
+ spec.add_development_dependency "rubocop"
41
+ spec.add_development_dependency "rubocop-rails"
42
+ spec.add_development_dependency "rubocop-rails_config"
43
+ spec.add_development_dependency "rubocop-performance"
44
+ spec.add_development_dependency "dotenv"
45
+
46
+ # For more information and examples about making a new gem, checkout our
47
+ # guide at: https://bundler.io/guides/creating_gem.html
48
+ end
@@ -0,0 +1,226 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Birdsong
4
+ class Tweet
5
+ def self.lookup(ids = [])
6
+ # If a single id is passed in we make it the appropriate array
7
+ ids = [ids] unless ids.kind_of?(Array)
8
+
9
+ # Check that the ids are at least real ids
10
+ ids.each { |id| raise Birdsong::InvalidIdError if !/\A\d+\z/.match(id) }
11
+
12
+ response = self.retrieve_data_v2(ids)
13
+ raise Birdsong::AuthorizationError, "Invalid response code #{response.code}" unless response.code == 200
14
+
15
+ json_response = JSON.parse(response.body)
16
+ check_for_errors(json_response)
17
+
18
+ return [] if json_response["data"].nil?
19
+
20
+ json_response["data"].map do |json_tweet|
21
+ Tweet.new(json_tweet, json_response["includes"])
22
+ end
23
+ end
24
+
25
+ # Attributes for after the response is parsed from Twitter
26
+ attr_reader :json
27
+ attr_reader :id
28
+ attr_reader :created_at
29
+ attr_reader :text
30
+ attr_reader :language
31
+ attr_reader :author_id
32
+ attr_reader :author
33
+ attr_reader :image_file_names
34
+ attr_reader :video_file_names
35
+ attr_reader :video_file_type
36
+
37
+ alias_method :user, :author # Every other gem uses `user` so we can just alias it
38
+
39
+ private
40
+
41
+ def initialize(json_tweet, includes)
42
+ @json = json_tweet
43
+ parse(json_tweet, includes)
44
+ end
45
+
46
+ def parse(json_tweet, includes)
47
+ @id = json_tweet["id"]
48
+ @created_at = DateTime.parse(json_tweet["created_at"])
49
+ @text = json_tweet["text"]
50
+ @language = json_tweet["lang"]
51
+ @author_id = json_tweet["author_id"]
52
+
53
+ # A sanity check to make sure we have media in there correctly
54
+ if includes.has_key? "media"
55
+ media_items = includes["media"].filter do |media_item|
56
+ json_tweet["attachments"]["media_keys"].include? media_item["media_key"]
57
+ end
58
+ else
59
+ media_items = []
60
+ end
61
+
62
+ @image_file_names = media_items.filter_map do |media_item|
63
+ next unless media_item["type"] == "photo"
64
+ Birdsong.retrieve_media(media_item["url"])
65
+ end
66
+
67
+ @video_file_names = media_items.filter_map do |media_item|
68
+ next unless (media_item["type"] == "video") || (media_item["type"] == "animated_gif")
69
+
70
+ # If the media is video we need to fall back to V1 of the API since V2 doesn't support
71
+ # videos yet. This is dumb, but not a big deal.
72
+ media_url = get_media_url_from_extended_entities
73
+ media_preview_url = get_media_preview_url_from_extended_entities
74
+ @video_file_type = media_item["type"]
75
+
76
+ # We're returning an array because, in the case that someday more videos are available our
77
+ # implementations won't breaks
78
+ [{ url: Birdsong.retrieve_media(media_url), preview_url: Birdsong.retrieve_media(media_preview_url) }]
79
+ end
80
+
81
+ # Look up the author given the new id.
82
+ # NOTE: This doesn't *seem* like the right place for this, but I"m not sure where else
83
+ @author = User.lookup(@author_id).first
84
+ end
85
+
86
+ # Used to extract a GIF or video URL from the extended entities object in the Twiter API response
87
+ # Assumes (as is the case right now) that a Tweet cannot have more than one GIF/video
88
+ def get_media_url_from_extended_entities
89
+ response = Tweet.retrieve_data_v1(@id)
90
+ response = JSON.parse(response.body)
91
+ get_largest_variant_url(response["extended_entities"]["media"])
92
+ end
93
+
94
+ # Used to extract a GIF or video preview URL from the extended entities object in the Twiter API response
95
+ # Assumes (as is the case right now) that a Tweet cannot have more than one GIF/video
96
+ def get_media_preview_url_from_extended_entities
97
+ response = Tweet.retrieve_data_v1(@id)
98
+ response = JSON.parse(response.body)
99
+ response["extended_entities"]["media"].first["media_url_https"]
100
+ end
101
+
102
+ def get_largest_variant_url(media_items)
103
+ # The API response is pretty deeply nested, but this handles that structure
104
+ largest_bitrate_variant = nil
105
+ media_items.each do |media_item|
106
+ # The API returns multiple different resolutions usually. Since we only want to archive
107
+ # the largest we'll run through and find it
108
+ media_item["video_info"]["variants"].each do |variant|
109
+ # Usually there's constant bitrate variants, and sometimes, a .m3u playlist which is for
110
+ # streaming. We want to ignore that one here.
111
+ next unless variant&.keys.include?("bitrate")
112
+
113
+ if largest_bitrate_variant.nil? || largest_bitrate_variant["bitrate"] < variant["bitrate"]
114
+ largest_bitrate_variant = variant
115
+ end
116
+ end
117
+ end
118
+ largest_bitrate_variant["url"]
119
+ end
120
+
121
+ def self.retrieve_data_v2(ids)
122
+ bearer_token = Birdsong.twitter_bearer_token
123
+
124
+ tweet_lookup_url = "https://api.twitter.com/2/tweets"
125
+
126
+ # Specify the Tweet IDs that you want to lookup below (to 100 per request)
127
+ tweet_ids = ids.join(",")
128
+
129
+ # Add or remove optional parameters values from the params object below. Full list of parameters and their values can be found in the docs:
130
+ # https://developer.twitter.com/en/docs/twitter-api/tweets/lookup/api-reference
131
+ params = {
132
+ "ids": tweet_ids,
133
+ "expansions": "attachments.media_keys,author_id,referenced_tweets.id",
134
+ "tweet.fields": Birdsong.tweet_fields,
135
+ "user.fields": Birdsong.user_fields,
136
+ "media.fields": "duration_ms,height,media_key,preview_image_url,public_metrics,type,url,width",
137
+ "place.fields": "country_code",
138
+ "poll.fields": "options"
139
+ }
140
+
141
+ response = tweet_lookup_v2(tweet_lookup_url, bearer_token, params)
142
+ raise Birdsong::AuthorizationError, "Invalid response code #{response.code}" unless response.code === 200
143
+
144
+ response
145
+ end
146
+
147
+ def self.tweet_lookup_v2(url, bearer_token, params)
148
+ options = {
149
+ method: "get",
150
+ headers: {
151
+ "User-Agent": "v2TweetLookupRuby",
152
+ "Authorization": "Bearer #{bearer_token}"
153
+ },
154
+ params: params
155
+ }
156
+
157
+ request = Typhoeus::Request.new(url, options)
158
+ response = request.run
159
+
160
+ raise Birdsong::RateLimitExceeded.new(
161
+ response.headers["x-rate-limit-limit"],
162
+ response.headers["x-rate-limit-remaining"],
163
+ response.headers["x-rate-limit-reset"]
164
+ ) if response.code === 429
165
+ raise Birdsong::AuthorizationError, "Invalid response code #{response.code}" unless response.code === 200
166
+
167
+ response
168
+ end
169
+
170
+ # Note that unlike the V2 this only supports one url at a time
171
+ def self.retrieve_data_v1(id)
172
+ bearer_token = Birdsong.twitter_bearer_token
173
+
174
+ tweet_lookup_url = "https://api.twitter.com/1.1/statuses/show.json?tweet_mode=extended&id=#{id}"
175
+
176
+ response = tweet_lookup_v1(tweet_lookup_url, bearer_token)
177
+ raise Birdsong::RateLimitExceeded.new(
178
+ response.headers["x-rate-limit-limit"],
179
+ response.headers["x-rate-limit-remaining"],
180
+ response.headers["x-rate-limit-reset"]
181
+ ) if response.code === 429
182
+ raise Birdsong::AuthorizationError, "Invalid response code #{response.code}" unless response.code === 200
183
+
184
+ response
185
+ end
186
+
187
+ # V2 of the Twitter API (which we use everywhere else) doesn't include videos or gifs yet,
188
+ # so we have to fall back to V1.
189
+ #
190
+ # There's a tracker for this at https://twittercommunity.com/t/where-would-i-find-the-direct-link-to-an-mp4-video-posted-in-v2/146933/2
191
+ def self.tweet_lookup_v1(url, bearer_token)
192
+ options = {
193
+ method: "get",
194
+ headers: {
195
+ "Authorization": "Bearer #{bearer_token}"
196
+ }
197
+ }
198
+
199
+ request = Typhoeus::Request.new(url, options)
200
+ response = request.run
201
+
202
+ raise Birdsong::RateLimitExceeded.new(
203
+ response.headers["x-rate-limit-limit"],
204
+ response.headers["x-rate-limit-remaining"],
205
+ response.headers["x-rate-limit-reset"]
206
+ ) if response.code === 429
207
+ raise Birdsong::AuthorizationError, "Invalid response code #{response.code}" unless response.code === 200
208
+
209
+ response
210
+ end
211
+
212
+
213
+ def self.check_for_errors(parsed_json)
214
+ return false unless parsed_json.key?("errors")
215
+ return false if parsed_json["errors"].empty?
216
+
217
+ parsed_json["errors"].each do |error|
218
+ # If the tweet is removed, or if the user is suspended you get an Authorization Error
219
+ if error["title"] == "Not Found Error" || error["title"] == "Authorization Error"
220
+ raise Birdsong::NoTweetFoundError, "Tweet with id #{error["value"]} not found"
221
+ end
222
+ end
223
+ false
224
+ end
225
+ end
226
+ end
@@ -0,0 +1,141 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Birdsong
4
+ class User
5
+ def self.lookup(ids = [])
6
+ # If a single id is passed in we make it the appropriate array
7
+ ids = [ids] unless ids.kind_of?(Array)
8
+
9
+ # Check that the ids are at least real ids
10
+ ids.each { |id| raise Birdsong::InvalidIdError if !/\A\d+\z/.match(id) }
11
+ self.lookup_primative(ids: ids)
12
+ end
13
+
14
+ def self.lookup_by_usernames(usernames = [])
15
+ # If a single id is passed in we make it the appropriate array
16
+ usernames = [usernames] unless usernames.kind_of?(Array)
17
+ self.lookup_primative(usernames: usernames)
18
+ end
19
+
20
+ # Attributes for after the response is parsed from Twitter
21
+ attr_reader :json
22
+ attr_reader :id
23
+ attr_reader :name
24
+ attr_reader :username
25
+ attr_reader :sign_up_date
26
+ attr_reader :location
27
+ attr_reader :profile_image_url
28
+ attr_reader :description
29
+ attr_reader :url
30
+ attr_reader :followers_count
31
+ attr_reader :following_count
32
+ attr_reader :tweet_count
33
+ attr_reader :listed_count
34
+ attr_reader :verified
35
+ attr_reader :created_at
36
+ attr_reader :profile_image_file_name
37
+
38
+ private
39
+
40
+ def initialize(json_user)
41
+ @json = json_user
42
+ parse(json_user)
43
+ end
44
+
45
+ def parse(json_user)
46
+ @id = json_user["id"]
47
+ @name = json_user["name"]
48
+ @username = json_user["username"]
49
+ @created_at = DateTime.parse(json_user["created_at"])
50
+ @location = json_user["location"]
51
+
52
+ # Removing the "normal" here gets us the full-sized image, instead of the 150x150 thumbnail
53
+ @profile_image_url = json_user["profile_image_url"].sub!("_normal", "")
54
+
55
+ @description = json_user["description"]
56
+ @url = json_user["url"]
57
+ @url = "https://www.twitter.com/#{@username}" if @url.nil?
58
+ @followers_count = json_user["public_metrics"]["followers_count"]
59
+ @following_count = json_user["public_metrics"]["following_count"]
60
+ @tweet_count = json_user["public_metrics"]["tweet_count"]
61
+ @listed_count = json_user["public_metrics"]["listed_count"]
62
+ @verified = json_user["verified"]
63
+ @profile_image_file_name = Birdsong.retrieve_media(@profile_image_url)
64
+ end
65
+
66
+ def self.lookup_primative(usernames: nil, ids: nil)
67
+ raise Birdsong::InvalidIdError if usernames.nil? && ids.nil? # can't pass in nothing
68
+ raise Birdsong::InvalidIdError if usernames.nil? == false && ids.nil? == false # don't pass in both
69
+
70
+ response = self.retrieve_data(ids: ids, usernames: usernames)
71
+
72
+ raise Birdsong::AuthorizationError, "Invalid response code #{response.code}" unless response.code == 200
73
+
74
+ json_response = JSON.parse(response.body)
75
+ return [] if json_response["data"].nil?
76
+
77
+ json_response["data"].map do |json_user|
78
+ User.new(json_user)
79
+ end
80
+ end
81
+
82
+ def self.retrieve_data(usernames: nil, ids: nil)
83
+ bearer_token = Birdsong.twitter_bearer_token
84
+
85
+ raise Birdsong::InvalidIdError if usernames.nil? && ids.nil? # can't pass in nothing
86
+ raise Birdsong::InvalidIdError if usernames.nil? == false && ids.nil? == false # don't pass in both
87
+
88
+ # Add or remove optional parameters values from the params object below. Full list of parameters and their values can be found in the docs:
89
+ # https://developer.twitter.com/en/docs/twitter-api/tweets/lookup/api-reference
90
+ params = {
91
+ "expansions": "pinned_tweet_id",
92
+ "tweet.fields": Birdsong.tweet_fields,
93
+ "user.fields": Birdsong.user_fields,
94
+ }
95
+
96
+ if usernames.nil? == false
97
+ user_lookup_url = "https://api.twitter.com/2/users/by"
98
+ # Specify the Usernames that you want to lookup below (to 100 per request)
99
+ params["usernames"] = usernames.join(",")
100
+ elsif ids.nil? == false
101
+ user_lookup_url = "https://api.twitter.com/2/users"
102
+ # Specify the User IDs that you want to lookup below (to 100 per request)
103
+ params["ids"] = ids.join(",")
104
+ end
105
+
106
+ response = self.user_lookup(user_lookup_url, bearer_token, params)
107
+
108
+ raise Birdsong::RateLimitExceeded.new(
109
+ response.headers["x-rate-limit-limit"],
110
+ response.headers["x-rate-limit-remaining"],
111
+ response.headers["x-rate-limit-reset"]
112
+ ) if response.code === 429
113
+ raise Birdsong::AuthorizationError, "Invalid response code #{response.code}" unless response.code == 200
114
+
115
+ response
116
+ end
117
+
118
+ def self.user_lookup(url, bearer_token, params)
119
+ options = {
120
+ method: "get",
121
+ headers: {
122
+ "User-Agent": "v2UserLookupRuby",
123
+ "Authorization": "Bearer #{bearer_token}"
124
+ },
125
+ params: params
126
+ }
127
+
128
+ request = Typhoeus::Request.new(url, options)
129
+ response = request.run
130
+
131
+ raise Birdsong::RateLimitExceeded.new(
132
+ response.headers["x-rate-limit-limit"],
133
+ response.headers["x-rate-limit-remaining"],
134
+ response.headers["x-rate-limit-reset"]
135
+ ) if response.code === 429
136
+ raise Birdsong::AuthorizationError, "Invalid response code #{response.code}" unless response.code == 200
137
+
138
+ response
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Birdsong
4
+ VERSION = "0.1.1"
5
+ end
data/lib/birdsong.rb ADDED
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "typhoeus"
5
+ require "date"
6
+ require "securerandom"
7
+ require "helpers/configuration"
8
+ require "fileutils"
9
+
10
+ require_relative "birdsong/version"
11
+ require_relative "birdsong/tweet"
12
+ require_relative "birdsong/user"
13
+
14
+ module Birdsong
15
+ extend Configuration
16
+
17
+ class Error < StandardError; end
18
+ class AuthorizationError < Error; end
19
+ class InvalidIdError < Error; end
20
+ class InvalidMediaTypeError < Error; end
21
+ class NoTweetFoundError < Error; end
22
+ class RateLimitExceeded < Error
23
+ attr_reader :rate_limit
24
+ attr_reader :rate_remaining
25
+ attr_reader :reset_time_left
26
+
27
+ def initialize(rate_limit, rate_remaining, reset_time)
28
+ @rate_limit = rate_limit
29
+ @rate_remaining = rate_remaining
30
+ @reset_time_left = reset_time.to_i - Time.now.to_i
31
+
32
+ super("Rate Limit Exceeded: Limit #{rate_limit}, Remaining #{rate_remaining}, Time Left #{reset_time_left}")
33
+ end
34
+ end
35
+
36
+ define_setting :temp_storage_location, "tmp/birdsong"
37
+ define_setting :twitter_bearer_token, ENV["TWITTER_BEARER_TOKEN"]
38
+ define_setting :save_media, true
39
+
40
+ # The general fields to always return for Users
41
+ def self.user_fields
42
+ "name,created_at,location,profile_image_url,protected,public_metrics,url,username,verified,withheld,description"
43
+ end
44
+
45
+ # The general fields to always return for Tweets
46
+ def self.tweet_fields
47
+ "attachments,author_id,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang"
48
+ end
49
+
50
+ # Get media from a URL and save to a temp folder set in the configuration under
51
+ # temp_storage_location
52
+ def self.retrieve_media(url)
53
+ return "" if !Birdsong.save_media
54
+
55
+ response = Typhoeus.get(url)
56
+
57
+ # Get the file extension if it's in the file
58
+ extension = url.split(".").last
59
+
60
+ # Do some basic checks so we just empty out if there's something weird in the file extension
61
+ # that could do some harm.
62
+ if extension.length.positive?
63
+ extension = extension[0...extension.index("?")]
64
+ extension = nil unless /^[a-zA-Z0-9]+$/.match?(extension)
65
+ extension = ".#{extension}" unless extension.nil?
66
+ end
67
+
68
+ temp_file_name = "#{Birdsong.temp_storage_location}/#{SecureRandom.uuid}#{extension}"
69
+
70
+ # We do this in case the folder isn't created yet, since it's a temp folder we'll just do so
71
+ self.create_temp_storage_location
72
+ File.binwrite(temp_file_name, response.body)
73
+ temp_file_name
74
+ end
75
+
76
+ private
77
+
78
+ def self.create_temp_storage_location
79
+ return if File.exist?(Birdsong.temp_storage_location) && File.directory?(Birdsong.temp_storage_location)
80
+ FileUtils.mkdir_p Birdsong.temp_storage_location
81
+ end
82
+ end
@@ -0,0 +1,3 @@
1
+ Birdsong.configuration do |config|
2
+ config.temp_storage_location = "tmp/birdsong"
3
+ end
@@ -0,0 +1,6 @@
1
+ class BirdsongGenerator < Rails::Generators::Base
2
+ source_root(File.expand_path(File.dirname(__FILE__)))
3
+ def copy_initializer
4
+ copy_file "birdsong.rb", "config/initializers/birdsong.rb"
5
+ end
6
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Borrowed with thanks from https://www.viget.com/articles/easy-gem-configuration-variables-with-defaults/
4
+ module Configuration
5
+ def configuration
6
+ yield self
7
+ end
8
+
9
+ def define_setting(name, default = nil)
10
+ class_variable_set("@@#{name}", default)
11
+
12
+ define_class_method "#{name}=" do |value|
13
+ class_variable_set("@@#{name}", value)
14
+ end
15
+
16
+ define_class_method name do
17
+ class_variable_get("@@#{name}")
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def define_class_method(name, &block)
24
+ (class << self; self; end).instance_eval do
25
+ define_method name, &block
26
+ end
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,204 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: birdsong
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Guess
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-05-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: typhoeus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.4.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.4.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: oauth
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.6
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.5.6
41
+ - !ruby/object:Gem::Dependency
42
+ name: byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-rails_config
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop-performance
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: dotenv
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description:
154
+ email:
155
+ - cguess@gmail.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".github/workflows/main.yml"
161
+ - ".gitignore"
162
+ - ".rubocop.yml"
163
+ - ".ruby-version"
164
+ - CHANGELOG.md
165
+ - CODE_OF_CONDUCT.md
166
+ - Gemfile
167
+ - Gemfile.lock
168
+ - LICENSE.txt
169
+ - README.md
170
+ - Rakefile
171
+ - bin/console
172
+ - bin/setup
173
+ - birdsong.gemspec
174
+ - lib/birdsong.rb
175
+ - lib/birdsong/tweet.rb
176
+ - lib/birdsong/user.rb
177
+ - lib/birdsong/version.rb
178
+ - lib/generators/birdsong.rb
179
+ - lib/generators/birdsong_generator.rb
180
+ - lib/helpers/configuration.rb
181
+ homepage:
182
+ licenses:
183
+ - MIT
184
+ metadata: {}
185
+ post_install_message:
186
+ rdoc_options: []
187
+ require_paths:
188
+ - lib
189
+ required_ruby_version: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ version: 2.7.0
194
+ required_rubygems_version: !ruby/object:Gem::Requirement
195
+ requirements:
196
+ - - ">="
197
+ - !ruby/object:Gem::Version
198
+ version: '0'
199
+ requirements: []
200
+ rubygems_version: 3.3.26
201
+ signing_key:
202
+ specification_version: 4
203
+ summary: A gem to interface with Twitter's API V2
204
+ test_files: []