factorio-mod 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: adcb31d23a6438f33528e0690b06f8134759979b6d4fc7fbce2f9fb25f3cbc13
4
- data.tar.gz: 07df7cc9b6882fe8248f123b9f88896041099c6cc2d55964473a8d5aebf71b44
3
+ metadata.gz: 8cc46a6267996d8e4e5891e39918998a66ad25b738d85d44814426685662ea89
4
+ data.tar.gz: 83c21b87bdcd20990724ed98814c9bbde13280fba66a2d6d815dd5113bb10558
5
5
  SHA512:
6
- metadata.gz: 8039d46e1b26cc7241af59ad447ed76b10e9975e72875da517d1d24342dbebf9c866af9a9aaa287393892b1cc62b3a9b6b97531be2620934d97f7ac611482632
7
- data.tar.gz: a3860020ad29e8e513c02b30a1e77ce6829ffab221f35a7d142c0ded64bc15001fa252ee4ac9c8a7a0326d1992447ba2c2f67fb703661bf65542e3fea5b4640d
6
+ metadata.gz: 77486d9176c9398349bd7befbfab82ae12d02c3f77e7cdafa42933d4898be00f8b1596c4433e010149ab661de3ea63fbc5628352c7524b7cf7a199d0dd4acbe7
7
+ data.tar.gz: 5b1902aa35d4dd2a704e0ee50aeb0bab8c9c741aabf30a584c85e1d34cbe5cbc324b9152b1b0a2d66e97ab22fa168e618066d9e035b2d254464e63115f583314
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- factorio-mod (0.3.0)
4
+ factorio-mod (0.4.0)
5
5
  nokogiri (~> 1.8)
6
6
 
7
7
  GEM
@@ -10,15 +10,20 @@ GEM
10
10
  ast (2.4.0)
11
11
  bump (0.6.1)
12
12
  coco (0.15.0)
13
+ coderay (1.1.2)
13
14
  diff-lcs (1.3)
14
15
  jaro_winkler (1.5.1)
16
+ method_source (0.9.0)
15
17
  mini_portile2 (2.3.0)
16
- nokogiri (1.8.4)
18
+ nokogiri (1.8.5)
17
19
  mini_portile2 (~> 2.3.0)
18
20
  parallel (1.12.1)
19
21
  parser (2.5.1.2)
20
22
  ast (~> 2.4.0)
21
23
  powerpack (0.1.2)
24
+ pry (0.11.3)
25
+ coderay (~> 1.1.0)
26
+ method_source (~> 0.9.0)
22
27
  rainbow (3.0.0)
23
28
  rake (10.5.0)
24
29
  rspec (3.8.0)
@@ -53,9 +58,10 @@ DEPENDENCIES
53
58
  bundler (~> 1.16)
54
59
  coco
55
60
  factorio-mod!
61
+ pry
56
62
  rake (~> 10.0)
57
63
  rspec (~> 3.0)
58
64
  rubocop
59
65
 
60
66
  BUNDLED WITH
61
- 1.16.4
67
+ 1.16.6
data/Rakefile CHANGED
@@ -5,5 +5,3 @@ require 'bump/tasks'
5
5
  RSpec::Core::RakeTask.new(:spec)
6
6
 
7
7
  task default: :spec
8
-
9
- Bump.tag_by_default = true
@@ -7,8 +7,8 @@ require 'factorio/mod'
7
7
  # with your gem easier. You can also use a different console, if you like.
8
8
 
9
9
  # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
10
+ require 'pry'
11
+ Pry.start
12
12
 
13
- require 'irb'
14
- IRB.start(__FILE__)
13
+ # require 'irb'
14
+ # IRB.start(__FILE__)
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
27
27
 
28
28
  spec.add_development_dependency 'bump'
29
29
  spec.add_development_dependency 'coco'
30
+ spec.add_development_dependency 'pry'
30
31
  spec.add_development_dependency 'rubocop'
31
32
 
32
33
  spec.add_dependency 'nokogiri', '~> 1.8'
@@ -1,3 +1,5 @@
1
+ require 'bundler/setup'
2
+
1
3
  require 'factorio/mod/version'
2
4
 
3
5
  require 'factorio/mod/mod'
@@ -11,9 +11,18 @@ module Factorio
11
11
  'Downloads' => :times
12
12
  }.freeze
13
13
 
14
- # set value by String name
14
+ HANDLE_VALUE = {
15
+ version: :text.to_proc,
16
+ game_version: :text.to_proc,
17
+ uri: Mod.method(:get_href).to_proc,
18
+ date: :text.to_proc,
19
+ times: proc { |item| item.text.to_i }
20
+ }.freeze
21
+
22
+ # set value by table header
15
23
  def set(name, value)
16
- self[TABLE_HEADER_TO_SYM[name]] = value
24
+ sym = TABLE_HEADER_TO_SYM[name]
25
+ self[sym] = HANDLE_VALUE[sym].call value
17
26
  end
18
27
  end
19
28
  end
@@ -36,52 +36,61 @@ module Factorio
36
36
  table.xpath xpath
37
37
  end
38
38
 
39
- # @return [String, String] `csrf_token` and cookie
40
- def self.csrf_and_cookie
41
- resp = open 'https://mods.factorio.com/login'
42
- cookie = resp.meta['set-cookie'].split('; ')[0]
43
- csrf_token = Nokogiri::HTML(resp).css('#csrf_token @value').to_s
44
- [csrf_token, cookie]
45
- end
46
- private_class_method :csrf_and_cookie
47
-
48
39
  public
