Class: Woro::Adapters::Gist

Inherits:
Object
  • Object
show all
Defined in:
lib/woro/adapters/gist.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gist_id) ⇒ Gist

Create a new gist collection adapter

Parameters:

  • gist_id (String)

    gist to which the adapter connects



11
12
13
# File 'lib/woro/adapters/gist.rb', line 11

def initialize(gist_id)
  @gist_id = gist_id
end

Instance Attribute Details

#gist_idObject (readonly)

Returns the value of attribute gist_id.



7
8
9
# File 'lib/woro/adapters/gist.rb', line 7

def gist_id
  @gist_id
end

Class Method Details

.create_initial_remote_task(app_name, access_token = nil) ⇒ Object

Creates an initial welcome gist on project setup

Parameters:

  • app_name (String)

    Name of the app is displayed in the initial welcome message



64
65
66
# File 'lib/woro/adapters/gist.rb', line 64

def self.create_initial_remote_task(app_name, access_token=nil)
  ::Gist.gist("Welcome to the Woro Task Repository for #{app_name}", filename: app_name, access_token: access_token)
end

Instance Method Details

#extract_description(data) ⇒ Object

Extract description from gist’s data content string.

String

description string

Parameters:

  • data (Hash)

    gist data hash



71
72
73
# File 'lib/woro/adapters/gist.rb', line 71

def extract_description(data)
  Woro::TaskHelper.extract_description(data['content'])
end

#gistHash

The Gist contains a collection of files. These are stored and accessed on Github.

Returns:

  • (Hash)

    parsed JSON hash of the gist’s metadata



34
35
36
# File 'lib/woro/adapters/gist.rb', line 34

def gist
  @gist ||= retrieve_gist(gist_id)
end

#list_keysHash

Returns the list of files included in the Gist

Returns:

  • (Hash)

    List of files in the format { filename: { data }}



17
18
19
# File 'lib/woro/adapters/gist.rb', line 17

def list_keys
  gist['files']
end

#push(task) ⇒ Object

Push this task’s file content to the Gist collection on the server. Existing contents by the same #file_name will be overriden, but can be accessed via Github or Gist’s API.



24
25
26
27
28
29
# File 'lib/woro/adapters/gist.rb', line 24

def push(task)
  ::Gist.multi_gist({ task.file_name => task.read_task_file },
                  public: false,
                  update: gist_id,
                  output: :all)
end

#raw_url(file_name) ⇒ String

The raw url is a permalink for downloading the content rake task within the Gist as a file.

Parameters:

  • file_name (String)

    name of the file to retrieve the download url

Returns:

  • (String)

    HTTP-URL of addressed file within the gist collection



58
59
60
# File 'lib/woro/adapters/gist.rb', line 58

def raw_url(file_name)
  retrieve_file_data(file_name)['raw_url']
end

#read_template_fileString

Read the rake task template

Returns:

  • (String)


77
78
79
# File 'lib/woro/adapters/gist.rb', line 77

def read_template_file
  File.read(File.join(File.dirname(__FILE__), 'templates','task.rake') )
end

#retrieve_file_data(file_name) ⇒ Hash

Retrieves the data hash included in the gist under the #file_name.

Parameters:

  • file_name (String)

    name of the file to retrieve the download url

Returns:

  • (Hash)

    parsed JSON hash



50
51
52
# File 'lib/woro/adapters/gist.rb', line 50

def retrieve_file_data(file_name)
  gist['files'][file_name]
end

#retrieve_gist(gist_id) ⇒ Hash

Retrieves metadata of the specified gist

Parameters:

  • gist_id (String)

    id of the gist

Returns:

  • (Hash)

    parsed JSON hash



41
42
43
44
45
# File 'lib/woro/adapters/gist.rb', line 41

def retrieve_gist(gist_id)
  service_url = "https://api.github.com/gists/#{gist_id}"
  response = Net::HTTP.get_response(URI.parse(service_url))
  JSON.parse(response.body || '')
end