jphastings-SlyPI 0.6 → 0.7

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.
Files changed (2) hide show
  1. data/slypi.rb +42 -5
  2. metadata +1 -1
data/slypi.rb CHANGED
@@ -55,11 +55,32 @@ class SlyPI
55
55
  @description = settings['About']['Description']
56
56
  @description.freeze
57
57
  @slypi_methods = []
58
+ @authenticated = true
59
+ if not settings['Login'].nil?
60
+ @authenticated = false
61
+ @slypi_methods.push("login")
62
+ eval("def login( options = {} )
63
+ raise \"Please specify a hash of the function terms\" if not options.is_a?(Hash)
64
+ details = #{settings['Login'].inspect}
65
+ if send(\"run_function\",details,options)['Success']
66
+ @authenticated = true
67
+ return true
68
+ else
69
+ $stderr.puts \"Login Failed\"
70
+ return false
71
+ end
72
+ end")
73
+ end
58
74
 
59
75
  settings['Functions'].each do |function_name,details|
60
76
  if function_name =~ /^[0-9a-zA-Z]+$/
61
77
  @slypi_methods.push(function_name)
62
- eval("def #{function_name}( options ); raise \"Please specify a hash of the function terms\" if not options.is_a?(Hash);details = #{details.inspect};params = {}; return send(\"run_function\",details,options); end")
78
+ eval("def #{function_name}( options = {} )
79
+ raise \"Please specify a hash of the function terms\" if not options.is_a?(Hash)
80
+ raise \"You're not authenticated.\" if not @authenticated
81
+ details = #{details.inspect}
82
+ return send(\"run_function\",details,options)
83
+ end")
63
84
  else
64
85
  $stderr.puts "The method '#{function_name}' is not a valid SlyPI method name. Please check your api file!"
65
86
  end
@@ -67,6 +88,11 @@ class SlyPI
67
88
  @slypi_methods.freeze
68
89
  end
69
90
 
91
+ # If the loaded SlyPI requires authentication this will tell you if everything is ready to proceed
92
+ def authenticated?
93
+ @authenticated
94
+ end
95
+
70
96
  # Just a standard inspect method at the moment. In the future it will allow you to inspect the dynamically generated methods to find out what they require and other such information
71
97
  def inspect(conditions = nil)
72
98
  if conditions.nil?
@@ -100,7 +126,12 @@ class SlyPI
100
126
 
101
127
  url = subst(spec['request']['url'],parameters)
102
128
  begin
103
- page = @agent.get(url)
129
+ case spec['request']['method']
130
+ when "POST"
131
+ page = @agent.post(url,Hash[*spec['request']['data'].collect{ |opt| [opt[0],parameters[opt[1].to_sym]] }.flatten])
132
+ else # Includes GET
133
+ page = @agent.get(url)
134
+ end
104
135
  rescue
105
136
  $stderr.puts "We've experienced an error attempting to get the information from '#{url}'. More information follows."
106
137
  raise
@@ -119,14 +150,20 @@ class SlyPI
119
150
  else
120
151
  begin
121
152
  el = root.xpath(subst(item[1]['xpath'],params))
122
- if item[1]['regex'].nil?
123
- output[item[0]] = (el.is_a?(String) or el.length < 2) ? el.inner_text.strip : el.collect{|e| e.inner_text.strip}
124
- else
153
+ if not item[1]['regex'].nil?
125
154
  output[item[0]] = (el.is_a?(String) or el.length < 2) ?
126
155
  el.inner_text.strip.match(Regexp.new(subst(item[1]['regex'],params),Regexp::MULTILINE))[1] :
127
156
  el.collect{ |e|
128
157
  e.inner_text.strip.match(Regexp.new(subst(item[1]['regex'],params),Regexp::MULTILINE))[1]
129
158
  }
159
+ elsif not item[1]['matches'].nil?
160
+ output[item[0]] = (el.is_a?(String) or el.length < 2) ?
161
+ (not el.inner_text.strip.match(Regexp.new(subst(item[1]['matches'],params),Regexp::MULTILINE)).nil?) :
162
+ el.inject(true){ |e|
163
+ (not e.inner_text.strip.match(Regexp.new(subst(item[1]['matches'],params),Regexp::MULTILINE)).nil?)
164
+ }
165
+ else
166
+ output[item[0]] = (el.is_a?(String) or el.length < 2) ? el.inner_text.strip : el.collect{|e| e.inner_text.strip}
130
167
  end
131
168
  rescue NoMethodError
132
169
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jphastings-SlyPI
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.6"
4
+ version: "0.7"
5
5
  platform: ruby
6
6
  authors:
7
7
  - JP Hastings-Spital