Class: Cliff::Snippet

Inherits:
Object
  • Object
show all
Defined in:
lib/cliff.rb

Overview

Class for representing a snippet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, params = {}) ⇒ Snippet

Returns a new instance of Snippet.



88
89
90
91
92
93
94
# File 'lib/cliff.rb', line 88

def initialize(data, params = {})
  data = {"snippet" => data} unless data.kind_of? Hash

  @data = { "title"    => "",
            "language" => "text"
           }.merge(data).merge(params)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



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

def method_missing(meth, *args, &blk)
  super unless @data.keys.include?(meth.to_s)
  @data[meth.to_s]
end

Instance Attribute Details

#snippet_idObject (readonly)

Returns the value of attribute snippet_id.



86
87
88
# File 'lib/cliff.rb', line 86

def snippet_id
  @snippet_id
end

Class Method Details

.create(data) ⇒ Object



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

def self.create(data)
  snippet = new(data)
  snippet.save
end

Instance Method Details

#saveObject



110
111
112
113
114
115
116
117
118
119
# File 'lib/cliff.rb', line 110

def save
  uri = URI.parse(Cliff::fp_url)
  headers = {"Content-Type"  => "application/json", "Accept" => "application/json"}
  res = Net::HTTP.new(uri.host, uri.port).post(uri.path, @data.to_json, headers)
  if res.kind_of? Net::HTTPSuccess
    @snippet_id = JSON.parse(res.body)["id"]
  else
    puts "Something went wrong #{res.code.inspect}"
  end
end

#urlObject



101
102
103
# File 'lib/cliff.rb', line 101

def url
  "#{Cliff::fp_url}#{@snippet_id}"
end