muddyit_fu 0.2.9 → 0.2.10
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/VERSION +1 -1
- data/lib/muddyit/base.rb +102 -9
- data/muddyit_fu.gemspec +2 -2
- data/test/test_muddyit_fu.rb +8 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.10
|
data/lib/muddyit/base.rb
CHANGED
@@ -8,7 +8,7 @@ module Muddyit
|
|
8
8
|
class_attr_accessor :http_open_timeout
|
9
9
|
class_attr_accessor :http_read_timeout
|
10
10
|
attr_accessor :rest_endpoint
|
11
|
-
attr_reader :consumer_key, :consumer_secret, :access_token, :access_token_secret
|
11
|
+
attr_reader :consumer_key, :consumer_secret, :access_token, :access_token_secret, :username, :password, :auth_type
|
12
12
|
|
13
13
|
@@http_open_timeout = 120
|
14
14
|
@@http_read_timeout = 120
|
@@ -50,25 +50,34 @@ module Muddyit
|
|
50
50
|
def initialize(config_hash_or_file)
|
51
51
|
if config_hash_or_file.is_a? Hash
|
52
52
|
config_hash_or_file.nested_symbolize_keys!
|
53
|
+
@username = config_hash_or_file[:username]
|
54
|
+
@password = config_hash_or_file[:password]
|
53
55
|
@consumer_key = config_hash_or_file[:consumer_key]
|
54
56
|
@consumer_secret = config_hash_or_file[:consumer_secret]
|
55
57
|
@access_token = config_hash_or_file[:access_token]
|
56
58
|
@access_token_secret = config_hash_or_file[:access_token_secret]
|
57
59
|
@rest_endpoint = config_hash_or_file.has_key?(:rest_endpoint) ? config_hash_or_file[:rest_endpoint] : REST_ENDPOINT
|
58
|
-
raise 'config_hash must contain consumer_key and consumer_secret' unless @consumer_key and @consumer_secret
|
59
60
|
else
|
60
61
|
config = YAML.load_file(config_hash_or_file)
|
61
62
|
config.nested_symbolize_keys!
|
63
|
+
@username = config[:username]
|
64
|
+
@password = config[:password]
|
62
65
|
@consumer_key = config[:consumer_key]
|
63
66
|
@consumer_secret = config[:consumer_secret]
|
64
67
|
@access_token = config[:access_token]
|
65
68
|
@access_token_secret = config[:access_token_secret]
|
66
69
|
@rest_endpoint = config.has_key?(:rest_endpoint) ? config[:rest_endpoint] : REST_ENDPOINT
|
67
|
-
raise 'config file must contain consumer_key and consumer_secret' unless @consumer_key and @consumer_secret
|
68
70
|
end
|
69
71
|
|
70
|
-
|
71
|
-
|
72
|
+
if !@consumer_key.nil?
|
73
|
+
@auth_type = :oauth
|
74
|
+
@consumer = ::OAuth::Consumer.new(@consumer_key, @consumer_secret, {:site=>@rest_endpoint})
|
75
|
+
@accesstoken = ::OAuth::AccessToken.new(@consumer, @access_token, @access_token_secret)
|
76
|
+
elsif !@username.nil?
|
77
|
+
@auth_type = :basic
|
78
|
+
else
|
79
|
+
raise "unable to find authentication credentials"
|
80
|
+
end
|
72
81
|
|
73
82
|
end
|
74
83
|
|
@@ -85,8 +94,14 @@ module Muddyit
|
|
85
94
|
def send_request(api_url, http_method = :get, opts = {}, body = nil)
|
86
95
|
|
87
96
|
raise 'no api_url supplied' unless api_url
|
88
|
-
|
89
|
-
|
97
|
+
|
98
|
+
res = nil
|
99
|
+
case @auth_type
|
100
|
+
when :oauth
|
101
|
+
res = oauth_request_over_http(api_url, http_method, opts, body)
|
102
|
+
when :basic
|
103
|
+
res = basic_request_over_http(api_url, http_method, opts, body)
|
104
|
+
end
|
90
105
|
|
91
106
|
case res
|
92
107
|
when Net::HTTPSuccess, Net::HTTPRedirection
|
@@ -94,6 +109,7 @@ module Muddyit
|
|
94
109
|
when " "
|
95
110
|
return res
|
96
111
|
when /^.+\((.+)\)$/
|
112
|
+
# Strip any js callback method
|
97
113
|
return JSON.parse($1)
|
98
114
|
else
|
99
115
|
return JSON.parse(res.body)
|
@@ -108,10 +124,29 @@ module Muddyit
|
|
108
124
|
# creates and/or returns the Muddyit::Collections object
|
109
125
|
def collections() @collections ||= Muddyit::Collections.new(self) end
|
110
126
|
|
127
|
+
# A mirror of the pages.create method, but for one off, non-stored, quick extraction
|
128
|
+
def extract(doc={}, options={})
|
129
|
+
|
130
|
+
# Ensure we get content_data as well
|
131
|
+
options[:include_content] = true
|
132
|
+
|
133
|
+
# Ensure we have encoded the identifier and URI
|
134
|
+
unless doc[:uri] || doc[:text]
|
135
|
+
raise
|
136
|
+
end
|
137
|
+
|
138
|
+
body = { :page => doc.merge!(:options => options) }
|
139
|
+
|
140
|
+
api_url = "/extract"
|
141
|
+
response = self.send_request(api_url, :post, {}, body.to_json)
|
142
|
+
return Muddyit::Collections::Collection::Pages::Page.new(self, response)
|
143
|
+
|
144
|
+
end
|
145
|
+
|
111
146
|
protected
|
112
147
|
|
113
148
|
# For easier testing. You can mock this method with a XML file you re expecting to receive
|
114
|
-
def
|
149
|
+
def oauth_request_over_http(api_url, http_method, opts, body)
|
115
150
|
|
116
151
|
http_opts = { "Accept" => "application/json", "Content-Type" => "application/json", "User-Agent" => "muddyit_fu" }
|
117
152
|
query_string = opts.to_a.map {|x| x.join("=")}.join("&")
|
@@ -129,7 +164,65 @@ module Muddyit
|
|
129
164
|
else
|
130
165
|
raise 'invalid http method specified'
|
131
166
|
end
|
132
|
-
|
167
|
+
end
|
168
|
+
|
169
|
+
def basic_request_over_http(path, http_method, opts, data)
|
170
|
+
|
171
|
+
http_opts = { "Accept" => "application/json", "Content-Type" => "application/json", "User-Agent" => "muddyit_fu" }
|
172
|
+
query_string = opts.to_a.map {|x| x.join("=")}.join("&")
|
173
|
+
|
174
|
+
u = URI.parse(@rest_endpoint+path)
|
175
|
+
|
176
|
+
if [:post, :put].include?(http_method)
|
177
|
+
data.reject! { |k,v| v.nil? } if data.is_a?(Hash)
|
178
|
+
end
|
179
|
+
|
180
|
+
headers = http_opts
|
181
|
+
|
182
|
+
case http_method
|
183
|
+
when :post
|
184
|
+
request = Net::HTTP::Post.new(path,headers)
|
185
|
+
request.basic_auth @username, @password
|
186
|
+
request["Content-Length"] = 0 # Default to 0
|
187
|
+
when :put
|
188
|
+
request = Net::HTTP::Put.new(path,headers)
|
189
|
+
request.basic_auth @username, @password
|
190
|
+
request["Content-Length"] = 0 # Default to 0
|
191
|
+
when :get
|
192
|
+
request = Net::HTTP::Get.new(path,headers)
|
193
|
+
request.basic_auth @username, @password
|
194
|
+
when :delete
|
195
|
+
request = Net::HTTP::Delete.new(path,headers)
|
196
|
+
request.basic_auth @username, @password
|
197
|
+
when :head
|
198
|
+
request = Net::HTTP::Head.new(path,headers)
|
199
|
+
request.basic_auth @username, @password
|
200
|
+
else
|
201
|
+
raise ArgumentError, "Don't know how to handle http_method: :#{http_method.to_s}"
|
202
|
+
end
|
203
|
+
|
204
|
+
if data.is_a?(Hash)
|
205
|
+
request.set_form_data(data)
|
206
|
+
elsif data
|
207
|
+
if data.respond_to?(:read)
|
208
|
+
request.body_stream = data
|
209
|
+
if data.respond_to?(:length)
|
210
|
+
request["Content-Length"] = data.length
|
211
|
+
elsif data.respond_to?(:stat) && data.stat.respond_to?(:size)
|
212
|
+
request["Content-Length"] = data.stat.size
|
213
|
+
else
|
214
|
+
raise ArgumentError, "Don't know how to send a body_stream that doesn't respond to .length or .stat.size"
|
215
|
+
end
|
216
|
+
else
|
217
|
+
request.body = data.to_s
|
218
|
+
request["Content-Length"] = request.body.length
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
http = Net::HTTP.new(u.host, u.port)
|
223
|
+
#http.open_timeout = self.http_open_timeout unless self.http_open_timeout.nil?
|
224
|
+
#http.read_timeout = self.http_read_timeout unless self.http_read_timeout.nil?
|
225
|
+
http.start { |http| http.request(request) }
|
133
226
|
end
|
134
227
|
|
135
228
|
end
|
data/muddyit_fu.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{muddyit_fu}
|
5
|
-
s.version = "0.2.
|
5
|
+
s.version = "0.2.10"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["rattle"]
|
9
|
-
s.date = %q{2010-01-
|
9
|
+
s.date = %q{2010-01-11}
|
10
10
|
s.email = %q{support[at]muddy.it}
|
11
11
|
s.extra_rdoc_files = [
|
12
12
|
"LICENSE",
|
data/test/test_muddyit_fu.rb
CHANGED
@@ -15,12 +15,19 @@ class TestMuddyitFu < Test::Unit::TestCase
|
|
15
15
|
:consumer_secret => c['consumer_secret'],
|
16
16
|
:access_token => c['access_token'],
|
17
17
|
:access_token_secret => c['access_token_secret'],
|
18
|
-
:rest_endpoint => c['rest_endpoint']
|
18
|
+
:rest_endpoint => c['rest_endpoint'],
|
19
|
+
:username => c['username'],
|
20
|
+
:password => c['password'])
|
19
21
|
rescue
|
20
22
|
puts "Failed to connect to muddy, are the details correct ?"
|
21
23
|
end
|
22
24
|
end
|
23
25
|
|
26
|
+
should "analyse a page without a collection" do
|
27
|
+
page = @muddyit.extract({:uri => @@STORY})
|
28
|
+
assert page.entities.length > 0
|
29
|
+
end
|
30
|
+
|
24
31
|
should 'be able to create a collection' do
|
25
32
|
collection = @muddyit.collections.create(@@COLLECTION_LABEL, 'http://www.test.com')
|
26
33
|
assert !collection.token.nil?
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: muddyit_fu
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- rattle
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-01-
|
12
|
+
date: 2010-01-11 00:00:00 +00:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|