gcs_api_gem 0.2.0 → 0.4.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: f39f5ad6c2fbe1d0f008d6754c0b487da6b4e907
4
- data.tar.gz: 332525d5462b31ca8388a045a040119b3edd4e08
3
+ metadata.gz: 3b1ea2ce3dab1cc8bec78eebfe4ec094771aead3
4
+ data.tar.gz: cd30351439e67035fc8a1f8e3d64d5f4ee71f9c3
5
5
  SHA512:
6
- metadata.gz: f6b3366b478d493fc818cd7bc3e3cd444d012dc3071b5d13db1c4540c05b7afa87fccfbc05da8ce22cb8678f93bc22346b07a5ed768ea8616100bc8509d1a475
7
- data.tar.gz: af930ac3ef7b737f858cb1481bb50b54023c229c30aaacb20e95e6e4147a631d78a12288d81612b202db84a32b14da609aae971f7f600ad4899297e83a4fd93f
6
+ metadata.gz: 90491d11b00bbfd521680153b67bf644facea5ffe40639aac23f2678a7a24f3b7bac193fd49a434b1e21f88c514326fc282b370856cc835534dc14c4d08c28df
7
+ data.tar.gz: 7be6cd60b4aea32cb7aaa715ce8b7b36dec4917f3a234beee027ca3e844dea0a69884bfb111a09b5a1a63267ebcc9708de289ca9630a3f5de744bc311e173b77
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2017 高橋 雄太
3
+ Copyright (c) 2017 Yuta Takahashi
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -45,35 +45,48 @@ If you want to know how to get GOOGLE_CUSTOME_SEARCH_ENGINE_ID and GOOGLE_API_KE
45
45
  Or see [here](http://ryutamaki.hatenablog.com/entry/2014/01/18/171640).
46
46
 
47
47
  ## Usage
48
+ ### In a program
48
49
 
49
50
  Write in your program as below:
50
51
 
51
52
  ```ruby
52
- GcsApiGem::SearchApu.rondom_image(<keyword>)
53
+ obj.GcsApiGem::SearchApi.new
54
+ obj.random_image(<keyword>)
53
55
  ```
54
56
 
55
57
  Then you get a random image searched by keyword.
56
58
 
57
- ## Example
59
+ #### Example
58
60
 
59
61
  ```
60
- > GcsApiGem::SearchApi.random_image('cat')
62
+ > obj = GcsApiGem::SearchApi.new
63
+ > obj.random_image('cat')
61
64
  => {
65
+ :keyword=>"cat",
62
66
  :title=>"Cute Cat - Chrome Web Store",
63
67
  :image=>"https://lh3.googleusercontent.com/FnN1_Jabl5WITJrHYLGv80r-GNSNAPI_nwrrSgHh8fRUh5fsHFMmPFkPOjaX16H5Pi-Fwotg=w640-h400-e365",
64
- :search_result=>"320000000"
68
+ :search_result=>"320000000",
69
+ :start_index=>2
65
70
  }
66
71
  ```
67
72
 
68
- ## Response
73
+ #### Response
69
74
 
70
- Response of GcsApiGem::SearchApi.random_image is as below:
75
+ Response of GcsApiGem::SearchApi#random_image is as below:
71
76
 
72
- | key | detail |
73
- | ------------- | --------------------------------------------- |
74
- | title | The title of the web site that got the image. |
75
- | image | The URL of the got image. |
76
- | search_result | Number of images hit in search. |
77
+ | key | detail |
78
+ | ------------- | ----------------------------------------------- |
79
+ | keyword | It is using keyword to search image. |
80
+ | title | The title of the web site that got the image. |
81
+ | image | The URL of the got image. |
82
+ | search_result | Number of images hit in search. |
83
+ | start_index | Number of page on which the image is displayed. |
84
+
85
+ ### In a command line
86
+
87
+ Write in a command line as below:
88
+
89
+ $ gcs_api_gem -r cat
77
90
 
78
91
  ## Contributing
79
92
 
@@ -33,5 +33,6 @@ unless errors.count.zero?
33
33
  exit 1
34
34
  end
35
35
 
36
- p output(GcsApiGem::SearchApi.random_image(keyword))
36
+ obj = GcsApiGem::SearchApi.new
37
+ p output(obj.random_image(keyword))
37
38
  exit 0
@@ -30,10 +30,10 @@ Gem::Specification.new do |spec|
30
30
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
31
  spec.require_paths = ["lib"]
32
32
 
33
- spec.add_runtime_dependency "httpclient"
33
+ spec.add_runtime_dependency "httpclient", "~> 2.8.3"
34
34
  spec.add_development_dependency "bundler", "~> 1.15"
35
35
  spec.add_development_dependency "rake", "~> 10.0"
36
36
  spec.add_development_dependency "rspec", "~> 3.0"
37
- spec.add_development_dependency "pry"
38
- spec.add_development_dependency "pry-byebug"
37
+ spec.add_development_dependency "pry", "0.10.4"
38
+ spec.add_development_dependency "pry-byebug", "3.5.1"
39
39
  end
@@ -1,68 +1,105 @@
1
1
  module GcsApiGem
2
2
  # SearchApi
3
3
  class SearchApi
4
- class << self
5
- # Select one piece randomly from images searched by keyword
6
- def random_image(keyword)
7
- return { error: 'Please set KEY to environment variable.' } if key_is_ng?
8
- return { error: 'Please set CSE_ID to environment variable.' } if cse_id_is_ng?
4
+ # Decide to what range of page you want to acquire
5
+ SEARCH_RANGE = 3
9
6
 
10
- choice(fetch(keyword))
11
- end
7
+ def initialize
8
+ @items = []
9
+ @search_result = nil
10
+ @keyword = nil
11
+ @start_index = nil
12
+ end
12
13
 
13
- private
14
+ # Select one piece randomly from images searched by keyword
15
+ def random_image(keyword)
16
+ return { error: 'Please set KEY to environment variable.' } if key_is_ng?
17
+ return { error: 'Please set CSE_ID to environment variable.' } if cse_id_is_ng?
14
18
 
15
- def key_is_ng?
16
- blank?(ENV['KEY'])
17
- end
19
+ choice(keyword)
20
+ end
18
21
 
19
- def cse_id_is_ng?
20
- blank?(ENV['CSE_ID'])
21
- end
22
+ private
22
23
 
23
- def url
24
- 'https://www.googleapis.com/customsearch/v1'
25
- end
24
+ def key_is_ng?
25
+ blank?(ENV['KEY'])
26
+ end
26
27
 
27
- def query(keyword)
28
- {
29
- 'key' => ENV['KEY'],
30
- 'cx' => ENV['CSE_ID'],
31
- 'searchType' => 'image',
32
- 'q' => keyword,
33
- 'start' => rand(50) + 1
34
- }
35
- end
28
+ def cse_id_is_ng?
29
+ blank?(ENV['CSE_ID'])
30
+ end
36
31
 
37
- def fetch(keyword)
38
- client = HTTPClient.new
39
- res = client.get(url, query(keyword))
40
- JSON.parse res.body
41
- end
32
+ def url
33
+ 'https://www.googleapis.com/customsearch/v1'
34
+ end
42
35
 
43
- def choice(res)
44
- if blank?(res['error'])
45
- if res['searchInformation']['totalResults'].to_i.zero?
46
- return { error: 'Search result was 0.' }
47
- end
48
- formatted_item(res)
49
- else
50
- res['error']
51
- end
52
- end
36
+ def query(keyword)
37
+ {
38
+ 'key' => ENV['KEY'],
39
+ 'cx' => ENV['CSE_ID'],
40
+ 'searchType' => 'image',
41
+ 'q' => keyword,
42
+ 'start' => rand(SEARCH_RANGE) + 1,
43
+ 'imgType' => 'photo'
44
+ }
45
+ end
53
46
 
54
- def blank?(obj)
55
- obj.nil? || obj.empty?
56
- end
47
+ def fetch(keyword)
48
+ client = HTTPClient.new
49
+ res = client.get(url, query(keyword))
50
+ JSON.parse res.body
51
+ end
57
52
 
58
- def formatted_item(res)
59
- item = res['items'][rand(res['items'].count)]
60
- {
61
- title: item['title'],
62
- image: item['link'],
63
- search_result: res['searchInformation']['totalResults']
64
- }
53
+ def choice(keyword)
54
+ if @keyword != keyword
55
+ @keyword = keyword
56
+ pull_data
65
57
  end
58
+ formatted_item(select_item)
59
+ rescue => e
60
+ return { error: e.message }
61
+ end
62
+
63
+ def pull_data
64
+ p 'do pull_data'
65
+ res = fetch(@keyword)
66
+ check_error(res)
67
+
68
+ @items = res['items']
69
+ @search_result = res['searchInformation']['totalResults'].to_i
70
+ @start_index = res['queries']['request'][0]['startIndex']
71
+ end
72
+
73
+ def check_error(res)
74
+ raise res['error'].to_s unless blank?(res['error'])
75
+ raise 'Search result was 0.' if count_zero?(res)
76
+ end
77
+
78
+ def select_item
79
+ pull_data if @items.count.zero?
80
+
81
+ index = [*(0..@items.count - 1)].sample
82
+ item = @items[index]
83
+ @items.delete_at(index)
84
+ item
85
+ end
86
+
87
+ def blank?(obj)
88
+ obj.nil? || obj.empty?
89
+ end
90
+
91
+ def formatted_item(item)
92
+ {
93
+ keyword: @keyword,
94
+ title: item['title'],
95
+ image: item['link'],
96
+ search_result: @search_result,
97
+ start_index: @start_index
98
+ }
99
+ end
100
+
101
+ def count_zero?(res)
102
+ res['searchInformation']['totalResults'].to_i.zero?
66
103
  end
67
104
  end
68
105
  end
@@ -1,3 +1,3 @@
1
1
  module GcsApiGem
2
- VERSION = '0.2.0'
2
+ VERSION = '0.4.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gcs_api_gem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuta Takahashi
@@ -14,16 +14,16 @@ dependencies:
14
14
  name: httpclient
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 2.8.3
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 2.8.3
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -70,30 +70,30 @@ dependencies:
70
70
  name: pry
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - '='
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: 0.10.4
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - '='
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: 0.10.4
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: pry-byebug
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ">="
87
+ - - '='
88
88
  - !ruby/object:Gem::Version
89
- version: '0'
89
+ version: 3.5.1
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ">="
94
+ - - '='
95
95
  - !ruby/object:Gem::Version
96
- version: '0'
96
+ version: 3.5.1
97
97
  description: Google Custom Search API gem.
98
98
  email:
99
99
  - m1205123ing@gmail.com