lgtm 0.4.0 → 0.5.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
  SHA1:
3
- metadata.gz: 58d58c1e75a0bf166829f769000c82541830873d
4
- data.tar.gz: 09623157b73c40cdbdf24485cdceeba115cd9470
3
+ metadata.gz: 8ceb211c29e4f2f4cbc9137f9ab452f143e9a0ef
4
+ data.tar.gz: 1c3a9666fc68d7c3dfee08fd4d2623579e14c346
5
5
  SHA512:
6
- metadata.gz: 613967c95a80b9b1a1c6b31291d93fe6081d07a06b764258dc7aa2e35b92db25088dcec6ccb10793e9eb633aa23836c82410d0f332e557de647951c3c46ce3ea
7
- data.tar.gz: 0797524047fae01500c43a21de80ab484ef2fb2343851bbce7304221b3d1c8cad017e755845cb4cfabcede599c242aede7b0df9fa8491db09ac823d4d360460f
6
+ metadata.gz: ccff47d835f76dbe2629b58bea948124757b4c524e537ec5accb010fe380d47997957126a8b2518409c5200dff8bc1d6b9751e25ae0e1d4d265680dbdd2fc682
7
+ data.tar.gz: 47745e86a76bf814659f0abb209c4b7489fe6405ced8a333c07805ae1b91c67c00d3d1db9dcdb3bef1f4f5c54670a0f72893abbeac81978bfeba33cd4ce37a09
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "lgtm"
3
+ require 'bundler/setup'
4
+ require 'lgtm'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "lgtm"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start
data/exe/lgtm CHANGED
@@ -1,6 +1,29 @@
1
1
  #! /usr/bin/env ruby
2
2
 
3
- require 'lgtm'
3
+ require 'thor'
4
+ require_relative '../lib/lgtm'
4
5
 
5
- print Lgtm::In.new.fetch_lgtm_link_markdown!
6
+ class Cli < Thor
7
+ desc 'link NUMBER', 'get lgtm markdown format. you can set pick index default or 0 by random'
8
+ def link(picker = :random)
9
+ print Lgtm::In.new.fetch_lgtm_link_markdown!(:link, picker)
10
+ end
6
11
 
12
+ desc 'image NUMBER', 'get image url format. you can set pick index default or 0 by random'
13
+ def image(picker = :random)
14
+ print Lgtm::In.new.fetch_lgtm_link_markdown!(:image, picker)
15
+ end
16
+
17
+ desc 'double', 'output of stdout to link, stderr to image url'
18
+ def double(picker = :random)
19
+ STDOUT.print Lgtm::In.new.fetch_lgtm_link_markdown!(:link, picker)
20
+ STDERR.print Lgtm::In.new.fetch_lgtm_link_markdown!(:image, picker)
21
+ end
22
+
23
+ map 'l' => 'link'
24
+ map 'i' => 'image'
25
+ map 'img' => 'image'
26
+ map 'd' => 'double'
27
+ end
28
+
29
+ Cli.start(ARGV)
@@ -4,24 +4,25 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'lgtm/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "lgtm"
7
+ spec.name = 'lgtm'
8
8
  spec.version = Lgtm::VERSION
9
- spec.authors = ["HaiTo"]
10
- spec.email = ["kimura@sansan.com"]
9
+ spec.authors = ['HaiTo']
10
+ spec.email = ['kimura@sansan.com']
11
11
 
12
12
  spec.summary = 'fetch image url in lgtm.in'
13
13
  spec.description = 'fetch image url in lgtm.in to pbcopy'
14
14
  spec.homepage = 'https://github.com/haito/lgtm'
15
15
 
