ohby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c89596c53031985de120c5778b6b6b7303b835fb
4
+ data.tar.gz: 3c58c916b441cc64d88ceb0239b4b9fba9be4306
5
+ SHA512:
6
+ metadata.gz: 8eed89717b0d0ae8ba8a391616b47962a59558ec7ca36dfbe41d94052d42e421b8da1869eb672409aed4009a465e7b2748feff039c4686370557c0544f9188aa
7
+ data.tar.gz: 15ee268a07e69fe848df49dd254485228dfe67728c0ade0bd26845dd320229019bc36e50e64b6b16ed1ec85494084bdfef994b6807fb79d3822fcde44ded66b6
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .*.swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at janlindblom@fastmail.fm. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ohby.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Jan Lindblom
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.
@@ -0,0 +1,56 @@
1
+ # Ohby
2
+
3
+ Access the "oh by" shortener (https://0x.co) from your code.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'ohby'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install ohby
20
+
21
+ ## Usage
22
+
23
+ Use it like so:
24
+
25
+ ```ruby
26
+ require 'ohby'
27
+
28
+ # a public message, expiring in 10 minutes
29
+ code = Ohby.shorten "a message", "10m"
30
+ ```
31
+
32
+ or to look up a code:
33
+
34
+ ```ruby
35
+ require 'ohby'
36
+
37
+ message = Ohby.lookup "HVRMFJ"
38
+ ```
39
+
40
+ Nothing else needed really. Read the docs for details.
41
+
42
+ ## Development
43
+
44
+ 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.
45
+
46
+ 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).
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on Bitbucket at https://bitbucket.org/janlindblom/ohby-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
51
+
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
56
+
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require "yard"
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ YARD::Rake::YardocTask.new do |t|
8
+ t.files = ['lib/**/*.rb']
9
+ t.stats_options = ['--list-undoc']
10
+ end
11
+
12
+ task :default => :spec
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ohby"
5
+
6
+ # (If you use this, don't forget to add pry to your Gemfile!)
7
+ require "pry"
8
+ Pry.start
@@ -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,15 @@
1
+ # This is a sample build configuration for Ruby.
2
+ # Check our guides at https://confluence.atlassian.com/x/8r-5Mw for more examples.
3
+ # Only use spaces to indent your .yml configuration.
4
+ # -----
5
+ # You can specify a custom docker image from Docker Hub as your build environment.
6
+ image: ruby:2.3.0
7
+
8
+ pipelines:
9
+ default:
10
+ - step:
11
+ script: # Modify the commands below to build your repository.
12
+ - bundler --version
13
+ - bundle install
14
+ - rake spec
15
+ - rake yard
@@ -0,0 +1,184 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'strscan'
4
+ require 'date'
5
+ require 'ohby/version'
6
+ require 'ohby/too_long_error'
7
+ require 'ohby/code_generation_error'
8
+ require 'ohby/wrong_code_format_error'
9
+ require 'ohby/code'
10
+
11
+ # Generate "oh by" (0x) codes.
12
+ #
13
+ # @author Jan Lindblom <janlindblom@fastmail.fm>
14
+ # @version 0.1.0
15
+ module Ohby
16
+
17
+ # Generate an "oh by" code from a given payload.
18
+ #
19
+ # See #shorten for valid options of the +expiry+ parameter.
20
+ #
21
+ # @param payload [String] up to 4096 characters
22
+ # @param expiry [String] expiration
23
+ # @param is_public [Boolean] visibility
24
+ # @raise [TooLongError] if the payload is to long.
25
+ # @since 0.1.0
26
+ def self.code(payload=nil,expiry=0,is_public=true)
27
+ shorten(payload,expiry,is_public,false)
28
+ end
29
+
30
+ # Generate an "oh by" redirect from a given payload that is a valid URI.
31
+ #
32
+ # See #shorten for valid options of the +expiry+ parameter.
33
+ #
34
+ # @param payload [String] up to 4096 characters
35
+ # @param expiry [String] expiration
36
+ # @param is_public [Boolean] visibility
37
+ # @raise [TooLongError] if the payload is to long.
38
+ # @since 0.1.0
39
+ def self.redirect(payload=nil,expiry=0,is_public=true)
40
+ shorten(payload,expiry,is_public,true)
41
+ end
42
+
43
+ # Generate an "oh by" code or redirect from a given payload.
44
+ #
45
+ # Valid options for the +expiry+ parameter:
46
+ # - +0+ - never expire
47
+ # - +10m+ - 10 Minutes
48
+ # - +1h+ - 1 Hour
49
+ # - +1D+ - 1 Day
50
+ # - +1W+ - 1 Week
51
+ # - +1M+ - 1 Month
52
+ # - +1Y+ - 1 Year
53
+ #
54
+ # @param payload [String] up to 4096 characters
55
+ # @param expiry [String] expiration
56
+ # @param is_public [Boolean] visibility
57
+ # @raise [TooLongError] if the payload is to long.
58
+ # @raise [CodeGenerationError] if the payload couldn't be shortened.
59
+ # @since 0.1.0
60
+ def self.shorten(payload=nil,expiry=0,is_public=true,redirect=false)
61
+ base_uri = "https://0x.co/shorten.html"
62
+ is_url = redirect ? true : false
63
+
64
+ # Check if the message is a URL to decide whether or not to set redirect.
65
+ is_url = check_if_message_is_url(payload)
66
+
67
+ request = {
68
+ "content" => payload,
69
+ "expiry" => expiry.to_s,
70
+ "public" => is_public ? "1" : "0"
71
+ }
72
+
73
+ unless payload.nil?
74
+ raise TooLongError.new(
75
+ "Payload too long, exceds 4096 characters.") if payload.size > 4096
76
+
77
+ if redirect == true
78
+ request["redirect"] = "1"
79
+ elsif redirect != false && is_url
80
+ request["redirect"] = "1"
81
+ elsif redirect == false && is_url
82
+ request["redirect"] = "0"
83
+ end
84
+
85
+ # Do a POST request and get the body if everything worked out.
86
+ post = Net::HTTP.post_form(URI(base_uri), request)
87
+ unless post.response.is_a? Net::HTTPOK
88
+ raise CodeGenerationError.new("Unable to perform request.")
89
+ end
90
+ result = post.body
91
+ # Get the <body> and set up a scanner on it
92
+ scanner = StringScanner.new(
93
+ result.gsub(/\n/,'').match(/<body>(.*)<\/body>/)[1])
94
+ # Find the part with the code
95
+ scanner.skip_until /0 x /
96
+ # Get the code (asciinumeral) from the scanner
97
+ code=scanner.scan /[A-z0-9]+/
98
+ code
99
+ end
100
+ end
101
+
102
+ # Look up an existing 0x-code to see what it contains.
103
+ #
104
+ # @param code [String] the code to look up
105
+ def self.lookup(code=nil)
106
+ base_uri = "https://0x.co/"
107
+ require 'rexml/document'
108
+ raise WrongCodeFormatError.new(
109
+ "Bad code format, should be a simple String.") unless code.is_a? String
110
+ raise WrongCodeFormatError.new("Bad code, cannot be nil.") if code.nil?
111
+
112
+ # Strip any "0x" from the supplied string.
113
+ payload = code.start_with?("0x") ? code.slice(2..-1) : code
114
+
115
+ # Perform lookup by posting to form, this way we get an empty xml back if
116
+ # the code is not found.
117
+ response = Net::HTTP.get(URI(base_uri + payload))
118
+
119
+ code_section = response.gsub(/\n/,'').match(
120
+ /<script.*id=\"code\".*application\/xml.*>.*<\/script>/)[0]
121
+
122
+ scanner = StringScanner.new(code_section)
123
+ end_tag = /\<\/script>/
124
+ scanner.skip(/<script.*\"application\/xml\">/)
125
+ response_xml = scanner.scan_until(end_tag)
126
+ response_xml.gsub!(end_tag,'').strip!
127
+
128
+ document = REXML::Document.new "<ohby>" + response_xml + "</ohby>"
129
+ response_hash = {}
130
+
131
+ code_object = Ohby::Code.new
132
+
133
+ document.root.elements.each do |element|
134
+ setter = element.name.to_s + "="
135
+ processed = process_text(element)
136
+
137
+ case element.name
138
+ when "code", "message", "hash"
139
+ code_object.send(setter, processed)
140
+ when "date"
141
+ code_object.send(setter,
142
+ process_string_into_datetime(processed))
143
+ when "expires"
144
+ code_object.send(setter,
145
+ process_text(element) == "" ? "" :
146
+ process_string_into_datetime(processed))
147
+ when "visibility", "redirect"
148
+ code_object.send(setter,
149
+ process_string_into_boolean(processed))
150
+ end
151
+ end
152
+ code_object
153
+ end
154
+
155
+ private
156
+
157
+ def self.process_text(element)
158
+ element.has_text? ? element.get_text.to_s : ""
159
+ end
160
+
161
+ # Appends "PST" to all datetime strings since 0x.co timestamps are given
162
+ # in PST (not really documented anywhere though)
163
+ def self.process_string_into_datetime(text)
164
+ DateTime.parse(text + " PST")
165
+ end
166
+
167
+ def self.process_string_into_boolean(text)
168
+ return true if text == "1"
169
+ false
170
+ end
171
+
172
+ def self.check_if_message_is_url(text)
173
+ is_url = false
174
+ begin
175
+ uri = URI.parse(text)
176
+ if uri.is_a? URI::HTTP or uri.is_a? URI::HTTPS
177
+ is_url = true
178
+ end
179
+ rescue URI::InvalidURIError
180
+ end
181
+ is_url
182
+ end
183
+
184
+ end
@@ -0,0 +1,24 @@
1
+ module Ohby
2
+ # Object representation of an 0x-code.
3
+ #
4
+ # @author Jan Lindblom <janlindblom@fastmail.fm>
5
+ # @version 0.1.0
6
+ # @since 0.1.0
7
+ class Code < Object
8
+ # The code itself
9
+ attr_accessor :code
10
+ # The message, or contents, this code represents
11
+ attr_accessor :message
12
+ # The date this 0x-code was created
13
+ attr_accessor :date
14
+ # The expiry of this code
15
+ attr_accessor :expires
16
+ # The visibility of this code
17
+ attr_accessor :visibility
18
+ # The Sha256 hash of this code
19
+ attr_accessor :hash
20
+ # Redirect flag on this code
21
+ attr_accessor :redirect
22
+
23
+ end
24
+ end
@@ -0,0 +1,9 @@
1
+ module Ohby
2
+ # An error during oh by code generation.
3
+ #
4
+ # @author Jan Lindblom <janlindblom@fastmail.fm>
5
+ # @version 0.1.0
6
+ # @since 0.1.0
7
+ class CodeGenerationError < StandardError
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module Ohby
2
+ # An error indicating the input is too long, more than 4096 characters.
3
+ #
4
+ # @author Jan Lindblom <janlindblom@fastmail.fm>
5
+ # @version 0.1.0
6
+ # @since 0.1.0
7
+ class TooLongError < StandardError
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ module Ohby
2
+ # Current version.
3
+ VERSION = "0.1.0"
4
+ end
@@ -0,0 +1,9 @@
1
+ module Ohby
2
+ # An error indicating the input is malformed in some way.
3
+ #
4
+ # @author Jan Lindblom <janlindblom@fastmail.fm>
5
+ # @version 0.1.0
6
+ # @since 0.1.0
7
+ class WrongCodeFormatError < StandardError
8
+ end
9
+ 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 'ohby/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ohby"
8
+ spec.version = Ohby::VERSION
9
+ spec.authors = ["Jan Lindblom"]
10
+ spec.email = ["janlindblom@fastmail.fm"]
11
+
12
+ spec.summary = %q{Generate oh by codes.}
13
+ spec.homepage = "https://bitbucket.org/janlindblom/ohby"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.metadata["yard.run"] = "yri"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.11"
24
+ spec.add_development_dependency "yard", "~> 0.9"
25
+ spec.add_development_dependency "rake", "~> 10"
26
+ spec.add_development_dependency "rspec", "~> 3"
27
+ spec.add_development_dependency "pry", "~> 0.10"
28
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ohby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jan Lindblom
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: yard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.9'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.9'
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'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10'
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'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.10'
83
+ description:
84
+ email:
85
+ - janlindblom@fastmail.fm
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - bitbucket-pipelines.yml
101
+ - lib/ohby.rb
102
+ - lib/ohby/code.rb
103
+ - lib/ohby/code_generation_error.rb
104
+ - lib/ohby/too_long_error.rb
105
+ - lib/ohby/version.rb
106
+ - lib/ohby/wrong_code_format_error.rb
107
+ - ohby.gemspec
108
+ homepage: https://bitbucket.org/janlindblom/ohby
109
+ licenses:
110
+ - MIT
111
+ metadata:
112
+ yard.run: yri
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.5.2
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Generate oh by codes.
133
+ test_files: []