musashi 0.0.1.b6 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -56,7 +56,7 @@ Musashi is a RestClient that follows relations or links.
56
56
 
57
57
  require 'musashi'
58
58
  class Customer
59
- include Musashi::Retriever
59
+ include Musashi
60
60
  include Musashi::Resource::JSON
61
61
  attr_accessor :id
62
62
  endpoint = '"http://foo.com/customers/#{id}"'
data/Rakefile CHANGED
@@ -11,11 +11,20 @@ RSpec::Core::RakeTask.new(:spec)
11
11
 
12
12
  desc 'Run the gem build'
13
13
  task :build do
14
- system 'gem build musashi.gemspec'
14
+ system "mkdir -p build && gem build musashi.gemspec && mv musashi-#{Musashi::VERSION}.gem build/."
15
15
  end
16
-
17
- desc 'Run the gem push'
18
- task :release => [:spec, :build] do
16
+
17
+ desc "Create tag v#{Musashi::VERSION} and build and push musashi-#{Musashi::VERSION}.gem to Rubygems"
18
+ task :release => :build do
19
+ unless `git branch` =~ /^\* master$/
20
+ puts "You must be on the master branch to release!"
21
+ exit!
22
+ end
23
+ system "git commit --allow-empty -a -m 'Release #{Musashi::VERSION}'"
24
+ system "git tag v#{Musashi::VERSION}"
25
+ system "git push origin master"
26
+ system "git push origin v#{Musashi::VERSION}"
27
+ system "gem push build/musashi-#{Musashi::VERSION}.gem"
19
28
  system "gem push musashi-#{Musashi::VERSION}.gem"
20
29
  end
21
30
 
@@ -1,4 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
+ require 'active_support/core_ext/object/to_query'
2
3
  require 'active_support/core_ext/integer'
3
4
  require 'active_support/core_ext/string'
4
5
  require 'active_support/core_ext/hash'
@@ -11,6 +12,16 @@ require 'faraday/response/raise_http_4xx'
11
12
  require 'faraday/response/raise_http_5xx'
12
13
  require 'musashi/version'
13
14
  require 'musashi/connection'
15
+ require 'musashi/publisher'
14
16
  require 'musashi/retriever'
15
17
  require 'musashi/resource'
16
18
 
19
+ module Musashi
20
+ include Publisher
21
+ include Retriever
22
+ end
23
+
24
+ class Musashi::Resource
25
+ include ::Musashi
26
+ end
27
+
@@ -1,8 +1,20 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  module Musashi::Connection
3
3
 
4
- attr_accessor :endpoint
5
- attr_accessor_with_default :format, :json
4
+ attr_writer :endpoint
5
+ def endpoint
6
+ raise 'Undefined endpoint' if @endpoint.nil?
7
+ @endpoint
8
+ end
9
+
10
+ attr_writer :format
11
+ def format
12
+ @format ||= :json
13
+ end
14
+
15
+ def accept
16
+ @accept ||= "application/#{format.to_s}"
17
+ end
6
18
 
7
19
  def options
8
20
  return @options if @options
@@ -22,7 +34,7 @@ module Musashi::Connection
22
34
  builder.adapter :net_http