16
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
- spec.bindir = "exe"
18
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
- spec.require_paths = ["lib"]
16
+ spec.files = `git ls-files -z`.split("\x0").reject {|f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) {|f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.12"
22
- spec.add_development_dependency "rake", "~> 10.0"
23
- spec.add_development_dependency "rspec", "~> 3.0"
21
+ spec.add_development_dependency 'bundler', '~> 1.12'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
24
  spec.add_development_dependency 'pry'
25
25
  spec.add_development_dependency 'pry-byebug'
26
26
  spec.add_dependency 'mechanize'
27
+ spec.add_dependency 'thor'
27
28
  end
@@ -1,25 +1,31 @@
1
- require "lgtm/version"
1
+ require 'lgtm/version'
2
2
  require 'mechanize'
3
3
 
4
4
  module Lgtm
5
+ class Fixnum
6
+ MAX = 4_611_686_018_427_387_903
7
+ end
8
+
5
9
  class In
6
10
  LGTM_LINK_BASE = 'http://lgtm.in/i'.freeze
7
- NOT_SET_VALIAVLE_ALERT_1 = "please set enviromant variable LGTM_IN_USER_PATH \n"
8
- NOT_SET_VALIAVLE_ALERT_2 = "lgtm.in mypage example -> http://lgtm.in/l/HaiTo \n"
9
- NOT_SET_VALIAVLE_ALERT_3 = "in .bashrc, .zshrc -> export LGTM_IN_USER_PATH='http://lgtm.in/l/HaiTo' \n"
11
+ NOT_SET_VALIAVLE_ALERT_1 = "please set enviromant variable LGTM_IN_USER_PATH \n".freeze
12
+ NOT_SET_VALIAVLE_ALERT_2 = "lgtm.in mypage example -> http://lgtm.in/l/HaiTo \n".freeze
13
+ NOT_SET_VALIAVLE_ALERT_3 = "in .bashrc, .zshrc -> export LGTM_IN_USER_PATH='http://lgtm.in/l/HaiTo' \n".freeze
10
14
 
11
- REFLASH_TIME = 604_800.freeze
15
+ REFLASH_TIME = 604_800
12
16
 
13
17
  LAST_EXECUTED_AT_TIME_PATH = 'last_executed_at.time'.freeze
14
18
  CACHED_LINKS_PATH = 'cached_links.links'.freeze
15
- BASE_PATH = File.expand_path('..', __FILE__)
19
+ BASE_PATH = '/tmp'.freeze
16
20
 
17
- LINK_PAR_PAGE_BASE = 32.freeze
21
+ LINK_PAR_PAGE_BASE = 32
18
22
 
19
- LGTM_MARKDOWN = '[![LGTM](__LGTM_IMAGE_DETAILS_PATH__)](__LGTM_IMAGE_PATH__)'
23
+ LGTM_MARKDOWN = '[![LGTM](__LGTM_IMAGE_DETAILS_PATH__)](__LGTM_IMAGE_PATH__)'.freeze
20
24
  LGTM_MARKDOWN_DETAIL_PATH_BASE = 'http://lgtm.in/p/'.freeze
21
25
  LGTM_MARKDOWN_IMAGE_PATH_BASE = 'http://lgtm.in/i/'.freeze
22
-
26
+
27
+ LgtmLinkStruct = Struct.new(:index, :link, :image)
28
+
23
29
  def not_set_valiavle_alert
24
30
  print NOT_SET_VALIAVLE_ALERT_1
25
31
  print NOT_SET_VALIAVLE_ALERT_2
@@ -37,24 +43,40 @@ module Lgtm
37
43
  end
38
44
  end
39
45
 
40
- def fetch_lgtm_link_markdown!
41
- links =
46
+ def fetch_lgtm_link_markdown!(mode, picker)
47
+ link_structs =
42
48
  if Time.now.to_i - @last_executed_at.to_i > REFLASH_TIME
43
49
  update_executed_at!
44
50
 
45
- links = fetch_links
46
- cache_links!(links)
51
+ link_structs = fetch_links
52
+ cache_links!(link_structs)
47
53
 
48
- links
54
+ link_structs
49
55
  else
50
56
  @cached_links.nil? ? fetch_links : Marshal.load(@cached_links)
51
57
  end
52
58
 
53
- link = links.sample
59
+ link_struct =
60
+ case picker.to_i
61
+ when 1..Fixnum::MAX
62
+ link_structs.compact[picker.to_i]
63
+ else
64
+ link_structs.compact.sample
65
+ end
66
+
67
+ unless link_struct
68
+ print "you could set to NUMBER at between 0 and #{link_structs.size}"
69
+ exit(1)
70
+ end
54
71
 
55
- hash = link.split('/').last
72
+ case mode
73
+ when :link
74
+ link_hash = link_struct.link.split('/').last
56
75
 
57
- LGTM_MARKDOWN.gsub('__LGTM_IMAGE_DETAILS_PATH__', "#{LGTM_MARKDOWN_DETAIL_PATH_BASE + hash}").gsub('__LGTM_IMAGE_PATH__', "#{LGTM_MARKDOWN_IMAGE_PATH_BASE + hash}")
76
+ LGTM_MARKDOWN.gsub('__LGTM_IMAGE_DETAILS_PATH__', (LGTM_MARKDOWN_DETAIL_PATH_BASE + link_hash).to_s).gsub('__LGTM_IMAGE_PATH__', (LGTM_MARKDOWN_IMAGE_PATH_BASE + link_hash).to_s)
77
+ when :image
78
+ link_struct.image
79
+ end
58
80
  end
59
81
 
60
82
  private
@@ -69,7 +91,10 @@ module Lgtm
69
91
 
70
92
  def fetch_link_per_page!(cursor, links, agent)
71
93
  page = agent.get(@lgtm_my_page + "?page=#{cursor}")
72
- links << page.links.map(&:href).select {|link| link.match(LGTM_LINK_BASE) }
94
+
95
+ links << page.links.select {|link| link.href.match(LGTM_LINK_BASE) }.map.with_index(links.size + 1) {|link, index|
96
+ LgtmLinkStruct.new(index, link.href, link.node.children[1].attributes['src'].value)
97
+ }
73
98
 
74
99
  if links.flatten!.size % LINK_PAR_PAGE_BASE == 0
75
100
  fetch_link_per_page!(cursor + 1, links, agent)
@@ -85,5 +110,11 @@ module Lgtm
85
110
  def cache_links!(links)
86
111
  File.open(BASE_PATH + '/' + CACHED_LINKS_PATH, 'w') {|f| f.write(Marshal.dump(links)) }
87
112
  end
113
+
114
+ def load_cached_links!
115
+ Marshal.load(@cached_links)
116
+ rescue
117
+ fetch_links
118
+ end
88
119
  end
89
120
  end
@@ -1,3 +1,3 @@
1
1
  module Lgtm
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lgtm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - HaiTo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-07-16 00:00:00.000000000 Z
11
+ date: 2016-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: thor
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  description: fetch image url in lgtm.in to pbcopy
98
112
  email:
99
113
  - kimura@sansan.com
@@ -110,7 +124,6 @@ files:
110
124
  - README.md
111
125
  - Rakefile
112
126
  - bin/console
113
- - bin/lgtm
114
127
  - bin/setup
115
128
  - exe/lgtm
116
129
  - lgtm.gemspec
data/bin/lgtm DELETED
@@ -1,57 +0,0 @@
1
- #! /usr/bin/env ruby
2
-
3
- require 'mechanize'
4
-
5
- LGTM_LINK_BASE = 'http://lgtm.in/i'.freeze
6
- LGTM_HAITO_PAGE = ENV['LGTM_IN_USER_PATH']
7
-
8
- REFLASH_TIME = 604_800.freeze
9
-
10
- LAST_EXECUTED_AT_TIME_PATH = 'last_executed_at.time'.freeze
11
- CACHED_LINKS_PATH = 'cached_links.links'.freeze
12
- BASE_PATH = File.expand_path('..', __FILE__)
13
-
14
- LGTM_MARKDOWN = '[![LGTM](__LGTM_IMAGE_DETAILS_PATH__)](__LGTM_IMAGE_PATH__)'
15
- LGTM_MARKDOWN_DETAIL_PATH_BASE = 'http://lgtm.in/p/'.freeze
16
- LGTM_MARKDOWN_IMAGE_PATH_BASE = 'http://lgtm.in/i/'.freeze
17
-
18
- last_executed_at = begin File.read(BASE_PATH + '/' + LAST_EXECUTED_AT_TIME_PATH); rescue; nil;
19
- end
20
-
21
- cached_links = begin File.read(BASE_PATH + '/' + CACHED_LINKS_PATH); rescue; nil
22
- end
23
-
24
- def fetch_links
25
- agent = Mechanize.new
26
- page = agent.get(LGTM_HAITO_PAGE)
27
- page.links.map(&:href).select {|link| link.match(LGTM_LINK_BASE) }
28
- end
29
-
30
- def update_executed_at!
31
- File.open(BASE_PATH + '/' + LAST_EXECUTED_AT_TIME_PATH, 'w') {|f| f.write(Time.now.to_i) }
32
- end
33
-
34
- def cache_links!(links)
35
- File.open(BASE_PATH + '/' + CACHED_LINKS_PATH, 'w') {|f| f.write(Marshal.dump(links)) }
36
- end
37
-
38
- links =
39
- if Time.now.to_i - last_executed_at.to_i > REFLASH_TIME
40
- update_executed_at!
41
-
42
- links = fetch_links
43
- cache_links!(links)
44
-
45
- links
46
- else
47
- cached_links.nil? ? fetch_links : Marshal.load(cached_links)
48
- end
49
-
50
- link = links.sample
51
-
52
- hash = link.split('/').last
53
-
54
- lgtm_markdown = LGTM_MARKDOWN.gsub('__LGTM_IMAGE_DETAILS_PATH__', "#{LGTM_MARKDOWN_DETAIL_PATH_BASE + hash}").gsub('__LGTM_IMAGE_PATH__', "#{LGTM_MARKDOWN_IMAGE_PATH_BASE + hash}")
55
-
56
- system("echo '#{lgtm_markdown}' | pbcopy")
57
-