Class: Badgerkit::Client

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

Overview

Responsible for constructing a client which is able to post values to badgherhq.com

Constant Summary collapse

BASE_URI =

The base uri the perform requests to.

'https://badgerhq.com/api/v1/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Badgerkit::Client

Construct a new Badgerkit::Client

Examples:

client = Badgerkit::Client.new(
  :access_token => '0dbce1478e94053d4282ccd4ace154c82a3475d5',
  :source       => 'github',
  :repo         => 'saladdays-nl/badgerkit'
)

Parameters:

  • options (Hash) (defaults to: {})
  • token (Hash)

    a customizable set of options

Options Hash (options):

  • :source (String)
  • :app (String)
  • :name (String)


34
35
36
37
38
# File 'lib/badgerkit/client.rb', line 34

def initialize(options={})
  @access_token = ENV['BADGER_ACCESS_TOKEN'] || options[:access_token]
  @source       = ENV['BADGER_SOURCE']       || options[:source]
  @repo         = ENV['BADGER_REPO']         || options[:repo]
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



10
11
12
# File 'lib/badgerkit/client.rb', line 10

def access_token
  @access_token
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/badgerkit/client.rb', line 10

def path
  @path
end

#repoObject (readonly)

Returns the value of attribute repo.



10
11
12
# File 'lib/badgerkit/client.rb', line 10

def repo
  @repo
end

#sourceObject (readonly)

Returns the value of attribute source.



10
11
12
# File 'lib/badgerkit/client.rb', line 10

def source
  @source
end

Instance Method Details

#path_for(badge) ⇒ String

Returns post path for badge.

Parameters:

  • badge (String)

    the badge name

Returns:

  • (String)

    escaped path



46
47
48
# File 'lib/badgerkit/client.rb', line 46

def path_for(badge)
  URI.escape("#{source}/#{repo}/#{badge}")
end

#post(badge, attributes = {}) ⇒ Hashie::Mash

Post a value.

Examples:

client.post('Documentation',
  :value       => 80,
  :commit_sha1 => '0dbce1478e94053d4282ccd4ace154c82a3475d5',
  :branch      => 'master'
)

Parameters:

  • attributes (Hash) (defaults to: {})

Options Hash (attributes):

  • :value (String, Integer, Float)
  • :commit_sha1 (String)
  • :branch (String)
  • :archive (File)

Returns:

  • (Hashie::Mash)


66
67
68
69
70
# File 'lib/badgerkit/client.rb', line 66

def post(badge, attributes={})
  attributes = { :value => attributes, :access_token => access_token }
  response   = HTTMultiParty.post("#{BASE_URI}#{path_for(badge)}", :body => attributes).parsed_response
  response   = Hashie::Mash.new(response)
end