polly_phone 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ba0aa3879fd46f6279195cfd48433fe43d617b7a
4
+ data.tar.gz: 36027da2d0f23de3adb5c754ddb51fe27b1bf757
5
+ SHA512:
6
+ metadata.gz: fb04d8d642c2547854aafd77cbc85e4a557d634821b6737c513338209e6d455344eca481202d73d9e364cd983adb464827bf79edeef5ccce0111bab13b85914b
7
+ data.tar.gz: 1b7cf556863f641f2a2c0eb0f3131307dc5f315205e86a126ebedbecd30cd0b304bd90c5c8e1980e121f94d707c8b8079a9328efdf4dc015068904c69c7546cc
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
6
+ script:
7
+ - bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in polly_phone.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 artkalmyk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,103 @@
1
+ # PollyPhone
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/polly_phone`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'polly_phone'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install polly_phone
22
+
23
+ ## Usage
24
+
25
+ To begin with we need a YAML file with following configuration:
26
+
27
+ | option | example | description |
28
+ | --------|---------|-------|
29
+ | name: | "MySite" | title of web-site we want to parse |
30
+ | main_url: | "http://mysite.com/" | main url of web-site we want to parse |
31
+ | brands_url: | "catalog.html" | additional path to catalog page where we want to get rand names |
32
+ | brand_link_path: | "ul li a" | path for Nokogiri search method to find element with href param for brand |
33
+ | brand_name_item: | "./img" | path for Nokogiri at_xpath method to find element with name of brand (only use when brand name is within another element, otherwise get name of brand link element) |
34
+ | brand_source: | "alt" | only use when we need another element param to find brand content, f.e. 'alt' (otherwise use element content) |
35
+ | model_link_path: | "ul li a" | path for Nokogiri search method to find element with href param for model |
36
+ | model_image_item: | "./img" | path for Nokogiri at_xpath method to find element with src param for model image |
37
+ | model_name_item: | "./h3" | path for Nokogiri at_xpath method to find element with content for model name |
38
+ | spec_path: | "#desc div" | path for Nokogiri at_xpath method to find container with phone description |
39
+ | spec_type_item: | "./h2" | path for Nokogiri at_xpath method to find description type within the container |
40
+ | spec_category_item: | "./h3" | path for Nokogiri at method to find description category within the container |
41
+ | spec_body_item: | "./p" | path for Nokogiri at method to find description text within the container |
42
+ | search_page: | "search.html?name=" | additional path for main url for web-site search |
43
+
44
+ At first initialize a new object
45
+ ```sh
46
+ site = PollyPhone::Site.new("config.yml")
47
+ ```
48
+ ##### Getting brands
49
+
50
+ There are two methods to find brands.
51
+ First will return an array of arrays [brand_name, brand_link]
52
+
53
+ ```sh
54
+ site = PollyPhone::Site.new("config.yml")
55
+ site.brands
56
+ ```
57
+ And the second will return an array of brand names as strings:
58
+
59
+ ```sh
60
+ site = PollyPhone::Site.new("config.yml")
61
+ site.brands_list
62
+ ```
63
+
64
+ ##### Getting models
65
+
66
+ There are also two methods to find models by brand.
67
+ First will return an array of hases {name: "Model name", img: "image src link", link: "model link" }
68
+
69
+ ```sh
70
+ site = PollyPhone::Site.new("config.yml")
71
+ site.models("Asus")
72
+ ```
73
+ And the second will return an array of model names as strings:
74
+
75
+ ```sh
76
+ site = PollyPhone::Site.new("config.yml")
77
+ site.models_list
78
+ ```
79
+
80
+ ##### Getting phone description
81
+
82
+ We can get phone description by phone path. It will return a has {"desription type" => {"category" => "text", "category2" => "text"}}
83
+
84
+ ```sh
85
+ site = PollyPhone::Site.new("config.yml")
86
+ site.phone_desc("iphone5s.html")
87
+ ```
88
+
89
+ ## Development
90
+
91
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
92
+
93
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
94
+
95
+ ## Contributing
96
+
97
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/polly_phone.
98
+
99
+
100
+ ## License
101
+
102
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
103
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "polly_phone"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,113 @@
1
+ #encoding: utf-8
2
+
3
+ require "yaml"
4
+ require "nokogiri"
5
+ require "open-uri"
6
+
7
+ module PollyPhone
8
+ class Site
9
+
10
+ include Utils
11
+
12
+ attr_accessor :main_url
13
+
14
+ DEFAULT_DESCRIPTION_TYPE = "Description"
15
+
16
+ # Create object with configuration for parsing with following params
17
+ # @name - title of web-site we want to parse
18
+ # @main_url - main url of web-site we want to parse
19
+ # @brands_url - additional path to catalog page where we want to get rand names
20
+ # @brand_link_path - path for Nokogiri search method to find element with href param for brand
21
+ # @brand_name_item - path for Nokogiri at_xpath method to find element with name of brand
22
+ # (only use when brand name is within another element, otherwise get name of brand link element)
23
+ # @brand_source - only use when we need another element param to find brand content, f.e. 'alt'
24
+ # (otherwise use element content)
25
+ # @model_link_path - path for Nokogiri search method to find element with href param for model
26
+ # @model_image_item - path for Nokogiri at_xpath method to find element with src param for model image
27
+ # @model_name_item - path for Nokogiri at_xpath method to find element with content for model name
28
+ # @spec_path - path for Nokogiri at_xpath method to find container with phone description
29
+ # @spec_type_item - path for Nokogiri at_xpath method to find description type within the container
30
+ # @spec_category_item - path for Nokogiri at method to find description category within the container
31
+ # @spec_body_item - path for Nokogiri at method to find description text within the container
32
+ # @search_page - additional path for main url for web-site search
33
+ def initialize(file_path)
34
+ site_config = symbolize_keys(YAML.load_file(file_path))
35
+ assign_attributes(site_config)
36
+ end
37
+
38
+ # set all configuration attributes as instance variables
39
+ def assign_attributes(hash)
40
+ hash.each do |key, value|
41
+ self.instance_variable_set(:"@#{key}", value)
42
+ end
43
+ end
44
+
45
+ # @return [Array]
46
+ def brands
47
+ doc = Nokogiri::HTML(open(@main_url + @brands_url))
48
+ doc.search(@brand_link_path).map do |item|
49
+ link = item["href"]
50
+ # if @brand_name_item exist we use another element within container
51
+ item = item.at_xpath(@brand_name_item) if @brand_name_item
52
+ # if @brand_source exist we use another param
53
+ name = @brand_source ? item[@brand_source] : item.content
54
+ [clr_str(name), link]
55
+ end
56
+ end
57
+
58
+ def brands_list
59
+ brands.to_h.keys
60
+ end
61
+
62
+ def models(brand)
63
+ phone_info(@main_url + brands.to_h[brand])
64
+ end
65
+
66
+ def models_list(brand)
67
+ models(brand).map{ |m| m[:name] }
68
+ end
69
+
70
+ def search(string)
71
+ phone_info(@main_url + @search_page + string)
72
+ end
73
+
74
+ # @return [Array]
75
+ def phone_info(url)
76
+ doc = Nokogiri::HTML(open(url))
77
+ phones = page_parser(doc)
78
+ while doc.at(@next_page)
79
+ next_page_link = @main_url + doc.at(@next_page)["href"]
80
+ doc = Nokogiri::HTML(open(next_page_link))
81
+ phones += page_parser(doc)
82
+ end
83
+ phones
84
+ end
85
+
86
+ # @return [Array]
87
+ def page_parser(doc)
88
+ doc.search(@model_link_path).map do |item|
89
+ item.css('br').each{ |br| br.replace " " }
90
+ { name: item.at_xpath(@model_name_item).content,
91
+ img: item.at_xpath(@model_image_item)['src'],
92
+ short: item.at_xpath(@model_image_item)['title'],
93
+ link: item["href"] }
94
+ end
95
+ end
96
+
97
+ # @return [Hash]
98
+ def phone_desc(url)
99
+ type = DEFAULT_DESCRIPTION_TYPE
100
+ desc = {}
101
+ doc = Nokogiri::HTML(open(@main_url + url))
102
+ doc.search(@spec_path).each do |item|
103
+ item_name = item.at_xpath(@spec_type_item)
104
+ type = item_name ? item_name.content : type
105
+ desc[type] ||= {}
106
+ category = item.at(@spec_category_item)
107
+ body = item.at(@spec_body_item)
108
+ desc[type][category.content] = body.content if category && body
109
+ end
110
+ desc
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,45 @@
1
+ #encoding: utf-8
2
+
3
+ module PollyPhone
4
+ module Utils
5
+
6
+ def symbolize_keys(obj)
7
+ case obj
8
+ when Array
9
+ obj.inject([]){|res, val|
10
+ res << case val
11
+ when Hash, Array
12
+ symbolize_keys(val)
13
+ else
14
+ val
15
+ end
16
+ res
17
+ }
18
+ when Hash
19
+ obj.inject({}){|res, (key, val)|
20
+ nkey = case key
21
+ when String
22
+ key.to_sym
23
+ else
24
+ key
25
+ end
26
+ nval = case val
27
+ when Hash, Array
28
+ symbolize_keys(val)
29
+ else
30
+ val
31
+ end
32
+ res[nkey] = nval
33
+ res
34
+ }
35
+ else
36
+ obj
37
+ end
38
+ end
39
+
40
+ # custom activesupport squish analog
41
+ def clr_str(string)
42
+ string.gsub(/\A[[:space:]]+/, '').gsub(/[[:space:]]+\z/, '').gsub(/[[:space:]]+/, ' ')
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module PollyPhone
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ require "polly_phone/version"
2
+ require "polly_phone/utils"
3
+ require "polly_phone/site"
4
+
5
+ module PollyPhone
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'polly_phone/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "polly_phone"
8
+ spec.version = PollyPhone::VERSION
9
+ spec.date = "2016-07-10"
10
+ spec.summary = "PollyPhone!"
11
+ spec.description = "A simple gem for searching mobile phones from internet"
12
+ spec.authors = ["artkalmyk"]
13
+ spec.email = ["artyom.kalm@gmail.com"]
14
+
15
+ spec.homepage = ""
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "nokogiri"
24
+ spec.add_development_dependency "bundler", "~> 1.12"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "simplecov"
28
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: polly_phone
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - artkalmyk
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A simple gem for searching mobile phones from internet
84
+ email:
85
+ - artyom.kalm@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/polly_phone.rb
100
+ - lib/polly_phone/site.rb
101
+ - lib/polly_phone/utils.rb
102
+ - lib/polly_phone/version.rb
103
+ - polly_phone.gemspec
104
+ homepage: ''
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.5.1
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: PollyPhone!
128
+ test_files: []