clarifai-rails 0.1.0 → 0.1.1

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: a2aaf992b5f89e18825c6fe7d333c149a4cf32ab
4
- data.tar.gz: 37359e89ae991252ac8fdad362c41027fc08f2d3
3
+ metadata.gz: 5be6cebafc81e05908db735da87c5fbdb0940102
4
+ data.tar.gz: 4f6cdf1efd108c823b239b056793e8c2e30c9a73
5
5
  SHA512:
6
- metadata.gz: 2a30cd070f84203bdb827473e6944456016dfd7d90dde2eefb5422357e803d17874bbdd94eab2b6f3b4fad48f25c7cfb26ad2a4d7df931aac560ea5a4ac52efe
7
- data.tar.gz: 48eba4b5a9ae29db66eb33af4d5747f486235f1f2a8508ef85f2c58f42dcaa4764ec2821088f664e07e23a9233e14278224a8007b82d1dbd0b8f5460df18bc01
6
+ metadata.gz: eada820d2430b077964e4453ddf8cad8fccae884ac4a16297079a8420db20d5092ce2b048ee2e64c6f926c6b3ae12d371d336dbc99bb01bdcfdd4ae36416b3db
7
+ data.tar.gz: dae8da72ef309e32ea535a553b24ba5b336240362b7e695fabffe8e04d7ee3e864b3099ed6fbc0cca20922078ce33c2d87f90539615bc718d65004a35daec6f6
data/README.md CHANGED
@@ -22,7 +22,94 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
25
+ ### Initialize
26
+
27
+ $ rails g clarifai:install
28
+
29
+ ### Config Client ID, Client Secret
30
+
31
+ Insert your application info in ```config/initializers/clarifai.rb```
32
+
33
+ ### How to detect image
34
+
35
+ ```ruby
36
+ Clarifai::Rails::Detector.new(image_url).image
37
+ ```
38
+
39
+ Return ```Clarifai::Rails::Image``` object
40
+
41
+ ### For multiple images
42
+
43
+ ```ruby
44
+ Clarifai::Rails::Detector.new([image_url_1, image_url_2]).images
45
+ ```
46
+
47
+ Return array ```Clarifai::Rails::Image``` object
48
+
49
+
50
+ With ```image``` is ```Clarifai::Rails::Image``` object
51
+
52
+ #### Get tags
53
+
54
+ ```
55
+ image.tags
56
+ ```
57
+ Return for you is a String array, it is tags list
58
+
59
+ #### Get tags with percent in image
60
+
61
+ ```
62
+ image.tags_with_percent
63
+ ```
64
+ Return is a Hash with key is tag and value is percent
65
+
66
+ #### Get url
67
+
68
+ ```
69
+ image.url
70
+ ```
71
+ Return ia a String
72
+
73
+ #### Get docid
74
+
75
+ ```
76
+ image.docid
77
+ ```
78
+ Return ia a Number
79
+
80
+ #### Get docid_str
81
+
82
+ ```
83
+ image.docid_str
84
+ ```
85
+ Return ia a String
86
+
87
+ #### Check status
88
+
89
+ ```
90
+ image.success?
91
+ ```
92
+ AND
93
+
94
+ ```
95
+ image.error?
96
+ ```
97
+
98
+ #### Get status_code
99
+
100
+ ```
101
+ image.status_code
102
+ ```
103
+ Return ia a String
104
+
105
+ Can you see more info at https://developer.clarifai.com/docs/status_codes
106
+
107
+ #### Get status_msg
108
+
109
+ ```
110
+ image.status_messages
111
+ ```
112
+ Return ia a String
26
113
 
27
114
  ## Development
28
115
 
@@ -32,7 +119,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
119
 
33
120
  ## Contributing
34
121
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/clarifai-rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
122
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ThanhKhoaIT/clarifai-rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36
123
 
37
124
 
38
125
  ## License
@@ -4,6 +4,7 @@ module Clarifai
4
4
  autoload :Token, "clarifai/rails/token"
5
5
  autoload :Detector, "clarifai/rails/detector"
6
6
  autoload :Image, "clarifai/rails/image"
7
+ autoload :Error, "clarifai/rails/error"
7
8
 
8
9
  def self.setup
9
10
  yield self
@@ -11,31 +11,51 @@ module Clarifai
11
11
  @@clarifai_token_expire ||= Time.current
12
12
  end
13
13
 
14
- def image
14
+ def to_hash
15
15
  @data_urls ||= fetch
16
- Clarifai::Rails::Image.new(@data_urls["results"].first)
16
+ JSON.parse(@data_urls).symbolize_keys
17
+ end
18
+
19
+ def results
20
+ to_hash[:results]
17
21
  end
18
22
 
19
23
  def images
20
- @data_urls ||= fetch
21
- @data_urls["results"].map do |json|
24
+ results.map do |json|
22
25
  Clarifai::Rails::Image.new(json)
23
26
  end
24
27
  end
25
28
 
29
+ def image
30
+ Clarifai::Rails::Image.new(results.first)
31
+ end
32
+
33
+ # Status method
34
+
35
+ def error
36
+ Clarifai::Rails::Error.detector(to_hash[:status_code])
37
+ end
38
+
39
+ def error?
40
+ error.present?
41
+ end
42
+
43
+ def success?
44
+ error.blank?
45
+ end
46
+
26
47
  private
27
48
 
28
49
  def fetch
