Skip to content

Commit 2dad020

Browse files
committed
Update test
1 parent 7f16527 commit 2dad020

File tree

15 files changed

+284
-179
lines changed

15 files changed

+284
-179
lines changed

.gitignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
# Bundler
2-
Gemfile.lock
32
/.bundle
43
/vendor/bundle
54

65
# OS Specific
76
.DS_Store
87
Thumbs.db
9-
10-
# dotenv
11-
.env

.rspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
--color
2-
--format progress
2+
--require spec_helper

Gemfile

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
source 'https://rubygems.org'
22

3-
group :development, :test do
4-
gem 'dotenv'
5-
gem 'rspec', require: false
6-
end
3+
gem 'rake', :require => false
4+
gem 'rspec', require: false
5+
gem 'webmock'

Gemfile.lock

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
addressable (2.3.6)
5+
crack (0.4.2)
6+
safe_yaml (~> 1.0.0)
7+
diff-lcs (1.2.5)
8+
rake (10.1.0)
9+
rspec (3.0.0)
10+
rspec-core (~> 3.0.0)
11+
rspec-expectations (~> 3.0.0)
12+
rspec-mocks (~> 3.0.0)
13+
rspec-core (3.0.3)
14+
rspec-support (~> 3.0.0)
15+
rspec-expectations (3.0.3)
16+
diff-lcs (>= 1.2.0, < 2.0)
17+
rspec-support (~> 3.0.0)
18+
rspec-mocks (3.0.3)
19+
rspec-support (~> 3.0.0)
20+
rspec-support (3.0.3)
21+
safe_yaml (1.0.3)
22+
webmock (1.18.0)
23+
addressable (>= 2.3.6)
24+
crack (>= 0.3.2)
25+
26+
PLATFORMS
27+
ruby
28+
29+
DEPENDENCIES
30+
rake
31+
rspec
32+
webmock

Qiita.alfredworkflow

1.24 MB
Binary file not shown.

Rakefile

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
1+
require 'rspec/core/rake_task'
2+
13
BUNDLE_ID = "co.randompaper.qiita.alfred"
24
PACKAGE_FILE = "info.plist"
35

4-
workflow_home = File.expand_path('~/Library/Application Support/Alfred 2/Alfred.alfredpreferences/workflows')
5-
dbx_workflow_path = File.expand_path('~/Dropbox/アプリ/Alfred.alfredpreferences/workflows')
6+
ALFRED_WORKFLOW_PATH = ENV['ALFRED_WORKFLOW_PATH'] || File.expand_path('~/Library/Application Support/Alfred 2/Alfred.alfredpreferences/workflows')
7+
8+
task :default => [:spec]
69

710
desc "Link to Alfred"
8-
task :link => [:configure] do
9-
ln_sf File.expand_path('../', __FILE__), File.join(workflow_home, BUNDLE_ID)
11+
task :link do
12+
ln_sf File.expand_path('../', __FILE__), File.join(ALFRED_WORKFLOW_PATH, BUNDLE_ID)
1013
end
1114

1215
desc "Unlink from Alfred"
13-
task :unlink => [:configure] do
14-
rm File.join(workflow_home, BUNDLE_ID)
16+
task :unlink do
17+
rm File.join(ALFRED_WORKFLOW_PATH, BUNDLE_ID)
1518
end
16-
17-
desc "Install to Alfred Sync folder on Dropbox"
18-
task :link_dropbox => [:configure] do
19-
ln_sf File.expand_path('../', __FILE__), File.join(dbx_workflow_path, BUNDLE_ID)
20-
end
21-
22-
desc "Unlink from Alfred Sync folder on Dropbox"
23-
task :unlink_dropbox => [:configure] do
24-
rm File.join(dbx_workflow_path, BUNDLE_ID)
25-
end

commands/stocks.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
begin
66
config = Qiita::Config.new
77
rescue Qiita::FileMissingError
8-
Qiita::Alfred.message "RUN: 'qiita setup' command first"
9-
exit
8+
config.save
109
end
1110

1211
unless config.token
@@ -31,4 +30,4 @@
3130
results << item
3231
end
3332

34-
puts Qiita::Alfred.to_alfred(results)
33+
puts Qiita::Alfred.to_alfred(results)

lib/qiita.rb

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
module Qiita
2-
VERSION = '1.0'
3-
BUNDLE_ID = 'co.randompaper.qiita.alfred'
1+
$LOAD_PATH.unshift(File.dirname(__FILE__)) unless $LOAD_PATH.include?(File.dirname(__FILE__))
42

5-
CONFIG_FILE = File.expand_path("~/Library/Application Support/Alfred 2/Workflow Data/#{BUNDLE_ID}/config.json")
6-
CACHE_DIR = File.expand_path("~/Library/Caches/com.runningwithcrayons.Alfred-2/Workflow Data/#{BUNDLE_ID}")
7-
8-
class FileMissingError < StandardError; end
9-
end
10-
11-
require_relative 'qiita/object.rb'
12-
require_relative 'qiita/config.rb'
13-
require_relative 'qiita/api.rb'
14-
require_relative 'qiita/alfred.rb'
3+
require 'qiita/config'
4+
require 'qiita/api'
5+
require 'qiita/alfred'
6+
require 'qiita/version'

lib/qiita/alfred.rb

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
require 'rexml/document'
22

