vibe 0.0.2 → 0.0.3

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: 572f86112bab6191738be93ce7f36014f12e764e
4
- data.tar.gz: a273852fd99843b808095f012054822f4dfd134c
3
+ metadata.gz: d995065c644162bb30adbc920ae3272a6221ccfb
4
+ data.tar.gz: 434fce7d0cbaaa953f652d4455fe2a8b8a3d5144
5
5
  SHA512:
6
- metadata.gz: c7446c1be177bcaa1a8c8b7b7deb721bf44c0205c78e44966cb707846fd1de73e95cf9f9b9bbdd5df8cdb84c8d9e15a8ce570925cd8582a6c4ea71900e5fcc4c
7
- data.tar.gz: 96293a87ce562ae4cd8e353d33c5cd2ccc99d58636cee83b243df843c0cd115bd0c43ae7003386ccc6a26c8d0af268c35563b01c6e4f6ce481add233a4c6e23b
6
+ metadata.gz: 0fa8e1be38624e4c3a4685e268fcd2bebf60118f3c8469d1f9b4536e15a4939d0dfd1c4802dd28e97c5660f2be82d94905356723b2c9c86b5cbaea3a191604a9
7
+ data.tar.gz: c8d2d0c62f8064fb84cc9ea153bad0bb4abd3b15d2bba5d61a197d187e5dec6c40b34d569effd31afaa9550ae5e9b9944346783bacbd444a734e0ec6e295d0c0
@@ -7,7 +7,8 @@ PATH
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- mime-types (2.4.2)
10
+ mime-types (2.4.3)
11
+ minitest (5.4.2)
11
12
  netrc (0.8.0)
12
13
  rake (10.1.0)
13
14
  rest-client (1.7.2)
@@ -19,5 +20,6 @@ PLATFORMS
19
20
 
20
21
  DEPENDENCIES
21
22
  bundler (~> 1.7)
23
+ minitest (~> 5.4.2)
22
24
  rake (~> 10.0)
23
25
  vibe!
data/README.md CHANGED
@@ -10,7 +10,7 @@ Vibe API
10
10
 