29
50
  new_token if @@clarifai_token_expire <= Time.current
30
51
  params_string = @@urls.join("&url=")
31
- response = open("https://api.clarifai.com/v1/tag?url=#{params_string}", "Authorization" => "Bearer #{@@clarifai_token["access_token"]}")
32
-
33
- JSON.parse(response.read)
52
+ response = open("https://api.clarifai.com/v1/tag?url=#{params_string}", "Authorization" => "Bearer #{@@clarifai_token[:access_token]}")
53
+ response.read
34
54
  end
35
55
 
36
56
  def new_token
37
- @@clarifai_token = Clarifai::Rails::Token.new.create
38
- @@clarifai_token_expire = Time.current + @@clarifai_token["expires_in"]
57
+ @@clarifai_token = Clarifai::Rails::Token.new.create.symbolize_keys
58
+ @@clarifai_token_expire = Time.current + @@clarifai_token[:expires_in]
39
59
  end
40
60
 
41
61
  end
@@ -0,0 +1,60 @@
1
+ module Clarifai
2
+ module Rails
3
+
4
+ class Error < StandardError
5
+
6
+ def initialize(code, text)
7
+ @error_code = code
8
+ @error_text = text
9
+ end
10
+
11
+ def code
12
+ @error_code
13
+ end
14
+
15
+ def message
16
+ @error_text
17
+ end
18
+
19
+ def self.detector(status_code)
20
+ error = case status_code
21
+ when "PARTIAL_ERROR"
22
+ PartialError.new(200, "Some images in request have failed. Please review the error messages per image.")
23
+ when "ALL_ERROR"
24
+ AllError.new(400, "Bad request.")
25
+ when "CLIENT_ERROR"
26
+ error_code = 400
27
+ error_text = "Data loading failed, see results for details."
28
+ when "SERVER_ERROR"
29
+ error_code = 500
30
+ error_text = "Data failed to process, see results for details."
31
+ when "TOKEN_APP_INVALID"
32
+ error_code = 401
33
+ error_text = "Application for this token is not valid. Please ensure that you are using ID and SECRET from same application."
34
+ when "TOKEN_EXPIRED"
35
+ error_code = 401
36
+ error_text = "Token has expired, you must generate a new access token."
37
+ when "TOKEN_INVALID"
38
+ error_code = 401
39
+ error_text = "Token is not valid. Please use valid tokens for a application in your account."
40
+ when "TOKEN_NONE"
41
+ error_code = 401
42
+ error_text = "Authentication credentials were not provided in request."
43
+ when "TOKEN_NO_SCOPE"
44
+ error_code = 401
45
+ error_text = "Token does not have the required scope to access resources."
46
+ end
47
+
48
+ return error if error.present?
49
+ end
50
+
51
+ class PartialError < Error; end
52
+ class AllError < Error; end
53
+ class ClientError < Error; end
54
+ class ServerError < Error; end
55
+ class TokenAppInvalid < Error; end
56
+
57
+ end
58
+
59
+ end
60
+ end
@@ -15,15 +15,14 @@ module Clarifai
15
15
  end
16
16
 
17
17
  def tags
18
- return nil if is_error?
19
- json[:result]["tag"]["classes"]
18
+ raise error if error?
19
+ result["tag"]["classes"]
20
20
  end
21
21
 
22
22
  def tags_with_percent
23
- return nil if is_error?
24
23
  tags_hash = {}
25
24
  tags.each_with_index do |tag, index|
26
- tags_hash["#{tag}"] = json[:result]["tag"]["probs"][index]
25
+ tags_hash["#{tag}"] = result["tag"]["probs"][index]
27
26
  end
28
27
  tags_hash.symbolize_keys
29
28
  end
@@ -37,21 +36,29 @@ module Clarifai
37
36
  end
38
37
 
39
38
  def docid_str
40
- json[:result]["docid_str"]
39
+ result["docid_str"]
41
40
  end
42
41
 
43
- def is_error?
44
- status_code != "OK"
42
+ def error
43
+ Clarifai::Rails::Error.detector(status_code)
45
44
  end
46
45
 
47
- def is_success?
48
- status_code == "OK"
46
+ def error?
47
+ error.present?
49
48
  end
50
49
 
51
- protected
50
+ def success?
51
+ error.blank?
52
+ end
53
+
54
+ private
52
55
 
53
56
  attr_reader :json
54
57
 
58
+ def result
59
+ json[:result]
60
+ end
61
+
55
62
  end
56
63
  end
57
64
  end
@@ -1,5 +1,5 @@
1
1
  module Clarifai
2
2
  module Rails
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
@@ -1,7 +1,7 @@
1
1
  Clarifai::Rails.setup do |config|
2
2
 
3
- config.client_id = ""
3
+ config.client_id = "your clarifai client id"
4
4
 
5
- config.client_secret = ""
5
+ config.client_secret = "your clarifai client secret"
6
6
 
7
7
  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.0
4
+ version: 0.1.1
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-17 00:00:00.000000000 Z
11
+ date: 2016-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -58,6 +58,7 @@ files:
58
58
  - lib/clarifai-rails.rb
59
59
  - lib/clarifai/rails.rb
60
60
  - lib/clarifai/rails/detector.rb
61
+ - lib/clarifai/rails/error.rb
61
62
  - lib/clarifai/rails/image.rb
62
63
  - lib/clarifai/rails/token.rb
63
64
  - lib/clarifai/rails/version.rb