zunnit 0.0.3 → 0.0.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.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- zunnit (0.0.2)
4
+ zunnit (0.0.3)
5
5
  faraday
6
6
 
7
7
  GEM
@@ -12,7 +12,7 @@ GEM
12
12
  faraday (0.7.2)
13
13
  addressable (~> 2.2.6)
14
14
  multipart-post (~> 1.1.0)
15
- rack (>= 1.1.0, < 2)
15
+ rack (< 2, >= 1.1.0)
16
16
  multipart-post (1.1.2)
17
17
  rack (1.3.0)
18
18
  rspec (2.6.0)
data/lib/zunnit/api.rb ADDED
@@ -0,0 +1,57 @@
1
+ module Zunnit
2
+ class Api
3
+ attr_accessor :client, :key, :version
4
+
5
+ def initialize
6
+ self.client = Zunnit.client
7
+ self.key = Zunnit.key
8
+ self.version = Zunnit.version
9
+
10
+ yield self if block_given?
11
+ end
12
+
13
+ # Connection object
14
+ def connection
15
+ @connection ||= Faraday.new(:url => "#{Zunnit::URL}#{self.client}")
16
+ end
17
+ private :connection
18
+
19
+ def params_for(options={})
20
+ return {
21
+ :api_key => self.key
22
+ }.merge(options)
23
+ end
24
+
25
+ # Make a request to Zunnit's API
26
+ def request(http_method, action, options={})
27
+ # Raise error if the actions is invalid
28
+ unless Zunnit.actions[action]
29
+ raise "Invalid action \"#{action}\""
30
+ end
31
+
32
+ # Raise error if the http method is invalid
33
+ unless [:get, :post].include? http_method
34
+ raise "Invalid http_method \"#{http_method}\""
35
+ end
36
+
37
+ # Make the Request
38
+ action_url = Zunnit.actions[action]
39
+ response = connection.send(http_method) do |req|
40
+ req.url(action_url)
41
+ req.params = params_for options
42
+ end
43
+
44
+ response
45
+ end
46
+
47
+ # Make a GET request to Zunnit's API
48
+ def get(action, options={})
49
+ self.request(:get, action, options)
50
+ end
51
+
52
+ # Make a POST request to Zunnit's API
53
+ def post(action, options={})
54
+ self.request(:post, action, options)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,25 @@
1
+ module Zunnit
2
+ extend Utils
3
+ mattr_accessor :client, :key, :version, :actions
4
+
5
+ # Fixed constants
6
+ URL = "http://api.zunnit.com/"
7
+ ACTIONS = {
8
+ :related_items => "/related/items",
9
+ :recommendation_items_for_user => "/recommendation/items/for_user",
10
+ :recommendation_groups_for_user => "/recommendation/groups/for_user",
11
+ :recommendation_users_for_user => "/recommendation/users/for_user",
12
+ :recommendation_users_for_item => "/recommendation/users/for_item",
13
+ :recommendation_tags_for_item => "/recommendation/tags/for_item",
14
+ :recommendation_cluster_for_item => "/recommendation/cluster/for_item",
15
+ :action_items_add => "/action/items/add",
16
+ :action_items_rate => "/action/items/rate",
17
+ :action_items_view => "/action/items/view",
18
+ :action_user_follow => "/action/user/follow"
19
+ }
20
+
21
+ def self.setup
22
+ self.actions = ACTIONS
23
+ yield self
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ module Zunnit::Utils
2
+ def mattr_accessor *attrs
3
+ attrs.each do |attr|
4
+ module_eval %[
5
+ def self.#{attr.to_s}=(value)
6
+ @#{attr.to_s} = value
7
+ end
8
+
9
+ def self.#{attr.to_s}
10
+ @#{attr.to_s}
11
+ end
12
+ ]
13
+ end
14
+ end
15
+ end
@@ -1,4 +1,4 @@
1
1
  module Zunnit
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
4
4
 
data/lib/zunnit.rb CHANGED
@@ -5,5 +5,7 @@ require "faraday"
5
5
 
6
6
  # Local Libs
7
7
  $:.unshift(File.dirname(__FILE__) + '/../../lib')
