lospec 0.1.0

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: 0dd31664ed14d707489f86eacf10037a7eb0875af1758100fcfea5673ed2ee9b
4
+ data.tar.gz: a669cffe86b57fdae1a883cebce8a80e36c2535f74a6a2d19692e3f012c9c450
5
+ SHA512:
6
+ metadata.gz: 29657aed1c51c66c77a4d63da0acad77451d1e16c9e85be69919d86545b29ad6f5ceb67d9ec34631a80678e4a360339146b2ec3799f826c10a7f7e89f3e9092b
7
+ data.tar.gz: d93a79509fb015c567da995ff35e6b1fd6246e015bfb40cf0fcc7c98e30d8f5e7e9248c667604efdf5607f6ac0bc70a19d3e9933c0c4de33955cfed259f7413a
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,24 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ NewCops: enable
7
+ TargetRubyVersion: 3.2
8
+
9
+ Layout/LineLength:
10
+ Max: 120
11
+
12
+ Style/Documentation:
13
+ Enabled: false
14
+
15
+ Style/SignalException:
16
+ EnforcedStyle: semantic
17
+
18
+ Style/StringLiterals:
19
+ Enabled: true
20
+ EnforcedStyle: double_quotes
21
+
22
+ Style/StringLiteralsInInterpolation:
23
+ Enabled: true
24
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-03-02
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 John DeSilva
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,63 @@
1
+ # Lospec
2
+
3
+ ## Installation
4
+
5
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ ## Usage
16
+
17
+ Use `Lospec::Palette.search` to search for palettes. For example, to find a palette with four colors:
18
+ ```ruby
19
+ palette = Lospec::Palette.search(colors: 4).first
20
+
21
+ palette.slug # => "tropical-fruit-04"
22
+ palette.colors # => ["f5b413", "9c1904", "dd0956", "250442"]
23
+ ```
24
+
25
+ You can also sort by most downloads first, and specify an endless range for colors. The available sort options are `%i[default alphabetical downloads newest]`.
26
+ ```ruby
27
+ palette = Lospec::Palette.search(colors: ..3, tag: 'black', sort: :downloads).first
28
+
29
+ palette.title # => "1bit Monitor Glow"
30
+ palette.colors # => ["222323", "f0f6f0"]
31
+ ```
32
+
33
+ The search method returns a lazy enumerable, so you can filter and limit. For example, to find up to three popular palettes with four or more colors containing pure white:
34
+ ```ruby
35
+ palettes = Lospec::Palette
36
+ .search(colors: 4.., sort: :downloads)
37
+ .filter { |palette| palette.colors.include?("ffffff") }
38
+ .take(3)
39
+ .to_a
40
+
41
+ palettes.map(&:slug) # => ["2-bit-grayscale", "arq4", "cga-palette-1-high"]
42
+ ```
43
+
44
+ If you know the slug of a specific palette, you can fetch it directly:
45
+ ```ruby
46
+ palette = Lospec::Palette.fetch("nintendo-gameboy-bgb")
47
+
48
+ palette.description # => "The default palette used by the bgb emulator..."
49
+ ```
50
+
51
+ ## Development
52
+
53
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
54
+
55
+ 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).
56
+
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Aesthetikx/lospec.
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/Steepfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ target :lib do
4
+ signature "sig"
5
+
6
+ check "lib"
7
+
8
+ library "json"
9
+ library "open-uri"
10
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "open-uri/cached"
5
+
6
+ module Lospec
7
+ class Palette
8
+ class Search
9
+ SORT_TYPES = %i[default alphabetical downloads newest].freeze
10
+
11
+ attr_reader :colors, :tag, :sort
12
+
13
+ def initialize(colors: nil, tag: "", sort: :default)
14
+ fail ArgumentError, "Sort should be one of #{SORT_TYPES}" unless SORT_TYPES.include?(sort.to_sym)
15
+
16
+ @colors = colors
17
+ @tag = tag
18
+ @sort = sort
19
+ end
20
+
21
+ def results(&)
22
+ return to_enum(:results).lazy unless block_given?
23
+
24
+ (0..).lazy.each do |page| # : Numeric
25
+ results = results_for_page(page:)
26
+
27
+ results.each(&)
28
+
29
+ break if results.empty?
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def results_for_page(page:)
36
+ url = url(page:)
37
+
38
+ json = url.open.read
39
+
40
+ data = JSON.parse(json)
41
+
42
+ data["palettes"].map(&method(:parse))
43
+ end
44
+
45
+ def url(page: 0)
46
+ host = "lospec.com"
47
+
48
+ path = "/palette-list/load"
49
+
50
+ params = {
51
+ **colors_params,
52
+ page:,
53
+ tag:,
54
+ sortingType: sort
55
+ }
56
+
57
+ query = URI.encode_www_form params
58
+
59
+ URI::HTTPS.build(host:, path:, query:)
60
+ end
61
+
62
+ def colors_params # rubocop:disable Metrics
63
+ if colors.nil?
64
+ { colorNumberFilterType: "any" }
65
+ elsif colors.is_a?(Range) && colors.begin && colors.end
66
+ fail ArgumentError, "Range must be one sided"
67
+ elsif colors.is_a?(Range) && colors.begin.nil?
68
+ { colorNumberFilterType: "max", colorNumber: colors.end }
69
+ elsif colors.is_a?(Range) && colors.end.nil?
70
+ { colorNumberFilterType: "min", colorNumber: colors.begin }
71
+ elsif colors.is_a?(Numeric)
72
+ { colorNumberFilterType: "exact", colorNumber: colors }
73
+ else
74
+ fail ArgumentError, "Colors must be a number or one sided range"
75
+ end
76
+ end
77
+
78
+ def parse(json) # rubocop:disable Metrics
79
+ number = ->(n) { n.to_s.gsub(/[^0-9]/, "").to_i }
80
+
81
+ timestamp = Time.method(:parse)
82
+
83
+ Lospec::Palette.new(
84
+ id: json["_id"],
85
+ slug: json["slug"],
86
+ title: json["title"],
87
+ description: json["description"],
88
+ colors: json["colors"],
89
+ tags: json["tags"],
90
+ hashtag: json["hashtag"],
91
+ published_at: timestamp[json["publishedAt"]],
92
+ created_at: timestamp[json["createdAt"]],
93
+ downloads: number[json["downloads"]],
94
+ likes: number[json["likes"]],
95
+ comments: number[json["comments"]]
96
+ )
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lospec
4
+ class PaletteNotFound < Error
5
+ def initialize(slug)
6
+ super("Palette not found: #{slug}")
7
+ end
8
+ end
9
+
10
+ palette_attributes = %i[
11
+ id
12
+ slug
13
+ title
14
+ description
15
+ colors
16
+ tags
17
+ hashtag
18
+ published_at
19
+ created_at
20
+ downloads
21
+ likes
22
+ comments
23
+ ]
24
+
25
+ Palette = Data.define(*palette_attributes) do
26
+ def self.fetch(slug)
27
+ search = Lospec::Palette::Search.new(sort: :downloads)
28
+
29
+ result = search.results.detect { |palette| palette.slug == slug }
30
+
31
+ result or fail PaletteNotFound, slug
32
+ end
33
+
34
+ def self.search(...)
35
+ Lospec::Palette::Search.new(...).results
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lospec
4
+ VERSION = "0.1.0"
5
+ end
data/lib/lospec.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lospec
4
+ class Error < StandardError; end
5
+ end
6
+
7
+ require_relative "lospec/version"
8
+ require_relative "lospec/palette"
9
+ require_relative "lospec/palette/search"
data/lospec.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/lospec/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "lospec"
7
+ spec.version = Lospec::VERSION
8
+ spec.authors = ["John DeSilva"]
9
+ spec.email = ["john@aesthetikx.info"]
10
+
11
+ spec.summary = "A simple gem to fetch palettes from Lospec.com"
12
+ spec.homepage = "https://github.com/Aesthetikx/lospec"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 3.2.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
19
+ spec.metadata["rubygems_mfa_required"] = "true"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(__dir__) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (File.expand_path(f) == __FILE__) ||
26
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git appveyor Gemfile])
27
+ end
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ # Uncomment to register a new dependency of your gem
34
+ spec.add_dependency "open-uri-cached", "~> 1"
35
+
36
+ # For more information and examples about making a new gem, check out our
37
+ # guide at: https://bundler.io/guides/creating_gem.html
38
+ end
data/sig/lospec.rbs ADDED
@@ -0,0 +1,64 @@
1
+ module Lospec
2
+ VERSION: String
3
+
4
+ class Error < StandardError
5
+ end
6
+
7
+ class PaletteNotFound < Error
8
+ def initialize: (String) -> void
9
+ end
10
+
11
+ class Palette
12
+ attr_reader id: String
13
+ attr_reader slug: String
14
+ attr_reader title: String
15
+ attr_reader description: String
16
+ attr_reader colors: Array[String]
17
+ attr_reader tags: Array[String]
18
+ attr_reader hashtag: String?
19
+ attr_reader published_at: Time
20
+ attr_reader created_at: Time
21
+ attr_reader downloads: Integer
22
+ attr_reader likes: Integer
23
+ attr_reader comments: Integer
24
+
25
+ def initialize: (
26
+ id: String,
27
+ slug: String,
28
+ title: String,
29
+ description: String,
30
+ colors: Array[String],
31
+ tags: Array[String],
32
+ hashtag: String?,
33
+ published_at: Time,
34
+ created_at: Time,
35
+ downloads: Integer,
36
+ likes: Integer,
37
+ comments: Integer,
38
+ ) -> void
39
+
40
+ class Search
41
+ SORT_TYPES: Array[Symbol]
42
+
43
+ attr_reader colors: (Numeric | Range[Numeric])?
44
+ attr_reader tag: String?
45
+ attr_reader sort: String | Symbol
46
+
47
+ def initialize: (
48
+ ?colors: Numeric | Range[Numeric],
49
+ ?tag: String,
50
+ ?sort: String | Symbol
51
+ ) -> void
52
+
53
+ def results: () -> Enumerator::Lazy[Palette, void]
54
+
55
+ private
56
+
57
+ def url: (page: Numeric) -> URI::HTTP
58
+
59
+ def colors_params: () -> Hash[Symbol, (String | Numeric)]
60
+
61
+ def results_for_page: (page: Numeric) -> Array[Palette]
62
+ end
63
+ end
64
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lospec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John DeSilva
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-03-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: open-uri-cached
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ description:
28
+ email:
29
+ - john@aesthetikx.info
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".rubocop.yml"
36
+ - CHANGELOG.md
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - Steepfile
41
+ - lib/lospec.rb
42
+ - lib/lospec/palette.rb
43
+ - lib/lospec/palette/search.rb
44
+ - lib/lospec/version.rb
45
+ - lospec.gemspec
46
+ - sig/lospec.rbs
47
+ homepage: https://github.com/Aesthetikx/lospec
48
+ licenses:
49
+ - MIT
50
+ metadata:
51
+ homepage_uri: https://github.com/Aesthetikx/lospec
52
+ source_code_uri: https://github.com/Aesthetikx/lospec
53
+ changelog_uri: https://github.com/Aesthetikx/lospec/blob/main/CHANGELOG.md
54
+ rubygems_mfa_required: 'true'
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 3.2.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.4.10
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: A simple gem to fetch palettes from Lospec.com
74
+ test_files: []