49
40
 
50
- # rubocop:disable MethodLength
51
-
52
41
  # Map each value with table header.
53
42
  # @param table_header [Array] table header in order
54
43
  # @param line [Nokogiri::HTML] `<tr>`
55
- # @return [Hash] table with the table header as key
44
+ # @return [Download] _download_
56
45
  def self.bind_td_to_th(table_header, line)
57
46
  line.css('td').each_with_index.each_with_object(Download.new) \
58
47
  do |(item, index), obj|
59
48
  key = table_header[index]
60
- if key == 'Download'
61
- href = item.xpath('a/@href').to_s
62
- raise NotLoginError if href =~ %r{^/login}
63
- obj.uri = MOD_URI + href
64
- else
65
- obj.set(key, item.text)
66
- end
49
+ obj.set(key, item)
67
50
  end
68
51
  end
69
52
 
70
- # rubocop:enable MethodLength
71
-
72
53
  # Get the logged in cookie.
73
- # @param username [String]
74
- # @param password [String]
54
+ #
55
+ # @example
56
+ # # use firefox nightly
57
+ # Selenium::WebDriver::Firefox::Binary.path =
58
+ # `which firefox-nightly`.chomp
59
+ # # open firefox
60
+ # driver = Selenium::WebDriver.for :firefox
61
+ # cookie = Factorio::Mod.login_cookie(driver)
62
+ # # the browser will automatically close
63
+ # @param driver [Selenium::WebDriver::Driver] Web Driver
64
+ # @param autoclose [Bool] automatically close the browser after logging in
75
65
  # @return [String] The logged in cookie
76
- def self.login_cookie(username, password)
77
- csrf_token, cookie = csrf_and_cookie
78
- data = URI.encode_www_form(
79
- 'csrf_token' => csrf_token,
80
- 'username' => username,
81
- 'password' => password
82
- )
83
- resp = Net::HTTP.post(URI(MOD_URI + '/login'), data, 'Cookie' => cookie)
84
- resp.response['set-cookie'].split('; ')[0]
66
+ def self.login_cookie(driver, autoclose = true)
67
+ begin
68
+ driver.navigate.to 'https://mods.factorio.com/login'
69
+ rescue Net::ReadTimeout
70
+ retry
71
+ end
72
+
73
+ sleep(1) while driver.current_url == 'https://mods.factorio.com/login'
74
+ session = driver.manage.cookie_named('session')[:value]
75
+ driver.quit if autoclose
76
+
77
+ "session=#{session}"
78
+ end
79
+
80
+ # Extend relative links to absolute links
81
+ # @param rel [String] relative link
82
+ # @return [String] absolute link
83
+ # @raise [NotLoginError]
84
+ def self.extend_uri(rel)
85
+ raise NotLoginError if rel =~ %r{^/login}
86
+ MOD_URI + rel
87
+ end
88
+
89
+ # Get href in Nokogiri node
90
+ # @param item [Nokogiri::HTML]
91
+ # @return [String] relative link
92
+ def self.get_href(item)
93
+ extend_uri(item.xpath('a/@href').text)
85
94
  end
86
95
 
87
96
  # Get the title (also known as _display name_) of the Mod
@@ -100,7 +109,7 @@ module Factorio
100
109
  # Get the GitHub URI.
101
110
  # @return [String] GitHub URI
102
111
  def github
103
- uri = data_table('Source').xpath('a/@href').to_s
112
+ uri = @mod.any.xpath('//a[@class="source-code-link"]/@href').text
104
113
  return if uri.empty?
105
114
  uri
106
115
  end
@@ -123,11 +132,18 @@ module Factorio
123
132
  def license_uri
124
133
  data_table('License').xpath('a/@href').to_s
125
134
  end
135
+ alias license_link license_uri
126
136
 
127
137
  # Get the author of the Mod.
128
138
  # @return [String] author
129
139
  def author
130
- data_table('Owner').xpath('a').text
140
+ @mod.any.xpath('//div[@class="mod-card-author"]/a').text
141
+ end
142
+
143
+ # Whether Mod is deprecated
144
+ # @return [Bool] is deprecated
145
+ def deprecated?
146
+ !@mod.any.xpath('//span[@class="mod-card-title-deprecated"]').empty?
131
147
  end
132
148
 
133
149
  # Get the totle number of downloads of Mod.
@@ -157,5 +173,12 @@ module Factorio
157
173
  Mod.bind_td_to_th head, line
158
174
  end
159
175
  end
176
+
177
+ # Get latest download uri by download button in card
178
+ # @return [String] download uri
179
+ def latest_download_uri
180
+ Mod.get_href(@mod.any.css('.mod-download-button'))
181
+ end
182
+ alias latest_download_link latest_download_uri
160
183
  end
161
184
  end
@@ -1,5 +1,5 @@
1
1
  module Factorio
2
2
  class Mod
3
- VERSION = '0.3.0'.freeze
3
+ VERSION = '0.4.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factorio-mod
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - 71e6fd52
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-09 00:00:00.000000000 Z
11
+ date: 2018-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
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'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rubocop
85
99
  requirement: !ruby/object:Gem::Requirement