Class: Bone

Inherits:
Object
  • Object
show all
Extended by:
API::ClassMethods
Includes:
API::InstanceMethods
Defined in:
lib/bone.rb,
lib/bone.rb,
lib/bone/api.rb

Defined Under Namespace

Modules: API, VERSION Classes: CLI, NoToken, Problem

Constant Summary collapse

APIVERSION =
'v2'.freeze
SECRETCHAR =
[('a'..'z'),('A'..'Z'),(0..9)].map(&:to_a).flatten.freeze

Class Attribute Summary collapse

Attributes included from API::InstanceMethods

#secret, #token

Class Method Summary collapse

Instance Method Summary collapse

Methods included from API::ClassMethods

destroy, generate, get, key?, keys, register, set, token?

Methods included from API::InstanceMethods

#destroy, #generate, #get, #initialize, #key?, #keys, #register, #set, #token?

Class Attribute Details

.apiObject (readonly)

Returns the value of attribute api.



44
45
46
# File 'lib/bone.rb', line 44

def api
  @api
end

.apisObject (readonly)

Returns the value of attribute apis.



44
45
46
# File 'lib/bone.rb', line 44

def apis
  @apis
end

.debugObject

Returns the value of attribute debug.



43
44
45
# File 'lib/bone.rb', line 43

def debug
  @debug
end

.digest_typeObject (readonly)

Returns the value of attribute digest_type.



44
45
46
# File 'lib/bone.rb', line 44

def digest_type
  @digest_type
end

.secretObject



67
68
69
# File 'lib/bone.rb', line 67

def secret 
  @secret || ENV['BONE_SECRET']
end

.sourceObject Also known as: src

Returns the value of attribute source.



44
45
46
# File 'lib/bone.rb', line 44

def source
  @source
end

.tokenObject



63
64
65
# File 'lib/bone.rb', line 63

def token
  @token || ENV['BONE_TOKEN']
end

Class Method Details

.credentials=(token) ⇒ Object Also known as: cred=

e.g.

Bone.cred = 'token:secret'


58
59
60
# File 'lib/bone.rb', line 58

def credentials= token
  @token, @secret = *token.split(':')
end

.digest(val, type = nil) ⇒ Object



113
114
115
116
# File 'lib/bone.rb', line 113

def digest val, type=nil
  type ||= @digest_type
  type.hexdigest val
end

.info(*msg) ⇒ Object



71
72
73
# File 'lib/bone.rb', line 71

def info *msg
  STDERR.puts *msg
end

.is_sha1?(val) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/bone.rb', line 105

def is_sha1? val
  val.to_s.match /\A[0-9a-f]{40}\z/
end

.is_sha256?(val) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/bone.rb', line 109

def is_sha256? val
  val.to_s.match /\A[0-9a-f]{64}\z/
end

.ld(*msg) ⇒ Object



75
76
77
# File 'lib/bone.rb', line 75

def ld *msg
  info *msg if debug
end

.random_secretObject



125
126
127
128
129
130
131
# File 'lib/bone.rb', line 125

def random_secret 
  src = [SECRETCHAR, %w'* ^ $ ! / . - _ + %'].flatten
  p1 = (0...2).map{ SECRETCHAR[rand(SECRETCHAR.length)] }.join
  p2 = (0...60).map{ src[rand(src.length)] }.join
  p3 = (0...2).map{ SECRETCHAR[rand(SECRETCHAR.length)] }.join      
  [p1,p2,p3].join
end

.random_tokenObject



118
119
120
121
122
123
# File 'lib/bone.rb', line 118

def random_token
  p1 = (0...21).map{ SECRETCHAR[rand(SECRETCHAR.length)] }.join
  p2 = Bone.api.token_suffix
  p3 = (0...2).map{ SECRETCHAR[rand(SECRETCHAR.length)] }.join
  [p1,p2,p3].join.upcase
end

.register_api(scheme, klass) ⇒ Object



144
145
146
# File 'lib/bone.rb', line 144

def register_api scheme, klass
  Bone.apis[scheme.to_sym] = klass
end

.require_vendor(name, version) ⇒ Object

require a library from the vendor directory. The vendor directory should be organized such that name and version can be used to create the path to the library.

e.g.

vendor/httpclient-2.1.5.2/httpclient


157
158
159
160
161
162
# File 'lib/bone.rb', line 157

def require_vendor name, version
  path = File.join(BONE_HOME, 'vendor', "#{name}-#{version}", 'lib')
  $:.unshift path
  Bone.ld "REQUIRE VENDOR: ", path
  require name
end

.select_apiObject



133
134
135
136
137
138
139
140
141
142
# File 'lib/bone.rb', line 133

def select_api
  begin
    @api = Bone.apis[Bone.source.scheme.to_sym]
    raise RuntimeError, "Bad source: #{Bone.source}" if api.nil?
    @api.connect
  rescue => ex
    Bone.info "#{ex.class}: #{ex.message}", ex.backtrace
    exit
  end
end

.uri_escape(s) ⇒ Object

Stolen from Rack::Utils which stole it from Camping.



80
81
82
83
84
# File 'lib/bone.rb', line 80

def uri_escape s
  s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
    '%'+$1.unpack('H2'*bytesize($1)).join('%').upcase
  }.tr(' ', '+')
end

.uri_unescape(s) ⇒ Object

Stolen from Rack::Utils which stole it from Camping.



87
88
89
90
91
# File 'lib/bone.rb', line 87

def uri_unescape s
  s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){
    [$1.delete('%')].pack('H*')
  }
end

Instance Method Details

#bytesize(s) ⇒ Object



96
97
98
# File 'lib/bone.rb', line 96

def bytesize s
  s.bytesize
end