clarifai-rails 0.1.1 → 0.1.4

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: 5be6cebafc81e05908db735da87c5fbdb0940102
4
- data.tar.gz: 4f6cdf1efd108c823b239b056793e8c2e30c9a73
3
+ metadata.gz: 7bada0ca5c7cc4594a0a449144b13799f4f65973
4
+ data.tar.gz: 62a997af00984238fc741d4179a6393c5c5470bf
5
5
  SHA512:
6
- metadata.gz: eada820d2430b077964e4453ddf8cad8fccae884ac4a16297079a8420db20d5092ce2b048ee2e64c6f926c6b3ae12d371d336dbc99bb01bdcfdd4ae36416b3db
7
- data.tar.gz: dae8da72ef309e32ea535a553b24ba5b336240362b7e695fabffe8e04d7ee3e864b3099ed6fbc0cca20922078ce33c2d87f90539615bc718d65004a35daec6f6
6
+ metadata.gz: f792436b7f7ebdcc9cbdbee7a62e5bd73085a31cd2e158e8ba49576f948fdf1cb6629133ee91d912ae8f8e38f30b3d9e395109ded7ceaa7a6e8f7375a36abfe8
7
+ data.tar.gz: bce5ac6b6972c60cbe88dcc64219270d8b5a48beba6a8640f5c7dc23a30f6cc35a96e3ceb8649abde8602c4c85a49bd828a70acb8453f656e8478849a41d67c8
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ gem 'rest-client', '~> 1.8'
4
+
3
5
  # Specify your gem's dependencies in clarifai-rails.gemspec
4
6
  gemspec
data/README.md CHANGED
@@ -2,14 +2,12 @@
2
2
 
3
3
  Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/clarifai/rails`. To experiment with that code, run `bin/console` for an interactive prompt.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
5
  ## Installation
8
6
 
9
7
  Add this line to your application's Gemfile:
10
8
 
11
9
  ```ruby
12
- gem 'clarifai-rails'
10
+ gem 'clarifai-rails'
13
11
  ```
14
12
 
15
13
  And then execute:
@@ -33,7 +31,7 @@ Insert your application info in ```config/initializers/clarifai.rb```
33
31
  ### How to detect image
34
32
 
35
33
  ```ruby
36
- Clarifai::Rails::Detector.new(image_url).image
34
+ Clarifai::Rails::Detector.new(image_url).image
37
35
  ```
38
36
 
39
37
  Return ```Clarifai::Rails::Image``` object
@@ -41,64 +39,75 @@ Return ```Clarifai::Rails::Image``` object
41
39
  ### For multiple images
42
40
 
43
41
  ```ruby
44
- Clarifai::Rails::Detector.new([image_url_1, image_url_2]).images
42
+ Clarifai::Rails::Detector.new([image_url_1, image_url_2]).images
43
+ ```
44
+
45
+ Return array ```Clarifai::Rails::Image``` object
46
+
47
+ ### If you want to download images before detect (Facebook images, ...)
48
+
49
+ ```ruby
50
+ clarifai_detector = Clarifai::Rails::Detector.new([image_url_1, image_url_2])
51
+ clarifai_detector.need_download!
52
+ clarifai_detector.images
45
53
  ```
46
54
 
47
55
  Return array ```Clarifai::Rails::Image``` object
48
56
 
57
+ ### Note
49
58
 
50
59
  With ```image``` is ```Clarifai::Rails::Image``` object
51
60
 
52
61
  #### Get tags
53
62
 
54
- ```
55
- image.tags
63
+ ```ruby
64
+ image.tags
56
65
  ```
57
66
  Return for you is a String array, it is tags list
58
67
 
59
68
  #### Get tags with percent in image
60
69
 
61
- ```
62
- image.tags_with_percent
70
+ ```ruby
71
+ image.tags_with_percent
63
72
  ```
64
73
  Return is a Hash with key is tag and value is percent
65
74
 
66
75
  #### Get url
67
76
 
68
- ```
69
- image.url
77
+ ```ruby
78
+ image.url
70
79
  ```
71
80
  Return ia a String
72
81
 
73
82
  #### Get docid
74
83
 
75
- ```
76
- image.docid
84
+ ```ruby
85
+ image.docid
77
86
  ```
78
87
  Return ia a Number
79
88
 
80
89
  #### Get docid_str
81
90
 
82
- ```
83
- image.docid_str
91
+ ```ruby
92
+ image.docid_str
84
93
  ```
85
94
  Return ia a String
86
95
 
87
96
  #### Check status
88
97
 
89
- ```
90
- image.success?
98
+ ```ruby
99
+ image.success?
91
100
  ```
92
101
  AND
93
102
 
94
- ```
95
- image.error?
103
+ ```ruby
104
+ image.error?
96
105
  ```
