scrapify 0.0.1 → 0.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/Gemfile CHANGED
@@ -7,4 +7,5 @@ gem 'rspec'
7
7
  gem 'mocha'
8
8
  gem 'fakeweb'
9
9
  gem 'active_support'
10
- gem 'nokogiri'
10
+ gem 'nokogiri'
11
+ gem 'json'
data/README.md CHANGED
@@ -33,4 +33,9 @@ Now you can use finder methods to extract data from a static site
33
33
  > pizza = Pizza.find('mushroom')
34
34
  > pizza.name
35
35
  > pizza.image_url
36
+
37
+ > Pizza.first
38
+ > Pizza.last
39
+
40
+ > Pizza.count
36
41
  ```
data/lib/jsonify.rb ADDED
@@ -0,0 +1,26 @@
1
+ class Jsonify
2
+ def initialize(route, model)
3
+ @route = route
4
+ @model = model
5
+ end
6
+
7
+ def call(env)
8
+ path = env['REQUEST_PATH']
9
+ response = path == @route ? all : one(find_id(path))
10
+ [200, {"Content-Type" => "application/json"}, [response]]
11
+ end
12
+
13
+ private
14
+
15
+ def all
16
+ @model.all.to_json
17
+ end
18
+
19
+ def one(id)
20
+ @model.find(id).to_json
21
+ end
22
+
23
+ def find_id(path)
24
+ path[path.rindex('/') + 1, path.size]
25
+ end
26
+ end
data/lib/scrapify/base.rb CHANGED
@@ -3,6 +3,19 @@ module Scrapify
3
3
  def self.included(klass)
4
4
  klass.extend ClassMethods
5
5
  klass.cattr_accessor :url, :doc, :attribute_names
6
+ klass.instance_eval { attr_reader :attributes }
7
+ end
8
+
9
+ def initialize(attributes)
10
+ @attributes = attributes
11
+ end
12
+
13
+ def method_missing(method, *args, &block)
14
+ @attributes[method] || super
15
+ end
16
+
17
+ def to_json(*args)
18
+ @attributes.to_json(*args)
6
19
  end
7
20
 
8
21
  module ClassMethods
@@ -55,7 +68,7 @@ module Scrapify
55
68
  meta_define :find_by_index do |index|
56
69
  return if index.nil? or index < 0
57
70
  attributes = Hash[attribute_names.map {|attribute| [attribute, send("#{attribute}_values")[index]]}]
58
- OpenStruct.new(attributes)
71
+ self.new(attributes)
59
72
  end
60
73
  end
61
74
 
@@ -1,3 +1,3 @@
1
1
  module Scrapify
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/scrapify.rb CHANGED
@@ -3,5 +3,6 @@ require 'active_support/core_ext/class/attribute_accessors'
3
3
  require 'nokogiri'
4
4
  require 'open-uri'
5
5
  require 'meta_define'
6
- require 'ostruct'
7
- require 'scrapify/base'
6
+ require 'scrapify/base'
7
+ require 'json'
8
+ require 'jsonify'
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+ require 'test_models'
3
+
4
+ describe Jsonify do
5
+ it "should find all objects and convert to json for index url" do
6
+ pizzas = [{name: 'cheese'}, {name: 'chicken'}]
7
+ ::Pizza.expects(:all).returns(pizzas)
8
+
9
+ jsonify = Jsonify.new('/pizzas', ::Pizza)
10
+ status, header, response = jsonify.call('REQUEST_PATH' => '/pizzas')
11
+
12
+ status.should == 200
13
+ header['Content-Type'].should == 'application/json'
14
+ response.first.should == pizzas.to_json
15
+ end
16
+
17
+ it "should find object by id and convert to json for show url" do
18
+ pizza = {name: 'cheese'}
19
+ ::Pizza.expects(:find).with('cheese').returns(pizza)
20
+
21
+ jsonify = Jsonify.new('/pizzas', ::Pizza)
22
+ status, header, response = jsonify.call('REQUEST_PATH' => '/pizzas/cheese')
23
+
24
+ status.should == 200
25
+ header['Content-Type'].should == 'application/json'
26
+ response.first.should == pizza.to_json
27
+ end
28
+ end
@@ -76,4 +76,18 @@ describe Scrapify do
76
76
  ::Pizza.count.should == 3
77
77
  end
78
78
  end
79
+
80
+ describe "attributes" do
81
+ it "should return attributes hash" do
82
+ first_pizza = ::Pizza.first
83
+ first_pizza.attributes.should == {name: "chicken supreme", image_url: "chicken.jpg"}
84
+ end
85
+ end
86
+
87
+ describe "to_json" do
88
+ it "should convert attributes to json" do
89
+ first_pizza = ::Pizza.first
90
+ first_pizza.to_json.should == {name: "chicken supreme", image_url: "chicken.jpg"}.to_json
91
+ end
92
+ end
79
93
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scrapify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-25 00:00:00.000000000Z
12
+ date: 2012-05-26 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70282396705740 !ruby/object:Gem::Requirement
16
+ requirement: &70197239201780 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70282396705740
24
+ version_requirements: *70197239201780
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mocha
27
- requirement: &70282396705260 !ruby/object:Gem::Requirement
27
+ requirement: &70197239181000 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70282396705260
35
+ version_requirements: *70197239181000
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: fakeweb
38
- requirement: &70282396704820 !ruby/object:Gem::Requirement
38
+ requirement: &70197239178500 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70282396704820
46
+ version_requirements: *70197239178500
47
47
  description: ScrApify scraps static html sites to RESTlike APIs
48
48
  email:
49
49
  - sathish316@gmail.com
@@ -55,11 +55,13 @@ files:
55
55
  - Gemfile
56
56
  - README.md
57
57
  - Rakefile
58
+ - lib/jsonify.rb
58
59
  - lib/meta_define.rb
59
60
  - lib/scrapify.rb
60
61
  - lib/scrapify/base.rb
61
62
  - lib/scrapify/version.rb
62
63
  - scrapify.gemspec
64
+ - spec/jsonify_spec.rb
63
65
  - spec/pizza.rb
64
66
  - spec/scrapify_spec.rb
65
67
  - spec/spec_helper.rb
@@ -89,6 +91,7 @@ signing_key:
89
91
  specification_version: 3
90
92
  summary: ScrApify scraps static html sites to scraESTlike APIs
91
93
  test_files:
94
+ - spec/jsonify_spec.rb
92
95
  - spec/pizza.rb
93
96
  - spec/scrapify_spec.rb
94
97
  - spec/spec_helper.rb