tumbz 0.0.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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+ gem "her", :git => "git://github.com/remiprev/her.git", :branch => "master"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Rémi Prévost
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # Tumbz
2
+
3
+ This gem allows you to easily use the [Tum.bz API](http://tum.bz/api). It’s powered by [Her](https://github.com/remiprev/her).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'tumbz'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install tumbz
20
+
21
+ ## Usage
22
+
23
+ First, you must define a `configure` block with your API key:
24
+
25
+ ```ruby
26
+ Tumbz.configure do |config|
27
+ config.api_key = "nzaEhGbo4B9yAOn1GKveoSL003sexY9F"
28
+ end
29
+ ```
30
+
31
+ That’s it! You’ll then be able to use :
32
+
33
+ ```ruby
34
+ Tumbz::User.find("remi")
35
+ # => #<Tumbz::User(users/4f0e32936edcb2000100029d) id="4f0e32936edcb2000100029d" username="remi" profile_url="http://tum.bz/u/remi" firstname="Rémi" lastname="Prévost"…>
36
+ ```
37
+
38
+ Other modules are:
39
+
40
+ * `Tumbz::Comment`
41
+ * `Tumbz::Like`
42
+ * `Tumbz::Review`
43
+ * `Tumbz::PartnerLookup`
44
+ * `Tumbz::Product`
45
+ * `Tumbz::User`
46
+
47
+ The API wrapper is powered by [Her](https://github.com/remiprev/her), so most of [its documentation](https://github.com/remiprev/her) will be helpful.
48
+
49
+ ## OAuth
50
+
51
+ Support for OAuth-authenticated calls is supported, but very premitive (not quite thread-safe). Here’s how it works:
52
+
53
+ ```ruby
54
+ Tumbz::User.sign_in!("<email>", "<password>")
55
+ # => true (next calls will be made as the authenticated user)
56
+
57
+ Tumbz::Review.create(:product_external_id => "tt0458339", :positive => "1", :cat => "movie")
58
+ # => #<Tumbz::Review(reviews/50b9ebd7a9d29c000200af7c) id="50b9ebd7a9d29c000200af7c" positive=true text=""…>
59
+
60
+ Tumbz::User.sign_out!
61
+ ```
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require "bundler/gem_tasks"
2
+ require "tumbz"
3
+
4
+ task :demo do
5
+ Tumbz.configure do |config|
6
+ config.api_key = "nzaEhGbo4B9yAOn1GKveoSL003sexY9F"
7
+ end
8
+
9
+ p Tumbz::User.find("remi")
10
+ #p Tumbz::User.get(:search, :q => "bass")
11
+ #p Tumbz::Product.get(:search, :q => "office", :cat => "tv")
12
+ end
13
+
14
+ task :oauth do
15
+ Tumbz.configure do |config|
16
+ config.api_key = "nzaEhGbo4B9yAOn1GKveoSL003sexY9F"
17
+ end
18
+
19
+ #p Tumbz::User.sign_in!("", "",)
20
+ #p Tumbz::User.sign_out!
21
+ #p Tumbz::Review.create(:product_external_id => "tt0458339", :positive => "1", :cat => "movie")
22
+ end
@@ -0,0 +1,6 @@
1
+ module Tumbz
2
+ class Comment
3
+ include Her::Model
4
+ uses_api Tumbz.api
5
+ end
6
+ end
@@ -0,0 +1,4 @@
1
+ module Tumbz
2
+ class Config
3
+ end
4
+ end
data/lib/tumbz/like.rb ADDED
@@ -0,0 +1,6 @@
1
+ module Tumbz
2
+ class Like
3
+ include Her::Model
4
+ uses_api Tumbz.api
5
+ end
6
+ end
@@ -0,0 +1,15 @@
1
+ module Tumbz
2
+ module Middleware
3
+ class ApiKey < Faraday::Middleware
4
+ def initialize(app, options={})
5
+ @app = app
6
+ @options = options
7
+ end
8
+
9
+ def call(env)
10
+ env[:url].query = env[:url].query ? "#{env[:url].query}&apikey=#{@options[:api_key]}" : "apikey=#{@options[:api_key]}"
11
+ @app.call(env)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Tumbz
2
+ module Middleware
3
+ class Auth < Faraday::Middleware
4
+ def initialize(app, options={})
5
+ @app = app
6
+ @options = options
7
+ end
8
+
9
+ def call(env)
10
+ env[:request_headers]["Authorization"] = "OAuth #{Tumbz.access_token}" unless Tumbz.access_token.nil?
11
+ @app.call(env)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,36 @@
1
+ module Tumbz
2
+ module Middleware
3
+ class Parse < Faraday::Response::Middleware
4
+ # Parse the response body
5
+ #
6
+ # @param [String] body The response body
7
+ # @return [Mixed] the parsed response
8
+ def parse(body)
9
+ json = MultiJson.load(body, :symbolize_keys => true)
10
+ errors = []
11
+ metadata = json.delete(:metadata) || []
12
+
13
+ if json[:error].present?
14
+ json.delete(:error)
15
+ json.delete(:message)
16
+ errors = [json.delete(:developerMessage)]
17
+ end
18
+
19
+ {
20
+ :data => json,
21
+ :errors => errors,
22
+ :metadata => metadata
23
+ }
24
+ end
25
+
26
+ # This method is triggered when the response has been received. It modifies
27
+ # the value of `env[:body]`.
28
+ #
29
+ # @param [Hash] env The response environment
30
+ def on_complete(env)
31
+ env[:body] = parse(env[:body])
32
+ end
33
+ end
34
+ end
35
+ end
36
+
@@ -0,0 +1,7 @@
1
+ module Tumbz
2
+ module Model
3
+ def error?
4
+ self.respond_to?(:error) and !self.error.nil? and self.error != ""
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ module Tumbz
2
+ class PartnerLookup
3
+ include Her::Model
4
+ uses_api Tumbz.api
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Tumbz
2
+ class Product
3
+ include Her::Model
4
+ uses_api Tumbz.api
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module Tumbz
2
+ class Review
3
+ include Her::Model
4
+ uses_api Tumbz.api
5
+ end
6
+ end
data/lib/tumbz/user.rb ADDED
@@ -0,0 +1,19 @@
1
+ module Tumbz
2
+ class User
3
+ include Model
4
+ include Her::Model
5
+ uses_api Tumbz.api
6
+
7
+ def self.sign_in!(email, password)
8
+ post_raw("auth", :email => email, :password => password) do |parsed_data|
9
+ Tumbz.access_token = parsed_data[:data][:access_token]
10
+ end
11
+
12
+ Tumbz.access_token.nil? ? false : true
13
+ end
14
+
15
+ def self.sign_out!
16
+ Tumbz.access_token = nil
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Tumbz
2
+ VERSION = "0.0.1"
3
+ end
data/lib/tumbz.rb ADDED
@@ -0,0 +1,46 @@
1
+ # Dependencies
2
+ require "bundler"
3
+ Bundler.require
4
+
5
+ # Modules
6
+ require "tumbz/version"
7
+ require "tumbz/model"
8
+ require "tumbz/config"
9
+ require "tumbz/middleware/parse"
10
+ require "tumbz/middleware/api_key"
11
+ require "tumbz/middleware/auth"
12
+
13
+ module Tumbz
14
+ def self.api
15
+ @api
16
+ end
17
+
18
+ def self.access_token=(token)
19
+ @access_token = token
20
+ end
21
+
22
+ def self.access_token
23
+ @access_token
24
+ end
25
+
26
+ def self.configure(&blk)
27
+ options = OpenStruct.new
28
+ yield(options)
29
+
30
+ @api = Her::API.new
31
+ @api.setup :url => "http://api.tum.bz/v1/" do |connection|
32
+ connection.use Tumbz::Middleware::ApiKey, :api_key => options.api_key
33
+ connection.use Tumbz::Middleware::Auth
34
+ connection.use Faraday::Request::UrlEncoded
35
+ connection.use Tumbz::Middleware::Parse
36
+ connection.use Faraday::Adapter::NetHttp
37
+ end
38
+
39
+ require "tumbz/comment"
40
+ require "tumbz/like"
41
+ require "tumbz/review"
42
+ require "tumbz/partner_lookup"
43
+ require "tumbz/product"
44
+ require "tumbz/user"
45
+ end
46
+ end
data/tumbz.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tumbz/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "tumbz"
8
+ gem.version = Tumbz::VERSION
9
+ gem.authors = ["Rémi Prévost"]
10
+ gem.email = ["remi@exomel.com"]
11
+ gem.description = %q{Ruby wrapper for the Tum.bz API}
12
+ gem.summary = %q{Ruby wrapper for the Tum.bz API}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency "her"
21
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tumbz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rémi Prévost
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: her
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Ruby wrapper for the Tum.bz API
31
+ email:
32
+ - remi@exomel.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/tumbz.rb
43
+ - lib/tumbz/comment.rb
44
+ - lib/tumbz/config.rb
45
+ - lib/tumbz/like.rb
46
+ - lib/tumbz/middleware/api_key.rb
47
+ - lib/tumbz/middleware/auth.rb
48
+ - lib/tumbz/middleware/parse.rb
49
+ - lib/tumbz/model.rb
50
+ - lib/tumbz/partner_lookup.rb
51
+ - lib/tumbz/product.rb
52
+ - lib/tumbz/review.rb
53
+ - lib/tumbz/user.rb
54
+ - lib/tumbz/version.rb
55
+ - tumbz.gemspec
56
+ homepage: ''
57
+ licenses: []
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ segments:
69
+ - 0
70
+ hash: -3848226679834362904
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ segments:
78
+ - 0
79
+ hash: -3848226679834362904
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.23
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Ruby wrapper for the Tum.bz API
86
+ test_files: []