Module: Ohby

Defined in:
lib/ohby.rb,
lib/ohby/code.rb,
lib/ohby/version.rb,
lib/ohby/too_long_error.rb,
lib/ohby/code_generation_error.rb,
lib/ohby/wrong_code_format_error.rb

Overview

Generate “oh by” (0x) codes.

Author:

Version:

  • 0.1.0

Defined Under Namespace

Classes: Code, CodeGenerationError, TooLongError, WrongCodeFormatError

Constant Summary collapse

VERSION =

Current version.

"0.1.1"

Class Method Summary collapse

Class Method Details

.code(payload = nil, expiry = 0, is_public = true) ⇒ Object

Generate an “oh by” code from a given payload.

See #shorten for valid options of the expiry parameter.

Parameters:

  • payload (String) (defaults to: nil)

    up to 4096 characters

  • expiry (String) (defaults to: 0)

    expiration

  • is_public (Boolean) (defaults to: true)

    visibility

Raises:

Since:

  • 0.1.0



26
27
28
# File 'lib/ohby.rb', line 26

def self.code(payload=nil,expiry=0,is_public=true)
  shorten(payload,expiry,is_public,false)
end

.lookup(code = nil) ⇒ Object

Look up an existing 0x-code to see what it contains.

Parameters:

  • code (String) (defaults to: nil)

    the code to look up

Raises:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ohby.rb', line 105

def self.lookup(code=nil)
  base_uri = "https://0x.co/"
  require 'rexml/document'
  raise WrongCodeFormatError.new(
    "Bad code format, should be a simple String.") unless code.is_a? String
  raise WrongCodeFormatError.new("Bad code, cannot be nil.") if code.nil?

  # Strip any "0x" from the supplied string.
  payload = code.start_with?("0x") ? code.slice(2..-1) : code

  # Perform lookup by posting to form, this way we get an empty xml back if
  # the code is not found.
  response = Net::HTTP.get(URI(base_uri + payload))

  code_section = response.gsub(/\n/,'').match(
    /<script.*id=\"code\".*application\/xml.*>.*<\/script>/)[0]

  scanner = StringScanner.new(code_section)
  end_tag = /\<\/script>/
  scanner.skip(/<script.*\"application\/xml\">/)
  response_xml = scanner.scan_until(end_tag)
  response_xml.gsub!(end_tag,'').strip!
  
  document = REXML::Document.new "<ohby>" + response_xml + "</ohby>"
  response_hash = {}
  
  code_object = Ohby::Code.new

  document.root.elements.each do |element|
    setter = element.name.to_s + "="
    processed = process_text(element)
    
    case element.name
    when "code", "message", "hash"
      code_object.send(setter, processed)
    when "date"
      code_object.send(setter,
                       process_string_into_datetime(processed))
    when "expires"
      code_object.send(setter,
                       process_text(element) == "" ? "" :
                       process_string_into_datetime(processed))
    when "visibility", "redirect"
      code_object.send(setter,
                       process_string_into_boolean(processed))
    end
  end
  code_object
end

.redirect(payload = nil, expiry = 0, is_public = true) ⇒ Object

Generate an “oh by” redirect from a given payload that is a valid URI.

See #shorten for valid options of the expiry parameter.

Parameters:

  • payload (String) (defaults to: nil)

    up to 4096 characters

  • expiry (String) (defaults to: 0)

    expiration

  • is_public (Boolean) (defaults to: true)

    visibility

Raises:

Since:

  • 0.1.0



39
40
41
# File 'lib/ohby.rb', line 39

def self.redirect(payload=nil,expiry=0,is_public=true)
  shorten(payload,expiry,is_public,true)
end

.shorten(payload = nil, expiry = 0, is_public = true, redirect = false) ⇒ Object

Generate an “oh by” code or redirect from a given payload.

Valid options for the expiry parameter:

  • 0 - never expire

  • 10m - 10 Minutes

  • 1h - 1 Hour

  • 1D - 1 Day

  • 1W - 1 Week

  • 1M - 1 Month

  • 1Y - 1 Year

Parameters:

  • payload (String) (defaults to: nil)

    up to 4096 characters

  • expiry (String) (defaults to: 0)

    expiration

  • is_public (Boolean) (defaults to: true)

    visibility

Raises:

Since:

  • 0.1.0



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ohby.rb', line 60

def self.shorten(payload=nil,expiry=0,is_public=true,redirect=false)
  base_uri = "https://0x.co/shorten.html"
  is_url = redirect ? true : false

  # Check if the message is a URL to decide whether or not to set redirect.
  is_url = check_if_message_is_url(payload)

  request = {
      "content" => payload,
      "expiry" => expiry.to_s,
      "public" => is_public ? "1" : "0"
  }

  unless payload.nil?
    raise TooLongError.new(
      "Payload too long, exceds 4096 characters.") if payload.size > 4096

    if redirect == true
      request["redirect"] = "1"
    elsif redirect != false && is_url
      request["redirect"] = "1"
    elsif redirect == false && is_url
      request["redirect"] = "0"
    end

    # Do a POST request and get the body if everything worked out.
    post = Net::HTTP.post_form(URI(base_uri), request)
    unless post.response.is_a? Net::HTTPOK
      raise CodeGenerationError.new("Unable to perform request.") 
    end
    result = post.body
    # Get the <body> and set up a scanner on it
    scanner = StringScanner.new(
        result.gsub(/\n/,'').match(/<body>(.*)<\/body>/)[1])
    # Find the part with the code
    scanner.skip_until /0 x /
    # Get the code (asciinumeral) from the scanner
    code=scanner.scan /[A-z0-9]+/
    code
  end
end