3-
module Qiita::Alfred
4-
def self.to_alfred(array)
5-
raise "Argument must be an Array" unless array.kind_of? Array
6-
7-
doc = REXML::Document.new
8-
root = doc.add_element("items")
3+
module Qiita
4+
module Alfred
5+
def self.to_alfred(array)
6+
raise "Argument must be an Array" unless array.kind_of? Array
97

10-
array.each do |entry|
11-
item = root.add_element("item")
8+
doc = REXML::Document.new
9+
root = doc.add_element("items")
1210

13-
entry.each do |key, value|
14-
if [:uid, :arg, :valid, :autocomplete].include?(key)
15-
item.attributes[key.to_s] = value.to_s
16-
else
17-
element = item.add_element(key.to_s)
18-
element.text = value ? value.to_s : ""
11+
array.each do |entry|
12+
item = root.add_element("item")
13+
14+
entry.each do |key, value|
15+
if [:uid, :arg, :valid, :autocomplete].include?(key)
16+
item.attributes[key.to_s] = value.to_s
17+
else
18+
element = item.add_element(key.to_s)
19+
element.text = value ? value.to_s : ""
20+
end
1921
end
2022
end
21-
end
2223

23-
doc.to_s
24-
end
24+
doc.to_s
25+
end
2526

26-
def self.message(str)
27-
puts Qiita::Alfred.to_alfred([{ uid: 0, arg: nil, title: str, subtitle: nil, icon: 'icon.png'}])
27+
def self.message(str)
28+
puts Qiita::Alfred.to_alfred([{ uid: 0, arg: nil, title: str, subtitle: nil, icon: 'icon.png'}])
29+
end
2830
end
29-
end
31+
end

lib/qiita/api.rb

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,65 @@
22
require 'uri'
33
require 'json'
44

5-
module Qiita::API
6-
def self.auth(name: name, password: password)
7-
uri = URI('https://qiita.com/api/v1/auth')
8-
params = {url_name: name, password: password}
9-
fetch uri, params, method: :post
10-
end
5+
module Qiita
6+
class API
117

12-
def self.search(query, args={})
13-
uri = URI('https://qiita.com/api/v1/search')
14-
fetch uri, {q: query}.update(args)
15-
end
8+
ENDPOINT = "https://qiita.com/api/v1/"
169

17-
def self.fetch(uri, params={}, *args)
18-
defaults = { method: :get, limit: 10 }
19-
opt = args.last.kind_of?(Hash)? defaults.update(args.pop) : defaults
20-
raise ArgumentError, 'too many HTTP redirects' if opt[:limit] == 0
21-
22-
if opt[:method] == :get
23-
uri.query = URI.encode_www_form(params) if params
24-
request = Net::HTTP::Get.new(uri)
25-
else
26-
request = Net::HTTP::Post.new(uri)
27-
request.set_form_data(params)
10+
def self.auth(name: name, password: password)
11+
uri = URI.join(ENDPOINT, 'auth')
12+
params = {url_name: name, password: password}
13+
fetch uri, params, method: :post
2814
end
2915

30-
response = Net::HTTP.start(
31-
uri.host,
32-
uri.port,
33-
:use_ssl => (uri.scheme == 'https'),
34-
:verify_mode => OpenSSL::SSL::VERIFY_NONE
35-
) do |https|
36-
https.request(request)
16+
def self.search(query, args={})
17+
uri = URI.join(ENDPOINT, 'search')
18+
fetch uri, {q: query}.update(args)
3719
end
3820

39-
case response
40-
when Net::HTTPSuccess
21+
def self.fetch(uri, params={}, *args)
22+
defaults = { method: :get, limit: 10 }
23+
opt = args.last.kind_of?(Hash)? defaults.update(args.pop) : defaults
24+
raise ArgumentError, 'too many HTTP redirects' if opt[:limit] == 0
25+
26+
if opt[:method] == :get
27+
uri.query = URI.encode_www_form(params) if params
28+
request = Net::HTTP::Get.new(uri)
29+
else
30+
request = Net::HTTP::Post.new(uri)
31+
request.set_form_data(params)
32+
end
33+
34+
response = Net::HTTP.start(
35+
uri.host,
36+
uri.port,
37+
:use_ssl => (uri.scheme == 'https'),
38+
:verify_mode => OpenSSL::SSL::VERIFY_NONE
39+
) do |https|
40+
https.request(request)
41+
end
42+
43+
case response
44+
when Net::HTTPSuccess
4145

42-
if(response.class.body_permitted?)
43-
begin
44-
JSON(response.body)
45-
rescue JSON::ParserError
46+
if(response.class.body_permitted?)
47+
begin
48+
JSON(response.body)
49+
rescue JSON::ParserError
50+
false
51+
end
52+
else
4653
false
4754
end
48-
else
55+
56+
when Net::HTTPRedirection
57+
location = response['location']
58+
fetch(location, limit - 1)
59+
when Net::HTTPUnauthorized
4960
false
61+
else
62+
response.value
5063
end
51-
52-
when Net::HTTPRedirection
53-
location = response['location']
54-
fetch(location, limit - 1)
55-
when Net::HTTPUnauthorized
56-
false
57-
else
58-
response.value
5964
end
6065
end
61-
end
66+
end

0 commit comments

Comments
 (0)