8
- require 'zunnit/zunnit'
8
+ require 'zunnit/utils'
9
+ require 'zunnit/setup'
10
+ require 'zunnit/api'
9
11
 
data/spec/spec_helper.rb CHANGED
@@ -7,3 +7,9 @@ require 'zunnit'
7
7
  RSpec.configure do |config|
8
8
  config.fail_fast = true
9
9
  end
10
+
11
+ Zunnit.setup do |z|
12
+ z.client = "busk"
13
+ z.key = "busk"
14
+ z.version = "1.0"
15
+ end
data/spec/zunnit_spec.rb CHANGED
@@ -2,18 +2,13 @@
2
2
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
3
 
4
4
  describe Zunnit do
5
-
6
- describe "url_for" do
7
- it "return url for related item" do
8
- url = Zunnit.url_for(:related_items)
9
- url.should == "http://api.zunnit.com/busk/related/items?api_key=busk"
10
- end
11
- end
5
+ # let(zunnit) { Zunnit::Api.new }
12
6
 
13
7
  describe "get" do
14
8
  it "should return related items" do
15
- response = Zunnit.get(:related_items, {:item_id=>"20275919"})
16
- response.status.should == 200
9
+ zunnit = Zunnit::Api.new
10
+ response = zunnit.get(:related_items, {:item_id=>"20275919"})
11
+ response.should == 200
17
12
  end
18
13
  end
19
14
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: zunnit
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.3
5
+ version: 0.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Marcelo Eden
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-06-20 00:00:00 -03:00
13
+ date: 2011-06-21 00:00:00 -03:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -52,8 +52,10 @@ files:
52
52
  - README
53
53
  - Rakefile
54
54
  - lib/zunnit.rb
55
+ - lib/zunnit/api.rb
56
+ - lib/zunnit/setup.rb
57
+ - lib/zunnit/utils.rb
55
58
  - lib/zunnit/version.rb
56
- - lib/zunnit/zunnit.rb
57
59
  - spec/spec_helper.rb
58
60
  - spec/zunnit_spec.rb
59
61
  - zunnit.gemspec
data/lib/zunnit/zunnit.rb DELETED
@@ -1,58 +0,0 @@
1
- module Zunnit
2
- # Fixed constants
3
- API_URL = "http://api.zunnit.com/"
4
- API_ACTIONS = {
5
- :related_items => "/related/items",
6
- :recommendation_items_for_user => "/recommendation/items/for_user",
7
- :recommendation_groups_for_user => "/recommendation/groups/for_user",
8
- :recommendation_users_for_user => "/recommendation/users/for_user",
9
- :recommendation_users_for_item => "/recommendation/users/for_item",
10
- :recommendation_tags_for_item => "/recommendation/tags/for_item",
11
- :recommendation_cluster_for_item => "/recommendation/cluster/for_item",
12
- :action_items_add => "/action/items/add",
13
- :action_items_rate => "/action/items/rate",
14
- :action_items_view => "/action/items/view",
15
- :action_user_follow => "/action/user/follow"
16
- }
17
-
18
- # Configurable constants
19
- API_CLIENT = "busk"
20
- API_KEY = "busk"
21
- API_VERSION = "1.0"
22
-
23
- class << self
24
- # API URL for action
25
- def url_for(action)
26
- raise "Invalid action '#{action}'" unless API_ACTIONS[action]
27
-
28
- # Generate the URL
29
- url = "#{API_URL}#{API_CLIENT}#{API_ACTIONS[action]}?api_key=#{API_KEY}"
30
- url
31
- end
32
-
33
- # Make a request to Zunnit's API
34
- def request(http_method, action, options={})
35
- connection = Faraday.new(:url => self.url_for(action))
36
- response = case http_method.to_sym
37
- when :get
38
- connection.get { |req| req.params = options }
39
- when :post
40
- connection.post { |req| req.params = options }
41
- else
42
- raise "Invalid http_method \"#{http_method}\""
43
- end
44
-
45
- response
46
- end
47
-
48
- # Make a GET request to Zunnit's API
49
- def get(action, options={})
50
- self.request(:get, action, options)
51
- end
52
-
53
- # Make a POST request to Zunnit's API
54
- def post(action, options={})
55
- self.request(:post, action, options)
56
- end
57
- end
58
- end