style_stats 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dc28984cb3d689bfb0de102dee0c95fa83f1f92a
4
- data.tar.gz: 77a188a3fa1eed3af65804e3cd9206f7f6d8dcfb
3
+ metadata.gz: 7780214f096be1c28f03ea84b95e7d74bea2eedf
4
+ data.tar.gz: 47a4b0a89188167f8612ba5dce3840f5c6f67120
5
5
  SHA512:
6
- metadata.gz: 93516fa4b52889fba7473c9d5317fc9a71f8fe3219f0e2309dedd096efc63d7b79c612940b7394f4e4f93552f651eb92c561a0309c7c1447f6c0bbbd143e5e00
7
- data.tar.gz: 954008105e187676610831447f05bf720d7f589628745d5509922be787b45198af82cac33a5ad4bf34da71611771842bcab10c60c6ace32d8d84340836da7b51
6
+ metadata.gz: 283051e8c0e4bbfc685de1c807be32f3bbadb2a4d2a0152cad6932fd424de9e9633efc85953be091dcbbc72593a4ef336b1ec148bb83808e741c3279c50a59b6
7
+ data.tar.gz: 28adf7e15f5089a6ecc3ef7fd0924c68301d995c2f219ee5cf79f6801ab1c6d2747144561185d8645c3bb5f6c21c97867ba343c30d865e70df424ac6f6296f5e
@@ -18,6 +18,8 @@ opt.on('-V', '--version', 'output the version number') do |v|
18
18
  end
19
19
 
20
20
  opt.on('-f', '--format <format>', 'set the output format <json|html|md>') { |v| options[:format] = v }
21
+ opt.on('-m', '--mobile [name]', 'set the mobile user agent') { |v| options[:user_agent] = v || 'ios' }
22
+ opt.on('--user-anget <string>', 'set the user agent') { |v| options[:user_agent] = v }
21
23
 
22
24
  opt.parse!(ARGV)
23
25
 
@@ -16,12 +16,25 @@ class StyleStats
16
16
  end.flatten
17
17
 
18
18
  @css = files.inject(Css.new) do |css, file|
19
- css.merge!(Css.new(file))
19
+ css.merge!(Css.new(file, css_options))
20
20
  end
21
21
  end
22
22
 
23
23
  def render
24
- Template.new(@css, @options).render
24
+ Template.new(@css, template_options).render
25
+ end
26
+
27
+ private
28
+ def css_options
29
+ {
30
+ user_agent: @options[:user_agent]
31
+ }
32
+ end
33
+
34
+ def template_options
35
+ {
36
+ format: @options[:format]
37
+ }
25
38
  end
26
39
  end
27
40
 
@@ -1,14 +1,36 @@
1
1
  class StyleStats
2
2
  class CLI
3
- def self.run(files, options)
4
- stylestats = StyleStats.new(files, options)
5
- stylestats.render
6
- rescue StyleStats::RequestError
7
- puts '[ERROR] getaddrinfo ENOTFOUND'
8
- rescue StyleStats::ContentError
9
- puts '[ERROR] Content type is not HTML or CSS!'
10
- rescue StyleStats::InvalidError
11
- puts '[ERROR] Argument is invalid'
3
+ class << self
4
+ def run(files, option)
5
+ @options = option
6
+ stylestats = StyleStats.new(files, options)
7
+ stylestats.render
8
+ rescue StyleStats::RequestError
9
+ puts '[ERROR] getaddrinfo ENOTFOUND'
10
+ rescue StyleStats::ContentError
11
+ puts '[ERROR] Content type is not HTML or CSS!'
12
+ rescue StyleStats::InvalidError
13
+ puts '[ERROR] Argument is invalid'
14
+ end
15
+
16
+ private
17
+ def options
18
+ {
19
+ format: @options[:format],
20
+ user_agent: user_agent
21
+ }
22
+ end
23
+
24
+ def user_agent
25
+ case @options[:user_agent]
26
+ when 'ios'
27
+ 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.4 Version/8.0 Safari/600.1.4'
28
+ when 'android'
29
+ 'Mozilla/5.0 (Linux; Android 4.4.2; Nexus 5 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.99 Mobile Safari/537.36'
30
+ else
31
+ @options[:user_agent]
32
+ end
33
+ end
12
34
  end
13
35
  end
14
36
  end
@@ -8,7 +8,7 @@ class StyleStats
8
8
  class Css
9
9
  attr_accessor :path, :paths, :rules, :media_types, :selectors, :stylesheets, :elements
10
10
 
11
- def initialize(path = nil)
11
+ def initialize(path = nil, options = {})
12
12
  self.path = path
13
13
  self.paths = path ? [path] : []
14
14
  self.rules = []
@@ -17,6 +17,7 @@ class StyleStats
17
17
  self.stylesheets = []
18
18
  self.elements = []
19
19
 
20
+ @options = options
20
21
  parse if path
21
22
  end
22
23
 
@@ -95,7 +96,7 @@ class StyleStats
95
96
 
96
97
  private
97
98
  def parse
98
- fetch = Fetch.new(self.path)
99
+ fetch = Fetch.new(self.path, @options)
99
100
 
100
101
  self.stylesheets = fetch.stylesheets
101
102
  self.elements = fetch.elements
@@ -2,9 +2,10 @@ class StyleStats::Css
2
2
  class Fetch
3
3
  attr_accessor :stylesheets, :elements
4
4
 
5
- def initialize(path)
5
+ def initialize(path, options={})
6
6
  self.stylesheets = []
7
7
  self.elements = []
8
+ @options = options
8
9
  get(path)
9
10
  end
10
11
 
@@ -18,7 +19,7 @@ class StyleStats::Css
18
19
  end
19
20
 
20
21
  def request(path)
21
- file = open(path)
22
+ file = open(path, "User-Agent" => user_agent)
22
23
  case file.content_type
23
24
  when 'text/css'
24
25
  self.stylesheets.push(file.read)
@@ -29,7 +30,7 @@ class StyleStats::Css
29
30
  else
30
31
  raise StyleStats::ContentError.new
31
32
  end
32
- rescue SocketError
33
+ rescue SocketError, RuntimeError
33
34
  raise StyleStats::RequestError.new
34
35
  end
35
36
 
@@ -47,5 +48,9 @@ class StyleStats::Css
47
48
  uri.to_s
48
49
  end
49
50
  end
51
+
52
+ def user_agent
53
+ @options[:user_agent] || "Ruby/StyleStats #{StyleStats::VERSION}"
54
+ end
50
55
  end
51
56
  end
@@ -11,7 +11,7 @@ class StyleStats
11
11
  end
12
12
 
13
13
  def render
14
- case @options[:format].to_sym
14
+ case @options[:format].to_s.to_sym
15
15
  when :md, :html
16
16
  text = File.read("#{File.dirname(__FILE__)}/templates/#{@options[:format]}.erb")
17
17
  ERB.new(text, nil, '-').run(binding)
@@ -1,3 +1,3 @@
1
1
  class StyleStats
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: style_stats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - shiro16
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-08-29 00:00:00.000000000 Z
11
+ date: 2015-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oga