11
11
  [RDocs](http://rubydoc.info/github/amalfra/vibe/master/frames)
12
12
 
13
- A Ruby wrapper for the Vibe REST API.
13
+ A Ruby wrapper for the Vibe REST API.
14
14
 
15
15
  > Get API key here : https://vibeapp.co/dev/
16
16
 
@@ -36,7 +36,7 @@ Create a new instance
36
36
  vibe = Vibe.new
37
37
  ```
38
38
 
39
- At this stage you can also supply the configuration parameter `:api_key` which is used throughout the API. These can be passed directly as hash options:
39
+ At this stage you can also supply the configuration parameter `:api_key`, `:cache` which is used throughout the API. These can be passed directly as hash options:
40
40
 
41
41
  ```ruby
42
42
  vibe = Vibe.new api_key: 'api_key'
@@ -47,15 +47,21 @@ Alternatively, you can configure the Vibe settings by passing a block:
47
47
  ```ruby
48
48
  vibe = Vibe.new do |config|
49
49
  config.api_key = 'api_key'
50
+ config.cache = false # Maybe i need to turn off cache
50
51
  end
51
52
  ```
52
53
 
54
+ ## Cache
55
+
56
+ The wrapper has Cache feature which would cache API response for a day. **The stats API call which is used to get the status of an API key won't be cached**. The currently supported Cache methods are:
57
+ * File: Stored in system temporary path.
58
+
53
59
  ## API
54
60
 
55
- The currently available api methods are :
61
+ The currently available api methods are:
56
62
  * get_data(email) - Get Data from an Email
57
63
  * stats - Get Stats for API Key. If API key parameter is not
58
- passed then the one in config will be used
64
+ passed then the one in config will be used
59
65
 
60
66
 
61
67
  ## Development
@@ -1,4 +1,5 @@
1
- %w{version error configuration client}.each do |local|
1
+ %w{version error configuration cache client}.each do |local|
2
+ puts "Including #{local}" if ENV['DEBUG']
2
3
  require "vibe/#{local}"
3
4
  end
4
5
 
@@ -8,7 +9,7 @@ module Vibe
8
9
  # Alias for Vibe::Client.new
9
10
  #
10
11
  # @return [Vibe::Client]
11
- def self.new(options = { }, &block)
12
+ def self.new(options = {}, &block)
12
13
  @api_client = Vibe::Client.new(options, &block)
13
14
  end
14
15
  end
@@ -0,0 +1,56 @@
1
+ module Vibe
2
+ class File
3
+
4
+ def initialize(cache_dir = '')
5
+ # Cache directory in sys temp path if not
6
+ # explicitly set
7
+ @cache_dir = Dir.tmpdir()+'/vibe_gem/' if cache_dir == ''
8
+
9
+ if !Dir.exists?(@cache_dir)
10
+ Dir.mkdir(@cache_dir)
11
+ end
12
+ end
13
+
14
+ # Get content from cache file. This function would
15
+ # delete cache if expired(Older than a day)
16
+ #
17
+ # @return [Boolean]
18
+ def get(key = '')
19
+ if key != '' && ::File.exists?(@cache_dir+key)
20
+ # Is the File older than a day?
21
+ if file_age(@cache_dir+key) > 1
22
+ # Delete and return a cache miss
23
+ File.delete(@cache_dir+key)
24
+ return false
25
+ end
26
+
27
+ puts "Reading from cache with key: #{key}" if ENV['DEBUG']
28
+ data = ::File.read(@cache_dir+key)
29
+ return (data.length > 0) ? data : false
30
+ end
31
+
32
+ return false
33
+ end
34
+
35
+ # Write content into cache file
36
+ #
37
+ # @return [Boolean]
38
+ def put(key = '', data = '')
39
+ if key != '' && data != ''
40
+ puts "Writing into cache with key: #{key}" if ENV['DEBUG']
41
+ return ::File.write(@cache_dir+key, data)
42
+ end
43
+
44
+ return false
45
+ end
46
+
47
+ # Return the number of days since the file
48
+ # was last modified
49
+ #
50
+ # @return [Int]
51
+ def file_age(name)
52
+ (Time.now - ::File.stat(name).mtime)/(24*3600)
53
+ end
54
+
55
+ end # File
56
+ end
@@ -0,0 +1,37 @@
1
+ module Vibe
2
+ class Cache
3
+
4
+ extend Configuration
5
+ include Error
6
+
7
+ DRIVERS = ['FILE']
8
+
9
+ def initialize(options = {})
10
+ # If invalid cache driver given
11
+ if options.include?(:driver) && !DRIVERS.include?(options[:driver])
12
+ raise ArgumentError, "unkown cache driver: #{options[:driver]}"
13
+ else
14
+ # Load the required cache driver
15
+ require "vibe/cache-drivers/#{options[:driver].downcase}"
16
+ @driver = eval(options[:driver].downcase.capitalize).new
17
+ end
18
+ end
19
+
20
+ # Get key from cache, proxies into
21
+ # method with same name in driver class
22
+ #
23
+ # @return [Driver.get]
24
+ def get(key = '')
25
+ @driver.get(key)
26
+ end
27
+
28
+ # Write to cache, proxies into
29
+ # method with same name in driver class
30
+ #
31
+ # @return [Driver.put]
32
+ def put(key = '', data = '')
33
+ @driver.put(key, data)
34
+ end
35
+
36
+ end # Cache
37
+ end
@@ -7,7 +7,7 @@ module Vibe
7
7
 
8
8
  attr_accessor *Configuration::VALID_CONFIG_KEYS
9
9
 
10
- def initialize(options={}, &block)
10
+ def initialize(options = {}, &block)
11
11
  # Merge the config values from the module and those passed
12
12
  # to the client.
13
13
  merged_options = Vibe.options.merge(options)
@@ -15,6 +15,7 @@ module Vibe
15
15
  # Copy the merged values and ignore those
16
16
  # not part of our configuration
17
17
  Configuration::VALID_CONFIG_KEYS.each do |key|
18
+ puts "Setting Configuration: #{key}" if ENV['DEBUG']
18
19
  send("#{key}=", merged_options[key]) if Configuration::VALID_OPTIONS_KEYS.include? key
19
20
  send("#{key}=", Vibe.options[key]) if Configuration::VALID_CONNECTION_KEYS.include? key
20
21
  end
@@ -23,8 +24,10 @@ module Vibe
23
24
  end
24
25
 
25
26
  # Get Data from an Email
27
+ #
28
+ # @return [String]
26
29
  def get_data(email)
27
- if !email.is_a? String
30
+ if !email.is_a? String
28
31
  error = InvalidOptions.new(['Email(String)'], ['Email(String)'])
29
32
  raise error
30
33
  end
@@ -33,13 +36,16 @@ module Vibe
33
36
  end
34
37
 
35
38
  # Get Stats for API Key
39
+ #
40
+ # @return [String]
36
41
  def stats(api_key = nil)
37
42
  if api_key && (!api_key.is_a? String)
38
43
  error = InvalidOptions.new(['API key(String)'], ['API key(String)'])
39
44
  raise error
40
45
  end
41
46
 
42
- get_request '/api_stats', :api_key => api_key
47
+ # Disable cache for API key status calls
48
+ get_request('/api_stats', {:api_key => api_key}, true)
43
49
  end
44
50
 
45
51
  end # Client
@@ -1,46 +1,54 @@
1
1
  module Vibe
2
2
  module Configuration
3
3
  VALID_CONNECTION_KEYS = [:endpoint, :user_agent, :method, :format].freeze
4
- VALID_OPTIONS_KEYS = [:api_key].freeze
4
+ VALID_OPTIONS_KEYS = [:api_key, :cache, :cache_driver].freeze
5
5
  VALID_CONFIG_KEYS = VALID_CONNECTION_KEYS + VALID_OPTIONS_KEYS
6
-
7
- DEFAULT_ENDPOINT = 'https://vibeapp.co/api/v1/'.freeze
8
- DEFAULT_METHOD = :get.freeze
9
- DEFAULT_USER_AGENT = "Vibe API Ruby Gem #{Vibe::VERSION}".freeze
10
-
11
- DEFAULT_API_KEY = nil
12
- DEFAULT_FORMAT = :json.freeze
13
-
6
+
7
+ DEFAULT_ENDPOINT = 'https://vibeapp.co/api/v1/'.freeze
8
+ DEFAULT_METHOD = :get.freeze
9
+ DEFAULT_USER_AGENT = "Vibe API Ruby Gem #{Vibe::VERSION}".freeze
10
+
11
+ DEFAULT_API_KEY = nil
12
+ DEFAULT_FORMAT = :json.freeze
13
+
14
+ DEFAULT_CACHE_DRIVER = 'FILE'.freeze
15
+ DEFAULT_CACHE = true
16
+
14
17
  # Build accessor methods for every config options so we can do this
15
18
  attr_accessor *Configuration::VALID_CONFIG_KEYS
16
-
17
- def initialize
18
- self.endpoint = DEFAULT_ENDPOINT
19
- self.method = DEFAULT_METHOD
20
- self.user_agent = DEFAULT_USER_AGENT
21
- self.format = DEFAULT_FORMAT
22
- end
19
+
20
+ @endpoint = DEFAULT_ENDPOINT
21
+ @method = DEFAULT_METHOD
22
+ @user_agent = DEFAULT_USER_AGENT
23
+ @format = DEFAULT_FORMAT
24
+ @cache_driver = DEFAULT_CACHE_DRIVER
25
+ @cache = DEFAULT_CACHE
23
26
 
24
27
  # Make sure we have the default values set when we get 'extended'
25
28
  def self.extended(base)
26
29
  base.reset
27
30
  end
28
-
31
+
29
32
  # Setup Configuration
30
33
  def configure
31
34
  yield self
32
35
  end
33
36
 
37
+ # Reset config values to default
34
38
  def reset
35
- self.endpoint = DEFAULT_ENDPOINT
36
- self.method = DEFAULT_METHOD
37
- self.user_agent = DEFAULT_USER_AGENT
38
- self.format = DEFAULT_FORMAT
39
+ self.endpoint = DEFAULT_ENDPOINT
40
+ self.method = DEFAULT_METHOD
41
+ self.user_agent = DEFAULT_USER_AGENT
42
+ self.format = DEFAULT_FORMAT
39
43
 
40
- self.api_key = DEFAULT_API_KEY
44
+ self.api_key = DEFAULT_API_KEY
45
+ self.cache_driver = DEFAULT_CACHE_DRIVER
46
+ self.cache = true
41
47
  end
42
-
48
+
43
49
  # Return the configuration values set in this module
50
+ #
51
+ # @return [Hash]
44
52
  def options
45
53
  Hash[ * VALID_CONFIG_KEYS.map { |key| [key, send(key)] }.flatten ]
46
54
  end
@@ -8,21 +8,44 @@ module Vibe
8
8
 
9
9
  METHODS = [:get, :post, :put, :delete, :patch]
10
10
 
11
- def get_request(path, params={})
12
- request(:get, path, params)
11
+ # Perform a GET HTTP request
12
+ #
13
+ # @return [Request.request]
14
+ def get_request(path, params = {}, cache_override = false)
15
+ request(:get, path, params, cache_override)
13
16
  end
14
17
 
15
- def request(method, path, params)
18
+ # Execute an HTTP request and return the result
19
+ # Exceptions would be raised based on HTTP status codes
20
+ #
21
+ # @return [String]
22
+ def request(method, path, params, cache_override = false)
16
23
  if !METHODS.include?(method)
17
- raise ArgumentError, "unkown http method: #{method}"
24
+ raise ArgumentError, "unkown http method: #{method}"
25
+ end
26
+
27
+ # Is caching activated?
28
+ if cache && !cache_override
29
+ cache = Cache.new driver: cache_driver
30
+
31
+ # Check if cache present
32
+ param_string = params.map{|k,v| "#{k}-#{v}"}.join(':')
33
+ filename = Digest::MD5.hexdigest(method.to_s.gsub("/", "_")+path.to_s.gsub("/", "_")+param_string).freeze
34
+ response = cache.get(filename)
35
+
36
+ if response
37
+ return JSON.parse(response)
38
+ end
18
39
  end
19
40
 
20
41
  if !api_key
21
- raise ClientError, 'API key need to be set'
42
+ raise ClientError, 'API key need to be set'
22
43
  end
23
44
 
24
45
  puts "EXECUTED: #{method} - #{path} with #{params}" if ENV['DEBUG']
25
46
 
47
+ # If API key is passed as a paramter then ignore the one
48
+ # set in config
26
49
  if params[:api_key]
27
50
  new_api_key = params[:api_key]
28
51
  params = params.tap { |hs| hs.delete(:api_key) }
@@ -34,6 +57,7 @@ module Vibe
34
57
  begin
35
58
  puts params if ENV['DEBUG']
36
59
  response = RestClient.send("#{method}", endpoint + path.gsub!(/^\//, ""), :params => params){ |response, request, result, &block|
60
+ # Go through redirection based on status code
37
61
  if [301, 302, 307].include? response.code
38
62
  response.follow_redirection(request, result, &block)
39
63
  elsif !response.code.between?(200, 400)
@@ -50,11 +74,20 @@ module Vibe
50
74
  raise ServiceError, {:status => response.code, :method => method, :response_headers => response.headers, :url => endpoint + path, :body => "API Returned status code #{response.code}"}
51
75
  end
52
76
  else
77
+ # Is caching activated?
78
+ if cache
79
+ # Put into cache
80
+ if !cache.put(filename, response)
81
+ raise ClientError, 'Unable to write into Cache'
82
+ end
83
+ end
84
+
85
+ # Parse JSON response
53
86
  JSON.parse(response)
54
87
  end
55
88
  }
56
89
  rescue SocketError => e
57
- raise VibeError, 'xgcv'
90
+ raise VibeError, 'SocketError Occured!'
58
91
  end
59
92
 
60
93
  end
@@ -1,3 +1,3 @@
1
1
  module Vibe
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,3 +1,3 @@
1
- require 'vibe'
2
1
  require 'minitest/spec'
3
2
  require 'minitest/autorun'
3
+ require 'vibe'
@@ -18,8 +18,11 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.required_ruby_version = '>= 1.9.3'
22
+
21
23
  spec.add_dependency "rest-client", "~> 1.7"
22
24
 
23
25
  spec.add_development_dependency "bundler", "~> 1.7"
24
26
  spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest", "~> 5.4.2"
25
28
  end
metadata CHANGED
@@ -1,57 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vibe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amal Francis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-20 00:00:00.000000000 Z
11
+ date: 2014-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.7'
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
26
  version: '1.7'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '1.7'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.7'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ~>
46
46
  - !ruby/object:Gem::Version
47
47
  version: '10.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 5.4.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 5.4.2
55
69
  description: A Ruby wrapper for the Vibe REST API
56
70
  email:
57
71
  - amalfra@gmail.com
@@ -59,13 +73,15 @@ executables: []
59
73
  extensions: []
60
74
  extra_rdoc_files: []
61
75
  files:
62
- - ".gitignore"
76
+ - .gitignore
63
77
  - Gemfile
64
78
  - Gemfile.lock
65
79
  - LICENSE
66
80
  - README.md
67
81
  - Rakefile
68
82
  - lib/vibe.rb
83
+ - lib/vibe/cache-drivers/file.rb
84
+ - lib/vibe/cache.rb
69
85
  - lib/vibe/client.rb
70
86
  - lib/vibe/configuration.rb
71
87
  - lib/vibe/error.rb
@@ -99,17 +115,17 @@ require_paths:
99
115
  - lib
100
116
  required_ruby_version: !ruby/object:Gem::Requirement
101
117
  requirements:
102
- - - ">="
118
+ - - '>='
103
119
  - !ruby/object:Gem::Version
104
- version: '0'
120
+ version: 1.9.3
105
121
  required_rubygems_version: !ruby/object:Gem::Requirement
106
122
  requirements:
107
- - - ">="
123
+ - - '>='
108
124
  - !ruby/object:Gem::Version
109
125
  version: '0'
110
126
  requirements: []
111
127
  rubyforge_project:
112
- rubygems_version: 2.2.2
128
+ rubygems_version: 2.3.0
113
129
  signing_key:
114
130
  specification_version: 4
115
131
  summary: A Ruby wrapper for the Vibe REST API