remote_api 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest.txt +0 -2
- data/README.txt +40 -2
- data/Rakefile +1 -1
- data/lib/remote_api/base.rb +24 -11
- data/lib/remote_api/version.rb +1 -1
- data/lib/remote_api.rb +4 -4
- metadata +21 -4
- data/lib/dsl_accessor.rb +0 -50
data/Manifest.txt
CHANGED
data/README.txt
CHANGED
@@ -1,3 +1,41 @@
|
|
1
|
-
|
2
|
-
=====================
|
1
|
+
Simple Readme for now.
|
3
2
|
|
3
|
+
Rubyforge Project: http://rubyforge.org/projects/remote-api/
|
4
|
+
|
5
|
+
=== Installation
|
6
|
+
|
7
|
+
gem install remote_api
|
8
|
+
|
9
|
+
=== Example
|
10
|
+
|
11
|
+
This is an example of how to access a little API that allows you to change the title of an object.
|
12
|
+
The API would return the title, but in Title Case, and we want to capture that.
|
13
|
+
|
14
|
+
require 'remote_api'
|
15
|
+
|
16
|
+
class MyAPI < RemoteAPI
|
17
|
+
|
18
|
+
attr_reader :title
|
19
|
+
|
20
|
+
url 'http://test.com/posts/change_title'
|
21
|
+
|
22
|
+
def request
|
23
|
+
"title=#{@title}&post_id={@post_id}"
|
24
|
+
end
|
25
|
+
|
26
|
+
def assert_success
|
27
|
+
if @response =~ /&?error=(.+?)&?/
|
28
|
+
raise ResponseFailure, $1
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def process
|
33
|
+
@title = @response.match(/&?title=(.+?)&?/)[1]
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
result = MyAPI.new(:post_id => 123, :title => 'hello world')
|
39
|
+
# API call happens here
|
40
|
+
|
41
|
+
result.title #=> 'Hello World'
|
data/Rakefile
CHANGED
@@ -44,6 +44,6 @@ hoe = Hoe.new(GEM_NAME, VERS) do |p|
|
|
44
44
|
|
45
45
|
# == Optional
|
46
46
|
#p.changes - A description of the release's latest changes.
|
47
|
-
p.extra_deps =
|
47
|
+
p.extra_deps = [['activesupport'], ['dsl_accessor']]
|
48
48
|
#p.spec_extras - A hash of extra values to set in the gemspec.
|
49
49
|
end
|
data/lib/remote_api/base.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
class RemoteAPI
|
2
2
|
class ResponseFailure < RuntimeError; end
|
3
3
|
|
4
|
-
dsl_accessor :debug
|
4
|
+
dsl_accessor :debug
|
5
|
+
dsl_accessor :log_file
|
5
6
|
dsl_accessor :url
|
6
7
|
dsl_accessor :content_type
|
7
8
|
|
@@ -57,12 +58,12 @@ class RemoteAPI
|
|
57
58
|
true
|
58
59
|
end
|
59
60
|
|
60
|
-
def process
|
61
|
+
def process
|
61
62
|
raise 'You must define a "process" method! This should should parse the @response and' +
|
62
63
|
'extract relevant data.'
|
63
64
|
end
|
64
65
|
|
65
|
-
def request
|
66
|
+
def request
|
66
67
|
raise 'You must define a "request" method! This should create the body of your API request'
|
67
68
|
end
|
68
69
|
|
@@ -88,33 +89,45 @@ class RemoteAPI
|
|
88
89
|
def format_response(response)
|
89
90
|
response
|
90
91
|
end
|
92
|
+
|
93
|
+
def log(data)
|
94
|
+
data = "\n\n\n\n\n*** #{Time.now}\n\n" + data
|
95
|
+
if file = self.class.log_file
|
96
|
+
File.open(file, 'a') { |f| f.write data }
|
97
|
+
end
|
98
|
+
end
|
91
99
|
|
92
100
|
def debug_request(request_body)
|
93
|
-
return unless self.class.debug
|
94
|
-
|
101
|
+
return unless self.class.debug || self.class.log_file
|
102
|
+
text = <<DEBUG
|
95
103
|
|
96
104
|
-----------
|
97
105
|
- REQUEST -
|
98
106
|
-----------
|
99
107
|
|
100
108
|
#{request_body}
|
101
|
-
DEBUG
|
109
|
+
DEBUG
|
110
|
+
puts text if self.class.debug
|
111
|
+
log(text) if self.class.log_file
|
102
112
|
end
|
103
113
|
|
104
114
|
def debug_response(response, url)
|
105
|
-
return unless self.class.debug
|
115
|
+
return unless self.class.debug || self.class.log_file
|
106
116
|
|
107
|
-
|
117
|
+
text = format_response(response)
|
108
118
|
|
109
|
-
|
119
|
+
text = <<DEBUG
|
110
120
|
|
111
121
|
------------"
|
112
122
|
- RESPONSE -"
|
113
123
|
------------"
|
114
124
|
--- From: #{url}"
|
115
125
|
|
116
|
-
#{
|
126
|
+
#{text}
|
117
127
|
|
118
|
-
DEBUG
|
128
|
+
DEBUG
|
129
|
+
|
130
|
+
puts text if self.class.debug
|
131
|
+
log(text) if self.class.log_file
|
119
132
|
end
|
120
133
|
end
|
data/lib/remote_api/version.rb
CHANGED
data/lib/remote_api.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
+
# Standard Libraries
|
1
2
|
require 'net/https'
|
2
3
|
require 'rexml/document'
|
3
4
|
require 'uri'
|
4
5
|
|
6
|
+
# Gems
|
5
7
|
require 'active_support'
|
8
|
+
require 'dsl_accessor'
|
6
9
|
|
7
|
-
#
|
8
|
-
require File.join(File.dirname(__FILE__), 'dsl_accessor')
|
9
|
-
|
10
|
-
# remote_api files
|
10
|
+
# remote_api
|
11
11
|
require 'remote_api/base'
|
12
12
|
require 'remote_api/xml'
|
13
13
|
require 'remote_api/xml_response'
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.
|
2
|
+
rubygems_version: 0.9.1
|
3
3
|
specification_version: 1
|
4
4
|
name: remote_api
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date: 2007-01-
|
6
|
+
version: 0.1.2
|
7
|
+
date: 2007-01-17 00:00:00 -08:00
|
8
8
|
summary: Provides a basic framework for easily creating classes that access remote APIs.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -34,7 +34,6 @@ files:
|
|
34
34
|
- README.txt
|
35
35
|
- Rakefile
|
36
36
|
- setup.rb
|
37
|
-
- lib/dsl_accessor.rb
|
38
37
|
- lib/remote_api.rb
|
39
38
|
- lib/remote_api/base.rb
|
40
39
|
- lib/remote_api/xml.rb
|
@@ -68,3 +67,21 @@ dependencies:
|
|
68
67
|
- !ruby/object:Gem::Version
|
69
68
|
version: 0.0.0
|
70
69
|
version:
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: dsl_accessor
|
72
|
+
version_requirement:
|
73
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">"
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 0.0.0
|
78
|
+
version:
|
79
|
+
- !ruby/object:Gem::Dependency
|
80
|
+
name: hoe
|
81
|
+
version_requirement:
|
82
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: 1.1.7
|
87
|
+
version:
|
data/lib/dsl_accessor.rb
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
class Class
|
2
|
-
def dsl_accessor(name, options = {})
|
3
|
-
raise TypeError, "DSL Error: options should be a hash. but got `#{options.class}'" unless options.is_a?(Hash)
|
4
|
-
writer = options[:writer] || options[:setter]
|
5
|
-
writer =
|
6
|
-
case writer
|
7
|
-
when NilClass then Proc.new{|value| value}
|
8
|
-
when Symbol then Proc.new{|value| __send__(writer, value)}
|
9
|
-
when Proc then writer
|
10
|
-
else raise TypeError, "DSL Error: writer should be a symbol or proc. but got `#{options[:writer].class}'"
|
11
|
-
end
|
12
|
-
write_inheritable_attribute(:"#{name}_writer", writer)
|
13
|
-
|
14
|
-
default =
|
15
|
-
case options[:default]
|
16
|
-
when NilClass then nil
|
17
|
-
when [] then Proc.new{[]}
|
18
|
-
when {} then Proc.new{{}}
|
19
|
-
when Symbol then Proc.new{__send__(options[:default])}
|
20
|
-
when Proc then options[:default]
|
21
|
-
else Proc.new{options[:default]}
|
22
|
-
end
|
23
|
-
write_inheritable_attribute(:"#{name}_default", default)
|
24
|
-
|
25
|
-
self.class.class_eval do
|
26
|
-
define_method("#{name}=") do |value|
|
27
|
-
writer = read_inheritable_attribute(:"#{name}_writer")
|
28
|
-
value = writer.call(value) if writer
|
29
|
-
write_inheritable_attribute(:"#{name}", value)
|
30
|
-
end
|
31
|
-
|
32
|
-
define_method(name) do |*values|
|
33
|
-
if values.empty?
|
34
|
-
# getter method
|
35
|
-
key = :"#{name}"
|
36
|
-
if !inheritable_attributes.has_key?(key)
|
37
|
-
default = read_inheritable_attribute(:"#{name}_default")
|
38
|
-
value = default ? default.call(self) : nil
|
39
|
-
__send__("#{name}=", value)
|
40
|
-
end
|
41
|
-
read_inheritable_attribute(key)
|
42
|
-
else
|
43
|
-
# setter method
|
44
|
-
__send__("#{name}=", *values)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|