cinch-toolbox 1.0.1 → 1.0.2

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/.travis.yml CHANGED
@@ -1,5 +1,4 @@
1
- nguage: ruby
1
+ language: ruby
2
2
  rvm:
3
- - ruby-head
4
3
  - 1.9.2
5
4
  - 1.9.3
data/README.md CHANGED
@@ -24,7 +24,7 @@ Note that this *only* honors redirection requests from HTTP => HTTPS and *not* H
24
24
 
25
25
  Add this line to your application's Gemfile:
26
26
 
27
- gem 'cinch-toolbox'
27
+ gem 'cinch/toolbox'
28
28
 
29
29
  And then execute:
30
30
 
@@ -44,7 +44,14 @@ Or install it yourself as:
44
44
 
45
45
  ## Changelog
46
46
 
47
- * 1.0.0
47
+ * 1.0.2
48
+ * [Enhancement] Reorged the file layout a bit to be more canonical (`require cinch/toolbox`
49
+ now instead of `require cinch-toolbox`.
50
+ * [Enhancement] Added support for retrieving the full contents of a html element by passing
51
+ `:css_full` or `:xpath_full` to the `Cinch::Toolbox.get_html_element` method.
52
+ * 1.0.1
53
+ * [Refactor] Updated how `time_format` functions.
54
+ * 1.0.0 (
48
55
  * Added tests!
49
56
  * Added docs!
50
57
  * Cleaned up code in `Toolbox.get_html_element` to be more error resistant
@@ -1,7 +1,7 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'cinch-toolbox/version'
4
+ require 'cinch/toolbox/version'
5
5
 
6
6
  Gem::Specification.new do |gem|
7
7
  gem.name = "cinch-toolbox"
@@ -1,5 +1,5 @@
1
1
  module Cinch
2
2
  module Toolbox
3
- VERSION = "1.0.1"
3
+ VERSION = "1.0.2"
4
4
  end
5
5
  end
@@ -1,5 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
- require 'cinch-toolbox/version'
2
+ require 'cinch/toolbox/version'
3
3
  require 'open-uri'
4
4
  require 'patron'
5
5
  require 'nokogiri'
@@ -10,17 +10,25 @@ module Cinch
10
10
  # Get an element of the supplied website
11
11
  # @param [String] url The url to access.
12
12
  # @param [String] selector The the selector to try an acquire on the page.
13
- # @param [String] mode (:css) Set his to css or xpath based on the type of selector.
13
+ # @param [String] mode (:css) Set this to the kind of selection you want to do.
14
+ # @option [String] :mode :css Fetch just the text content at the css selector.
15
+ # @option [String] :mode :css_full Fetch the markup and text content at the css selector.
16
+ # @option [String] :mode :xpath Fetch just the text content at the xpath selector.
17
+ # @option [String] :mode :xpath_full Fetch the markup and text at the xpath selector.
14
18
  # @return [String] The content ofg the Element or Nil if the element could not be found.
15
19
  def Toolbox.get_html_element(url, selector, mode = :css)
16
20
  # Make sure the URL is legit
17
21
  url = URI::extract(url, ["http", "https"]).first
18
22
  url = Nokogiri::HTML(open(url))
19
- if url.send(mode.to_sym, selector).empty?
20
- return nil
21
- else
22
- return url.send(mode.to_sym, selector).first.content
23
- end
23
+
24
+ content = case mode
25
+ when :css, :xpath
26
+ page = url.send(mode.to_sym, selector)
27
+ page.first.content unless page.first.nil?
28
+ when :css_full, :xpath_full
29
+ url.send("at_#{mode.to_s.gsub(/_full/, '')}", selector).to_html
30
+ end
31
+ return content
24
32
  rescue SocketError, RuntimeError
25
33
  # Rescue for any kind of network sillyness
26
34
  return nil
@@ -78,16 +86,16 @@ module Cinch
78
86
  # Used to render a period of time in a uniform string.
79
87
  # There is probably a much better way to do this, so FIXME
80
88
  # @param [Fixnum] secs Number of seconds to render into a string.
81
- def Toolbox.time_format(secs, units = nil)
82
- data = { :days => (secs / 86400).floor,
83
- :hours => ((secs % 86400) / 3600).floor,
84
- :mins => ((secs % 3600) / 60).floor,
85
- :secs => (secs % 60).floor }
89
+ def Toolbox.time_format(secs, units = nil, format = :long)
90
+ data = { :days => (secs / 86400).floor,
91
+ :hours => ((secs % 86400) / 3600).floor,
92
+ :mins => ((secs % 3600) / 60).floor,
93
+ :seconds => (secs % 60).floor }
86
94
  string = []
87
95
  data.keys.map do |period|
88
- if period == :secs || !(data[period].zero? && string.empty?)
96
+ if period == :seconds || !(data[period].zero? && string.empty?)
89
97
  if units.nil? || units.include?(period)
90
- string << "#{data[period]} #{period}"
98
+ string << "#{data[period]}#{format == :long ? " #{period}" : period.slice(0)}"
91
99
  end
92
100
  end
93
101
  end
@@ -5,11 +5,9 @@ describe Cinch::Toolbox do
5
5
 
6
6
  describe 'the get_html_element method' do
7
7
  before(:all) do
8
+ @fake_web_html = "<div id=\"a1\" class=\"top\">\n<div class=\"foo\">Bar</div>\n<div id=\"foo1\">Baz</div>\n</div>"
8
9
  FakeWeb.register_uri( :get, "http://example.com/",
9
- :body => "<div id='a1' class='top'>
10
- <div class='foo'>Bar</div>
11
- <div id='foo1'>Baz</div>
12
- </div>")
10
+ :body => @fake_web_html)
13
11
  end
14
12
 
15
13
  it 'should return a string with contents of a css selector of a web page' do
@@ -17,11 +15,21 @@ describe Cinch::Toolbox do
17
15
  should == 'Bar'
18
16
  end
19
17
 
18
+ it 'should return a string with full markup of a css selector of a web page' do
19
+ Cinch::Toolbox.get_html_element('http://example.com/', '.top', :css_full).
20
+ should == @fake_web_html
21
+ end
22
+
20
23
  it 'should return a string with contents of a xpath selector of a web page' do
21
24
  Cinch::Toolbox.get_html_element('http://example.com/', "//div/div[1]", :xpath).
22
25
  should == 'Bar'
23
26
  end
24
27
 
28
+ it 'should return a string with contents of a xpath selector of a web page' do
29
+ Cinch::Toolbox.get_html_element('http://example.com/', "//div[@id='a1']", :xpath_full).
30
+ should == @fake_web_html
31
+ end
32
+
25
33
  it 'should return nil if the css element does not exist' do
26
34
  Cinch::Toolbox.get_html_element('http://example.com/', '.foo2', :css).
27
35
  should be_nil
@@ -111,7 +119,17 @@ describe Cinch::Toolbox do
111
119
  describe 'time parseing' do
112
120
  it 'should parse seconds into a user friendly string' do
113
121
  Cinch::Toolbox.time_format(126).
114
- should == '2m 6s'
122
+ should == '2 mins, 6 seconds'
123
+ end
124
+
125
+ it 'should allow users to limit the kinds of units returned' do
126
+ Cinch::Toolbox.time_format(126020, [:days, :seconds]).
127
+ should == '1 days, 20 seconds'
128
+ end
129
+
130
+ it 'should allow users to specify short form' do
131
+ Cinch::Toolbox.time_format(126000, [:days], :short).
132
+ should == '1d'
115
133
  end
116
134
  end
117
135
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  require 'coveralls'
2
2
  Coveralls.wear!
3
- require 'cinch-toolbox'
3
+ require 'cinch/toolbox'
4
4
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cinch-toolbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-24 00:00:00.000000000 Z
12
+ date: 2013-07-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -122,8 +122,8 @@ files:
122
122
  - README.md
123
123
  - Rakefile
124
124
  - cinch-toolbox.gemspec
125
- - lib/cinch-toolbox.rb
126
- - lib/cinch-toolbox/version.rb
125
+ - lib/cinch/toolbox.rb
126
+ - lib/cinch/toolbox/version.rb
127
127
  - spec/cinch-toolbox_spec.rb
128
128
  - spec/spec_helper.rb
129
129
  homepage: https://github.com/bhaberer/cinch-toolbox