97
106
 
98
107
  #### Get status_code
99
108
 
100
- ```
101
- image.status_code
109
+ ```ruby
110
+ image.status_code
102
111
  ```
103
112
  Return ia a String
104
113
 
@@ -106,8 +115,8 @@ Can you see more info at https://developer.clarifai.com/docs/status_codes
106
115
 
107
116
  #### Get status_msg
108
117
 
109
- ```
110
- image.status_messages
118
+ ```ruby
119
+ image.status_messages
111
120
  ```
112
121
  Return ia a String
113
122
 
@@ -29,4 +29,5 @@ Gem::Specification.new do |spec|
29
29
 
30
30
  spec.add_development_dependency "bundler", "~> 1.10"
31
31
  spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_dependency "rest-client", "1.8"
32
33
  end
@@ -10,10 +10,11 @@ module Clarifai
10
10
  yield self
11
11
  end
12
12
 
13
- mattr_accessor :client_id, :client_secret
13
+ mattr_accessor :client_id, :client_secret, :tag_url
14
14
 
15
15
  @@client_id = nil
16
16
  @@client_secret = nil
17
+ @@tag_url = "https://api.clarifai.com/v1/tag"
17
18
 
18
19
  end
19
20
  end
@@ -1,19 +1,26 @@
1
1
  require "open-uri"
2
+ require "rest-client"
2
3
 
3
4
  module Clarifai
4
5
  module Rails
5
6
  class Detector
6
7
 
7
- def initialize(urls)
8
+ def initialize(urls, download=false)
8
9
  raise "Input data not supported! (String Array or String only)" unless urls.is_a?(Array) || urls.is_a?(String)
9
- @@urls = urls.is_a?(Array) ? urls : [urls]
10
10
 
11
- @@clarifai_token_expire ||= Time.current
11
+ @urls = urls.is_a?(Array) ? urls : [urls]
12
+ @download = download
13
+ $clarifai_token_expire ||= Time.current
14
+ end
15
+
16
+ def need_download!
17
+ @download = true
18
+ self
12
19
  end
13
20
 
14
21
  def to_hash
15
- @data_urls ||= fetch
16
- JSON.parse(@data_urls).symbolize_keys
22
+ @data_urls = fetch if @data_urls.blank?
23
+ @data_urls.is_a?(Hash) ? @data_urls : JSON.parse(@data_urls).symbolize_keys
17
24
  end
18
25
 
19
26
  def results
@@ -47,15 +54,32 @@ module Clarifai
47
54
  private
48
55
 
49
56
  def fetch
50
- new_token if @@clarifai_token_expire <= Time.current
51
- params_string = @@urls.join("&url=")
52
- response = open("https://api.clarifai.com/v1/tag?url=#{params_string}", "Authorization" => "Bearer #{@@clarifai_token[:access_token]}")
57
+ new_token if $clarifai_token_expire <= Time.current
58
+ @download ? urls_protected : urls_unprotected
59
+ end
60
+
61
+ def urls_unprotected
62
+ params_string = @urls.join("&url=")
63
+ response = open("#{Clarifai::Rails.tag_url}?url=#{params_string}", "Authorization" => "Bearer #{$clarifai_token[:access_token]}")
53
64
  response.read
54
65
  end
55
66
 
67
+ def urls_protected
68
+ data = { results: [], status_code: nil }
69
+ @urls.each do |url|
70
+ result = RestClient.post( Clarifai::Rails.tag_url,
71
+ { encoded_data: File.new(open(url)) },
72
+ Authorization: "Bearer #{$clarifai_token[:access_token]}")
73
+ json = JSON.parse(result).symbolize_keys
74
+ data[:results] << json[:results].first
75
+ data[:status_code] = json[:status_code]
76
+ end
77
+ data
78
+ end
79
+
56
80
  def new_token
57
- @@clarifai_token = Clarifai::Rails::Token.new.create.symbolize_keys
58
- @@clarifai_token_expire = Time.current + @@clarifai_token[:expires_in]
81
+ $clarifai_token = Clarifai::Rails::Token.new.create.symbolize_keys
82
+ $clarifai_token_expire = Time.current + $clarifai_token[:expires_in]
59
83
  end
60
84
 
61
85
  end
@@ -1,5 +1,5 @@
1
1
  module Clarifai
2
2
  module Rails
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.4"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clarifai-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karl Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-18 00:00:00.000000000 Z
11
+ date: 2016-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
41
55
  description: Clarifai API Rails Client
42
56
  email:
43
57
  - thanhkhoait@gmail.com