Class: JsBinClient

Inherits:
Object
  • Object
show all
Defined in:
lib/jsbin-client.rb,
lib/jsbin-client/rest.rb,
lib/jsbin-client/config.rb,
lib/jsbin-client/version.rb,
lib/jsbin-client/exceptions.rb

Defined Under Namespace

Classes: AuthenticationRequired, BinMissing, Config, InvalidJson, OwnershipError, Rest

Constant Summary collapse

API_PREFIX =
'api/'
VERSION =
"0.1.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options_param = {}) ⇒ JsBinClient

Returns a new instance of JsBinClient.



11
12
13
14
# File 'lib/jsbin-client.rb', line 11

def initialize(options_param = {})
  @options = OpenStruct.new(Config.to_hash.merge(options_param || {}))
  @rest = Rest.new(@options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/jsbin-client.rb', line 9

def options
  @options
end

Instance Method Details

#create(bin_params) ⇒ Object

create a new bin



23
24
25
# File 'lib/jsbin-client.rb', line 23

def create(bin_params)
  @rest.post('save', bin_params)
end

#create_revision(id, bin_params) ⇒ Object

create revision for bin



28
29
30
# File 'lib/jsbin-client.rb', line 28

def create_revision(id, bin_params)
  @rest.post("#{id}/save", bin_params)
end

#get(id, revision = nil) ⇒ Object

retrieve a bin



17
18
19
20
# File 'lib/jsbin-client.rb', line 17

def get(id, revision = nil)
  url = [id, revision].compact.join('/')
  @rest.get(url)
end

#url_for(id, options = {}) ⇒ Object

URL for JSBin



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jsbin-client.rb', line 33

def url_for(id, options = {})
  panels = options[:panels]
  if options[:panels].kind_of?(Array)
    panels = options[:panels].join(',')
  end

  url = "#{@options.ssl ? 'https' : 'http'}://#{@options.host}:#{@options.port}/#{id}"
  url << if options[:revision]
    "/#{options[:revision]}"
  else
    "/latest"
  end
  if options[:embed]
    url << "/embed"
    url << "?#{panels}" unless panels.nil?
  elsif !options[:preview]
    url << "/edit"
    url << "##{panels}" unless panels.nil?
  end

  url
end