Class: Gembuild::AurScraper
- Inherits:
-
Object
- Object
- Gembuild::AurScraper
- Defined in:
- lib/gembuild/aur_scraper.rb
Overview
This class is used to query the AUR for information about a package.
Instance Attribute Summary collapse
-
#agent ⇒ Mechanize
readonly
The Mechanize agent.
-
#pkgname ⇒ String
readonly
The package about which to query the AUR.
-
#url ⇒ String
readonly
The AUR url for the package.
Instance Method Summary collapse
-
#get_version_hash(response) ⇒ Hash
Parse the version from the AUR response.
-
#initialize(pkgname) ⇒ Gembuild::AurScraper
constructor
Creates a new AurScraper instance.
-
#package_exists?(response) ⇒ Boolean
Determine whether the package already exists on the AUR by the number of results returned.
-
#query_aur ⇒ Hash
Query the AUR for information about a package and then parse the JSON results.
-
#scrape! ⇒ nil, Hash
Query the AUR and returned the parsed results.
Constructor Details
#initialize(pkgname) ⇒ Gembuild::AurScraper
Creates a new AurScraper instance.
58 59 60 61 62 63 64 65 |
# File 'lib/gembuild/aur_scraper.rb', line 58 def initialize(pkgname) fail Gembuild::UndefinedPkgnameError if pkgname.nil? || pkgname.empty? @agent = Mechanize.new @pkgname = pkgname @url = "https://aur.archlinux.org/rpc.php?type=info&arg=#{pkgname}" end |
Instance Attribute Details
#agent ⇒ Mechanize (readonly)
Returns the Mechanize agent.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/gembuild/aur_scraper.rb', line 30 class AurScraper attr_reader :agent, :pkgname, :url # Creates a new AurScraper instance. # # @raise [Gembuild::UndefinedPkgnameError] if the pkgname is nil or empty # # @example Create new AurScraper object # Gembuild::AurScraper.new('ruby-mina') # # => #<Gembuild::AurScraper:0x000000040659b0 # # @agent= # # #<Mechanize # # #<Mechanize::CookieJar:0x000000040658c0 # # @store= # # #<HTTP::CookieJar::HashStore:0x000000040813b8 # # @gc_index=0, # # @gc_threshold=150, # # @jar={}, # # @logger=nil, # # @mon_count=0, # # @mon_mutex=#<Mutex:0x00000004081368>, # # @mon_owner=nil>> # # nil>, # # @pkgname="ruby-mina", # # @url="https://aur.archlinux.org/rpc.php?type=info&arg=ruby-mina"> # # @param pkgname [String] The name of the package about which to query. # @return [Gembuild::AurScraper] a new AurScraper instance def initialize(pkgname) fail Gembuild::UndefinedPkgnameError if pkgname.nil? || pkgname.empty? @agent = Mechanize.new @pkgname = pkgname @url = "https://aur.archlinux.org/rpc.php?type=info&arg=#{pkgname}" end # Query the AUR for information about a package and then parse the JSON # results. # # @example Query the AUR about a package # s = Gembuild::AurScraper.new('ruby-mina') # r = s.query_aur # #=> {:version=>1, # # :type=>"info", # # :resultcount=>1, # # :results=> # # {:ID=>238062, # # :Name=>"ruby-mina", # # :PackageBaseID=>101492, # # :PackageBase=>"ruby-mina", # # :Version=>"0.3.7-1", # # :Description=>"Really fast deployer and server automation tool.", # # :URL=>"http://github.com/nadarei/mina", # # :NumVotes=>0, # # :OutOfDate=>nil, # # :Maintainer=>"supermario", # # :FirstSubmitted=>1444354070, # # :LastModified=>1444354135, # # :License=>"MIT", # # :URLPath=>"/cgit/aur.git/snapshot/ruby-mina.tar.gz", # # :CategoryID=>1}} # # @return [Hash] the information about the package def query_aur JSON.parse(agent.get(url).body, symbolize_names: true) end # Determine whether the package already exists on the AUR by the number of # results returned. # # @example Check if package exists on the AUR # s = Gembuild::AurScraper.new('ruby-mina') # r = s.query_aur # s.package_exists?(r) #=> true # # @param response [Hash] The JSON parsed response from the AUR. # @return [Boolean] whether or not the package exists already on the AUR def package_exists?(response) response[:results].count.zero? ? false : true end # Parse the version from the AUR response. # # A version string is expected to either look like 0.1.2-3 or like # 1:2.3.4-5. So the strategy is to first split on the dash to get the # package release number. Then with the remaining string attempt a split # on the colon. If there is only one part then it means that there is no # epoch (or rather that the epoch is zero). If there are two parts then we # use the first as the epoch value. Finally, whatever is left is the # actual version of the gem. # # @example Get package version from the AUR # s = Gembuild::AurScraper.new('ruby-mina') # r = s.query_aur # s.get_version_hash(r) # #=> {:epoch=>0, :pkgver=>Gem::Version.new("0.3.7"), :pkgrel=>1} # # @param response [Hash] The JSON parsed response from the AUR. # @return [Hash] a hash of the different version parts def get_version_hash(response) version = response[:results][:Version].split('-') pkgrel = version.pop.to_i version = version.join version = version.split(':') epoch = version.count == 1 ? 0 : version.shift.to_i version = Gem::Version.new(version.join) { epoch: epoch, pkgver: version, pkgrel: pkgrel } end # Query the AUR and returned the parsed results. # # @example Query the AUR for information about a package # s = Gembuild::AurScraper.new('ruby-mina') # s.scrape! # #=> {:epoch=>0, :pkgver=>Gem::Version.new("0.3.7"), :pkgrel=>1} # # @return [nil, Hash] the version hash or nil if the package doesn't exist def scrape! response = query_aur return nil unless package_exists?(response) get_version_hash(response) end end |
#pkgname ⇒ String (readonly)
Returns the package about which to query the AUR.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/gembuild/aur_scraper.rb', line 30 class AurScraper attr_reader :agent, :pkgname, :url # Creates a new AurScraper instance. # # @raise [Gembuild::UndefinedPkgnameError] if the pkgname is nil or empty # # @example Create new AurScraper object # Gembuild::AurScraper.new('ruby-mina') # # => #<Gembuild::AurScraper:0x000000040659b0 # # @agent= # # #<Mechanize # # #<Mechanize::CookieJar:0x000000040658c0 # # @store= # # #<HTTP::CookieJar::HashStore:0x000000040813b8 # # @gc_index=0, # # @gc_threshold=150, # # @jar={}, # # @logger=nil, # # @mon_count=0, # # @mon_mutex=#<Mutex:0x00000004081368>, # # @mon_owner=nil>> # # nil>, # # @pkgname="ruby-mina", # # @url="https://aur.archlinux.org/rpc.php?type=info&arg=ruby-mina"> # # @param pkgname [String] The name of the package about which to query. # @return [Gembuild::AurScraper] a new AurScraper instance def initialize(pkgname) fail Gembuild::UndefinedPkgnameError if pkgname.nil? || pkgname.empty? @agent = Mechanize.new @pkgname = pkgname @url = "https://aur.archlinux.org/rpc.php?type=info&arg=#{pkgname}" end # Query the AUR for information about a package and then parse the JSON # results. # # @example Query the AUR about a package # s = Gembuild::AurScraper.new('ruby-mina') # r = s.query_aur # #=> {:version=>1, # # :type=>"info", # # :resultcount=>1, # # :results=> # # {:ID=>238062, # # :Name=>"ruby-mina", # # :PackageBaseID=>101492, # # :PackageBase=>"ruby-mina", # # :Version=>"0.3.7-1", # # :Description=>"Really fast deployer and server automation tool.", # # :URL=>"http://github.com/nadarei/mina", # # :NumVotes=>0, # # :OutOfDate=>nil, # # :Maintainer=>"supermario", # # :FirstSubmitted=>1444354070, # # :LastModified=>1444354135, # # :License=>"MIT", # # :URLPath=>"/cgit/aur.git/snapshot/ruby-mina.tar.gz", # # :CategoryID=>1}} # # @return [Hash] the information about the package def query_aur JSON.parse(agent.get(url).body, symbolize_names: true) end # Determine whether the package already exists on the AUR by the number of # results returned. # # @example Check if package exists on the AUR # s = Gembuild::AurScraper.new('ruby-mina') # r = s.query_aur # s.package_exists?(r) #=> true # # @param response [Hash] The JSON parsed response from the AUR. # @return [Boolean] whether or not the package exists already on the AUR def package_exists?(response) response[:results].count.zero? ? false : true end # Parse the version from the AUR response. # # A version string is expected to either look like 0.1.2-3 or like # 1:2.3.4-5. So the strategy is to first split on the dash to get the # package release number. Then with the remaining string attempt a split # on the colon. If there is only one part then it means that there is no # epoch (or rather that the epoch is zero). If there are two parts then we # use the first as the epoch value. Finally, whatever is left is the # actual version of the gem. # # @example Get package version from the AUR # s = Gembuild::AurScraper.new('ruby-mina') # r = s.query_aur # s.get_version_hash(r) # #=> {:epoch=>0, :pkgver=>Gem::Version.new("0.3.7"), :pkgrel=>1} # # @param response [Hash] The JSON parsed response from the AUR. # @return [Hash] a hash of the different version parts def get_version_hash(response) version = response[:results][:Version].split('-') pkgrel = version.pop.to_i version = version.join version = version.split(':') epoch = version.count == 1 ? 0 : version.shift.to_i version = Gem::Version.new(version.join) { epoch: epoch, pkgver: version, pkgrel: pkgrel } end # Query the AUR and returned the parsed results. # # @example Query the AUR for information about a package # s = Gembuild::AurScraper.new('ruby-mina') # s.scrape! # #=> {:epoch=>0, :pkgver=>Gem::Version.new("0.3.7"), :pkgrel=>1} # # @return [nil, Hash] the version hash or nil if the package doesn't exist def scrape! response = query_aur return nil unless package_exists?(response) get_version_hash(response) end end |
#url ⇒ String (readonly)
Returns the AUR url for the package.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/gembuild/aur_scraper.rb', line 30 class AurScraper attr_reader :agent, :pkgname, :url # Creates a new AurScraper instance. # # @raise [Gembuild::UndefinedPkgnameError] if the pkgname is nil or empty # # @example Create new AurScraper object # Gembuild::AurScraper.new('ruby-mina') # # => #<Gembuild::AurScraper:0x000000040659b0 # # @agent= # # #<Mechanize # # #<Mechanize::CookieJar:0x000000040658c0 # # @store= # # #<HTTP::CookieJar::HashStore:0x000000040813b8 # # @gc_index=0, # # @gc_threshold=150, # # @jar={}, # # @logger=nil, # # @mon_count=0, # # @mon_mutex=#<Mutex:0x00000004081368>, # # @mon_owner=nil>> # # nil>, # # @pkgname="ruby-mina", # # @url="https://aur.archlinux.org/rpc.php?type=info&arg=ruby-mina"> # # @param pkgname [String] The name of the package about which to query. # @return [Gembuild::AurScraper] a new AurScraper instance def initialize(pkgname) fail Gembuild::UndefinedPkgnameError if pkgname.nil? || pkgname.empty? @agent = Mechanize.new @pkgname = pkgname @url = "https://aur.archlinux.org/rpc.php?type=info&arg=#{pkgname}" end # Query the AUR for information about a package and then parse the JSON # results. # # @example Query the AUR about a package # s = Gembuild::AurScraper.new('ruby-mina') # r = s.query_aur # #=> {:version=>1, # # :type=>"info", # # :resultcount=>1, # # :results=> # # {:ID=>238062, # # :Name=>"ruby-mina", # # :PackageBaseID=>101492, # # :PackageBase=>"ruby-mina", # # :Version=>"0.3.7-1", # # :Description=>"Really fast deployer and server automation tool.", # # :URL=>"http://github.com/nadarei/mina", # # :NumVotes=>0, # # :OutOfDate=>nil, # # :Maintainer=>"supermario", # # :FirstSubmitted=>1444354070, # # :LastModified=>1444354135, # # :License=>"MIT", # # :URLPath=>"/cgit/aur.git/snapshot/ruby-mina.tar.gz", # # :CategoryID=>1}} # # @return [Hash] the information about the package def query_aur JSON.parse(agent.get(url).body, symbolize_names: true) end # Determine whether the package already exists on the AUR by the number of # results returned. # # @example Check if package exists on the AUR # s = Gembuild::AurScraper.new('ruby-mina') # r = s.query_aur # s.package_exists?(r) #=> true # # @param response [Hash] The JSON parsed response from the AUR. # @return [Boolean] whether or not the package exists already on the AUR def package_exists?(response) response[:results].count.zero? ? false : true end # Parse the version from the AUR response. # # A version string is expected to either look like 0.1.2-3 or like # 1:2.3.4-5. So the strategy is to first split on the dash to get the # package release number. Then with the remaining string attempt a split # on the colon. If there is only one part then it means that there is no # epoch (or rather that the epoch is zero). If there are two parts then we # use the first as the epoch value. Finally, whatever is left is the # actual version of the gem. # # @example Get package version from the AUR # s = Gembuild::AurScraper.new('ruby-mina') # r = s.query_aur # s.get_version_hash(r) # #=> {:epoch=>0, :pkgver=>Gem::Version.new("0.3.7"), :pkgrel=>1} # # @param response [Hash] The JSON parsed response from the AUR. # @return [Hash] a hash of the different version parts def get_version_hash(response) version = response[:results][:Version].split('-') pkgrel = version.pop.to_i version = version.join version = version.split(':') epoch = version.count == 1 ? 0 : version.shift.to_i version = Gem::Version.new(version.join) { epoch: epoch, pkgver: version, pkgrel: pkgrel } end # Query the AUR and returned the parsed results. # # @example Query the AUR for information about a package # s = Gembuild::AurScraper.new('ruby-mina') # s.scrape! # #=> {:epoch=>0, :pkgver=>Gem::Version.new("0.3.7"), :pkgrel=>1} # # @return [nil, Hash] the version hash or nil if the package doesn't exist def scrape! response = query_aur return nil unless package_exists?(response) get_version_hash(response) end end |
Instance Method Details
#get_version_hash(response) ⇒ Hash
Parse the version from the AUR response.
A version string is expected to either look like 0.1.2-3 or like 1:2.3.4-5. So the strategy is to first split on the dash to get the package release number. Then with the remaining string attempt a split on the colon. If there is only one part then it means that there is no epoch (or rather that the epoch is zero). If there are two parts then we use the first as the epoch value. Finally, whatever is left is the actual version of the gem.
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/gembuild/aur_scraper.rb', line 130 def get_version_hash(response) version = response[:results][:Version].split('-') pkgrel = version.pop.to_i version = version.join version = version.split(':') epoch = version.count == 1 ? 0 : version.shift.to_i version = Gem::Version.new(version.join) { epoch: epoch, pkgver: version, pkgrel: pkgrel } end |
#package_exists?(response) ⇒ Boolean
Determine whether the package already exists on the AUR by the number of results returned.
108 109 110 |
# File 'lib/gembuild/aur_scraper.rb', line 108 def package_exists?(response) response[:results].count.zero? ? false : true end |
#query_aur ⇒ Hash
Query the AUR for information about a package and then parse the JSON results.
94 95 96 |
# File 'lib/gembuild/aur_scraper.rb', line 94 def query_aur JSON.parse(agent.get(url).body, symbolize_names: true) end |
#scrape! ⇒ nil, Hash
Query the AUR and returned the parsed results.
151 152 153 154 155 156 157 |
# File 'lib/gembuild/aur_scraper.rb', line 151 def scrape! response = query_aur return nil unless package_exists?(response) get_version_hash(response) end |