tincan-api 1.0.1 → 1.1.0

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tincan.rb +15 -53
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c32c0ab4d57941b00135030ff38a27c64af3dc25
4
- data.tar.gz: fe82042c353a55cae57feab55d020fb6e6b1ef58
3
+ metadata.gz: acc32e3308e93eccbe80e722a79fcc610a165657
4
+ data.tar.gz: fd5c1f34b2591273575dfc4c69241b071e8fb42b
5
5
  SHA512:
6
- metadata.gz: aa0d4f390dd840fbe9cf9202cecd6a853f9927ebefe9201bac056c76cfe968cadede3ad10adebed85a9193484f6bb8d763fb4859e97b63dc6698d8738b41278e
7
- data.tar.gz: 768ec2bdeff8ea0ddb9004fb3d10ae8c78add47da8b8784e3a7b3d74b3ed351103cd97e736cb72e3f5d98822353bbc35326ef7fe41e07026eaec07066fa4ae76
6
+ metadata.gz: 703cf0f5c0ac3ca1b56461b1b2090234a243340c541df2e9c634b26c926c67cde2030e594f2ce7f7623e1c5f5535d1bc7976062a886d696457c599b4692aa9ae
7
+ data.tar.gz: 634e47e0524324d442fa601bb9a5578a489d0d74a04b00d3e7cb8c2530acadce7b36e6a61e31d040f09cae6dccb731505fa1e479a8feae5825d312345eb09a3e
@@ -1,52 +1,27 @@
1
1
  ["httparty", "json"].each(&method(:require))
2
2
 
3
- # @author Charles Hollenbeck
4
3
  class TinCan
5
4
  include HTTParty
6
5
  base_uri "apps.tincan.me"
6
+ attr_accessor :id, :key, :name
7
7
 
8
- # Initializes the starting variables to connect to the TinCan Storage API
9
- #
10
- # @param app_id [String] The ID for your TinCan Storage application
11
- # @param app_key [String] The key for your TinCan Storage application
12
- # @param app_name [String] The name for your TinCan Storage application
13
- #
14
- # @example TinCan.new("5e6a7e38c97b", "81aca0b3a200dd52bda8bca268ee68a8", "example")
15
8
  def initialize(app_id, app_key, app_name)
16
- # Initialize the starting variables to connect to the TinCan Storage API
17
- @id = app_id
18
- @key = app_key
19
- @name = app_name
9
+ @id, @key, @name = app_id, app_key, app_name
20
10
  end
21
11
 
22
- # Inserts data into a TinCan Storage Application
23
- # @param query [Hash] A hash of the query you wish to send to the TinCan Storage API
24
- # @example var.insert({:key => "value", :sec_key => "value"})
25
- # @return [Boolean, String] True or the error
26
- def insert(query) return read_response(req("insert", query.to_json)) end
12
+ [:insert, :find, :update, :remove].each do |method|
13
+ define_method method do |query|
14
+ case query
15
+ when Hash
16
+ return read_response(req(method, query.to_json))
17
+ when String
18
+ return read_response(req(method, query)) # Assume it's JSON
19
+ else
20
+ raise "Query needs to either be a hash or a JSON string"
21
+ end
22
+ end
23
+ end
27
24
 
28
- # Finds data in a TinCan Storage Application
29
- # @param query [Hash] A hash of the query you wish to send to the TinCan Storage API
30
- # @example var.find({:query => {:key => "value"}, :options => {}}) Options are listed at http://apps.tincan.me/#manipulating
31
- # @return [String] The returned data from the query or an error
32
- def find(query) return read_response(req("find", query.to_json), true) end
33
-
34
- # Updates data in a TinCan Storage Application
35
- # @param query [Hash] A hash of the query you wish to send to the TinCan Storage API
36
- # @example var.find({:query => {:key => "value"}, :data => {"$inc" => {:age => 1}}}) Options are listed at http://apps.tincan.me/#manipulating
37
- # @return [Boolean, String] True or the error
38
- def update(query) return read_response(req("update", query.to_json)) end
39
-
40
- # Deletes data from a TinCan Storage Application
41
- # @param query [Hash] A hash of the query you wish to send to the TinCan Storage API
42
- # @example var.delete({:query => {:key => "value", :sec_key => "value"}, :options => {:drop => true}})
43
- # @note As a safeguard, an empty query will NOT delete all documents. To erase ALL data in your application's database, you can drop it by using the query {:options => {:drop => true}}. You don't need to supply a query attribute, as it will be ignored if this option is present.
44
- # @return [Boolean, String] True or the error
45
- def delete(query) return read_response(req("remove", query.to_json)) end
46
-
47
- # Validates your credentials with the TinCan Storage API
48
- # @example var.validate
49
- # @return [Boolean, String] True if it worked, an error otherwise. Errors can be: "NO SUCH APP" or "INVALID_CREDENTIALS"
50
25
  def validate
51
26
  return read_response(
52
27
  self.class.get(
@@ -58,10 +33,6 @@ class TinCan
58
33
  end
59
34
 
60
35
  private
61
- # Makes requests using HTTParty to the TinCan Storage API
62
- # @param type [String] The type of query being made (insert|find|update|remove)
63
- # @param query [String] The JSON Document to send to the TinCan Storage API
64
- # @return [String] The response's body
65
36
  def req(type, query)
66
37
  return self.class.post(
67
38
  "/#{@name}/#{type}",
@@ -71,17 +42,8 @@ class TinCan
71
42
  ).body
72
43
  end
73
44
 
74
- # Interprets the response from the TinCan Storage API
75
- # @param res [String] The response body from the request
76
- # @param has_data [Boolean] Optional parameter to tell it wether it has data being sent back in the response
77
- # @return [Boolean, String] True if successful, a ruby hash of data if output has data or an error
78
45
  def read_response(res, has_data = false)
79
46
  res = JSON.parse(res)
80
-
81
- if res["success"]
82
- return res_data if has_data
83
- return true
84
- end
85
- return res["error"]
47
+ return res["data"] || res["success"] || res["error"]
86
48
  end
87
49
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tincan-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charles Hollenbeck