23
35
  @options = {
24
36
  :headers => {
25
- 'Accept' => "application/#{format}",
37
+ 'Accept' => accept,
26
38
  'User-Agent' => "Musashi Gem #{::Musashi::VERSION}"
27
39
  },
28
40
  :url => endpoint,
@@ -0,0 +1,39 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module Musashi::Publisher
3
+
4
+ def data
5
+ @data ||= {}
6
+ end
7
+
8
+ def method_missing(sym,*args,&block)
9
+ super if block_given?
10
+ match = /(?<field>.+)=$/.match(sym.to_s)
11
+ super if match.nil? or match.size != 2
12
+ field = match[:field]
13
+ attrs[field] = data[field] = args.first
14
+ end
15
+
16
+ def post(headers={})
17
+ marshall = ::Musashi::Publisher.marshalls[format] || WWWFormUrlencoded
18
+ payload, content_type = marshall.marshall(data)
19
+ headers = {} if headers.nil?
20
+ headers['Content-Type'] = content_type
21
+ connection.post(connection.path_prefix, payload, headers)
22
+ end
23
+
24
+ def self.marshalls
25
+ @@marashalls ||= {}
26
+ end
27
+
28
+ def self.marshall(format,class_or_module)
29
+ marshalls[format] = class_or_module
30
+ end
31
+
32
+ module WWWFormUrlencoded
33
+ def self.marshall(data)
34
+ [data.to_query, 'www-form-urlencoded']
35
+ end
36
+ end
37
+
38
+ end
39
+
@@ -49,7 +49,6 @@ class Musashi::Resource
49
49
 
50
50
  end
51
51
 
52
- include ::Musashi::Retriever
53
52
  def initialize(url,options={})
54
53
  raise ArgumentError.new('Undefined url') if url.nil? or url.empty?
55
54
  resource_type = Format.formats[ options[:format] || :json ]
@@ -4,17 +4,12 @@ module Musashi::Retriever
4
4
  module Base
5
5
  include ::Musashi::Connection
6
6
 
7
- attr_writer :path
8
- def path
9
- @path ||= connection.path_prefix
10
- end
11
-
12
7
  def retrieved?
13
8
  @retrieved ||= false
14
9
  end
15
10
 
16
11
  def retrieve(header={})
17
- response = connection.get(path, header)
12
+ response = connection.get(connection.path_prefix, header)
18
13
  @retrieved = true
19
14
  response.body.extend(VisitorStrategist)
20
15
  end
@@ -127,31 +122,31 @@ module Musashi::Retriever
127
122
  module DelegateHash
128
123
  include VisitorStrategist
129
124
 
130
- attr_accessor :attrs
131
-
132
125
  def [](key)
133
- @attrs = retrieve if @attrs.nil?
126
+ attrs
134
127
  super
135
128
  end
136
129
 
137
130
  def method_missing(sym,*args,&block)
138
- @attrs = retrieve if @attrs.nil?
131
+ attrs
139
132
  super
140
133
  end
141
134
 
142
135
  def keys
143
- @attrs = retrieve if @attrs.nil?
144
- @attrs.keys
136
+ attrs.keys
145
137
  end
146
138
 
147
139
  def key?(key)
148
- @attrs = retrieve if @attrs.nil?
149
- @attrs.key?(key)
140
+ attrs.key?(key)
150
141
  end
151
142
 
152
143
  def fetch(*args)
153
- @attrs = retrieve if @attrs.nil?
154
- @attrs.fetch(*args)
144
+ attrs.fetch(*args)
145
+ end
146
+
147
+ def attrs
148
+ @attrs = retrieve unless retrieved?
149
+ @attrs
155
150
  end
156
151
 
157
152
  end
@@ -186,6 +181,7 @@ module Musashi::Retriever
186
181
  return if receiver.is_a?(Iterator)
187
182
  raise RuntimeError.new unless Iterator.extend? receiver
188
183
  receiver.each{ |item| Strategist.define(item) }
184
+ super
189
185
  end
190
186
  end
191
187
 
@@ -1,4 +1,4 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  module Musashi
3
- VERSION = "0.0.1.b6"
3
+ VERSION = "0.0.1"
4
4
  end
@@ -0,0 +1,36 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe Musashi::Connection do
5
+
6
+ before(:each) do
7
+ @resource = Object.new.extend(Musashi::Connection)
8
+ end
9
+
10
+ it 'should raise an error for invalid endpoint' do
11
+ lambda{ @resource.endpoint }.should raise_error
12
+ lambda{ @resource.options }.should raise_error
13
+ lambda{ @resource.connection }.should raise_error
14
+ end
15
+
16
+ it 'should have as default format json' do
17
+ @resource.format.should == :json
18
+ end
19
+
20
+ it 'should give to us a default connection resource' do
21
+ @resource.endpoint = 'http://foo.bar/'
22
+ @resource.options[:builder].should_not be_nil
23
+ @resource.options[:headers].should_not be_nil
24
+ @resource.options[:url].should_not be_nil
25
+ @resource.options[:ssl].should_not be_nil
26
+ @resource.connection.should_not be_nil
27
+ end
28
+
29
+ it 'should perform get http verbs agaist endpoint' do
30
+ @resource.endpoint = 'http://foo.bar/'
31
+ FakeWeb.register_uri(:get, @resource.endpoint, :body => '{"test":"foo"}')
32
+ @resource.connection.get.body.test.should eq('foo')
33
+ end
34
+
35
+ end
36
+
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe Musashi::Publisher do
5
+
6
+ before(:each) do
7
+ @resource = Musashi::Resource.new('http://foo.bar/')
8
+ FakeWeb.register_uri(:get, @resource.endpoint, :body => '{"foo":"bar","name":"bazz"}')
9
+ FakeWeb.register_uri(:post, @resource.endpoint, :payload => 'name=bar')
10
+ end
11
+
12
+ it 'should known what fields are dirty' do
13
+ @resource.attrs.size.should == 2
14
+ @resource.data.should be_empty
15
+ @resource.name.should eq('bazz')
16
+ @resource.name = 'bar'
17
+ @resource.name.should eq('bar')
18
+ @resource.data.size.should == 1
19
+ end
20
+
21
+ it 'should post ony fields are dirty' do
22
+ @resource.name.should eq('bazz')
23
+ @resource.name = 'bar'
24
+ @resource.name.should eq ('bar')
25
+ @resource.post
26
+ end
27
+
28
+ end
29
+
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe Musashi::Retriever::Base do
5
+
6
+ before(:all) do
7
+ @resource = Object.new.extend(Musashi::Retriever::Base)
8
+ @resource.endpoint = 'http://foo.bar/'
9
+ FakeWeb.register_uri(:get, @resource.endpoint, :body => '{"foo":"bar"}')
10
+ end
11
+
12
+ it 'should known if it fetched data' do
13
+ @resource.retrieved?.should == false
14
+ @resource.retrieve
15
+ @resource.retrieved?.should == true
16
+ end
17
+
18
+ it 'should fetch data and marshalled body response' do
19
+ @resource.retrieve.foo.should eq('bar')
20
+ end
21
+
22
+ end
23
+
@@ -1 +1,12 @@
1
- {"entries":[{"id":"1234567890","parent":"0","service_key":"SERHOST0001","created_at":"2011-09-23","active":true,"status":"active"}]}
1
+ {
2
+ "entries" : [
3
+ {
4
+ "created_at" : "2011-09-23",
5
+ "parent" : "0",
6
+ "service_key" : "SERHOST0001",
7
+ "status" : "active",
8
+ "active" : true,
9
+ "id" : "1234567890"
10
+ }
11
+ ]
12
+ }
@@ -1,4 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  require 'rspec'
3
3
  require 'musashi'
4
+ require 'fakeweb'
4
5
 
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: musashi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.b6
5
- prerelease: 6
4
+ version: 0.0.1
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - pahagon
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-03 00:00:00.000000000Z
12
+ date: 2011-10-10 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: i18n
16
- requirement: &14474960 !ruby/object:Gem::Requirement
16
+ requirement: &9140160 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0.6'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *14474960
24
+ version_requirements: *9140160
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &14474500 !ruby/object:Gem::Requirement
27
+ requirement: &9139460 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 3.1.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *14474500
35
+ version_requirements: *9139460
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: hashie
38
- requirement: &14473840 !ruby/object:Gem::Requirement
38
+ requirement: &9138420 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.1.0
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *14473840
46
+ version_requirements: *9138420
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: faraday
49
- requirement: &14473060 !ruby/object:Gem::Requirement
49
+ requirement: &9137720 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 0.7.4
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *14473060
57
+ version_requirements: *9137720
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: faraday_middleware
60
- requirement: &14472340 !ruby/object:Gem::Requirement
60
+ requirement: &9123940 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 0.7.0
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *14472340
68
+ version_requirements: *9123940
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: multi_json
71
- requirement: &14471680 !ruby/object:Gem::Requirement
71
+ requirement: &9123480 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 1.0.0
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *14471680
79
+ version_requirements: *9123480
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: multi_xml
82
- requirement: &14471000 !ruby/object:Gem::Requirement
82
+ requirement: &9123020 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,29 +87,40 @@ dependencies:
87
87
  version: 0.4.0
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *14471000
90
+ version_requirements: *9123020
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rspec
93
- requirement: &14470300 !ruby/object:Gem::Requirement
93
+ requirement: &9122220 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
- - - ! '>='
96
+ - - =
97
97
  - !ruby/object:Gem::Version
98
- version: '0'
98
+ version: 2.6.0
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *14470300
101
+ version_requirements: *9122220
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: sinatra
104
- requirement: &14469840 !ruby/object:Gem::Requirement
104
+ requirement: &9121760 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
- - - ! '>='
107
+ - - =
108
+ - !ruby/object:Gem::Version
109
+ version: 1.2.6
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: *9121760
113
+ - !ruby/object:Gem::Dependency
114
+ name: fakeweb
115
+ requirement: &9121260 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - =
108
119
  - !ruby/object:Gem::Version
109
- version: '0'
120
+ version: 1.3.0
110
121
  type: :development
111
122
  prerelease: false
112
- version_requirements: *14469840
123
+ version_requirements: *9121260
113
124
  description: ! 'Musashi is a library for '
114
125
  email: pahagon@gmail.com
115
126
  executables: []
@@ -125,10 +136,14 @@ files:
125
136
  - lib/musashi/connection.rb
126
137
  - lib/musashi/retriever.rb
127
138
  - lib/musashi/version.rb
139
+ - lib/musashi/publisher.rb
128
140
  - lib/musashi.rb
129
141
  - lib/faraday/response/raise_http_5xx.rb
130
142
  - lib/faraday/response/raise_http_4xx.rb
143
+ - spec/connection_spec.rb
131
144
  - spec/spec_helper.rb
145
+ - spec/publisher_spec.rb
146
+ - spec/retriever_base_spec.rb
132
147
  - spec/resource_spec.rb
133
148
  - spec/sinatra/public/customers/1.json
134
149
  - spec/sinatra/public/customers/1/orders
@@ -166,7 +181,10 @@ signing_key:
166
181
  specification_version: 3
167
182
  summary: Musashi is a library for
168
183
  test_files:
184
+ - spec/connection_spec.rb
169
185
  - spec/spec_helper.rb
186
+ - spec/publisher_spec.rb
187
+ - spec/retriever_base_spec.rb
170
188
  - spec/resource_spec.rb
171
189
  - spec/sinatra/public/customers/1.json
172
190
  - spec/sinatra/public/customers/1/orders