Skip to content

Commit

Permalink
lib rewrite, new methods, readme update, version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
torbjon committed Feb 16, 2015
1 parent b2cca7d commit 8a695d4
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 139 deletions.
2 changes: 1 addition & 1 deletion Guardfile
@@ -1,4 +1,4 @@
guard 'rspec', cmd: 'bundle exec rspec' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
end
end
21 changes: 17 additions & 4 deletions README.md
Expand Up @@ -4,14 +4,14 @@ Ruby library for Infogr.am

## Installation

$ gem install infogram-ruby
$ gem install infogram

## Usage
```ruby
require 'infogram-ruby'
require 'infogram'

infogram = Infogram.new('API_KEY', 'API_SECRET')
infogram.get_themes
client = Infogram.new('API_KEY', 'API_SECRET')
infogram.themes.list
```

## Example response
Expand All @@ -37,6 +37,19 @@ infogram.get_themes

## API Methods

### Themes
`client.themes.list`

### Infograraphics
`client.infographics.list`
`client.infographics.get(infographic_id)`
`client.infographics.create(params)`
`client.infographics.update(infographic_id, params)`
`client.infographics.destroy(infographic_id)`

### Users
`client.users.list.get_infographics(user_id)`

## Contributing

1. Fork it ( https://github.com/[my-github-username]/infogram-ruby/fork )
Expand Down
13 changes: 6 additions & 7 deletions infogram-ruby.gemspec → infogram.gemspec
@@ -1,19 +1,18 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'infogram/version'

Gem::Specification.new do |s|
s.name = 'infogram-ruby'
s.version = '0.0.5'
s.name = 'infogram'
s.version = Infogram::VERSION
s.authors = ['Maksim Berjoza']
s.email = ['torbjon@gmail.com']
s.summary = %q{Infogr.am Ruby SDK}
s.description = %q{Ruby library for Infogr.am}
s.summary = 'Infogr.am Ruby SDK'
s.description = 'Ruby library for Infogr.am'
s.homepage = 'https://github.com/infogram/infogram-ruby'
s.license = 'MIT'

s.files = `git ls-files -z`.split("\x0")
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
s.files = `git ls-files -x Gemfile.lock`.split("\n") rescue ''
s.test_files = s.files.grep(%r{^(test|spec|features)/})
s.require_paths = ['lib']

Expand Down
59 changes: 0 additions & 59 deletions lib/infogram-ruby.rb

This file was deleted.

10 changes: 10 additions & 0 deletions lib/infogram.rb
@@ -0,0 +1,10 @@
require 'httparty'
require 'json'
require 'cgi'

require 'infogram/version'
require 'infogram/resources'
require 'infogram/client'
require 'infogram/themes'
require 'infogram/infographics'
require 'infogram/users'
31 changes: 31 additions & 0 deletions lib/infogram/client.rb
@@ -0,0 +1,31 @@
module Infogram
class Client
API_URL = 'https://infogr.am/service/v1'

attr_accessor :config

def initialize(api_key, api_secret)
config(api_key: api_key, api_secret: api_secret)
end

def config(opts = {})
@config ||= {
api_key: opts[:api_key],
api_secret: opts[:api_secret],
api_url: API_URL
}
end

def themes
@themes ||= Infogram::Themes.new(config)
end

def infographics
@infographics ||= Infogram::Infographics.new(config)
end

def users
@users ||= Infogram::Users.new(config)
end
end
end
39 changes: 39 additions & 0 deletions lib/infogram/infographics.rb
@@ -0,0 +1,39 @@
module Infogram
class Infographics < Resources
def initialize(config = {})
@config = config
end

def list(opts = {})
opts[:api_key] = @config[:api_key]
opts[:api_sig] = signature('GET', 'infographics', opts, @config)
HTTParty.get("#{@config[:api_url]}/infographics", query: opts)
end

def get(id, opts = {})
opts[:api_key] = @config[:api_key]
opts[:api_sig] = signature('GET', "infographics/#{id}", opts, @config)
HTTParty.get("#{@config[:api_url]}/infographics/#{id}", query: opts)
end

def create(opts = {})
opts[:api_key] = @config[:api_key]
opts[:api_sig] = signature('POST', 'infographics', opts, @config)
opts[:content] = opts[:content].to_json
HTTParty.post("#{@config[:api_url]}/infographics", body: opts)
end

def update(id, opts = {})
opts[:api_key] = @config[:api_key]
opts[:api_sig] = signature('PUT', "infographics/#{id}", opts, @config)
opts[:content] = opts[:content].to_json
HTTParty.put("#{@config[:api_url]}/infographics/#{id}", body: opts)
end

def destroy(id, opts = {})
opts[:api_key] = @config[:api_key]
opts[:api_sig] = signature('DELETE', "infographics/#{id}", opts, @config)
HTTParty.delete("#{@config[:api_url]}/infographics/#{id}", body: opts)
end
end
end
21 changes: 21 additions & 0 deletions lib/infogram/resources.rb
@@ -0,0 +1,21 @@
module Infogram
class Resources
def decode_params(params)
params.keys.sort.map { |k| "#{k}=#{url_escaping(params[k].to_s)}" }.join('&')
end

def url_escaping(string)
CGI.escape(string).gsub('+', '%20')
end

def signature(method, path, params, config)
string_to_sign = [
method.upcase,
url_escaping("#{config[:api_url]}/#{path}"),
url_escaping(decode_params(params))
].compact.join('&')
raw_hmac = OpenSSL::HMAC.digest('sha1', config[:api_secret], string_to_sign)
Base64.encode64(raw_hmac)[0..-2]
end
end
end
13 changes: 13 additions & 0 deletions lib/infogram/themes.rb
@@ -0,0 +1,13 @@
module Infogram
class Themes < Resources
def initialize(config = {})
@config = config
end

def list(opts = {})
opts[:api_key] = @config[:api_key]
opts[:api_sig] = signature('GET', 'themes', opts, @config)
HTTParty.get("#{@config[:api_url]}/themes", query: opts)
end
end
end
19 changes: 19 additions & 0 deletions lib/infogram/users.rb
@@ -0,0 +1,19 @@
module Infogram
class Users < Resources
def initialize(config = {})
@config = config
end

def get(id, opts = {})
opts[:api_key] = @config[:api_key]
opts[:api_sig] = signature('GET', "users/#{id}", opts, @config)
HTTParty.get("#{@config[:api_url]}/users/#{id}", query: opts)
end

def get_infographics(id, opts = {})
opts[:api_key] = @config[:api_key]
opts[:api_sig] = signature('GET', "users/#{id}/infographics", opts, @config)
HTTParty.get("#{@config[:api_url]}/users/#{id}/infographics", query: opts)
end
end
end
3 changes: 3 additions & 0 deletions lib/infogram/version.rb
@@ -0,0 +1,3 @@
module Infogram
VERSION = '1.0.0'
end
13 changes: 8 additions & 5 deletions spec/helper.rb
@@ -1,4 +1,4 @@
require 'infogram-ruby'
require 'infogram'
require 'rspec'
require 'webmock/rspec'

Expand All @@ -11,21 +11,24 @@
end

def infogram_client(key, secret)
Infogram.new(key, secret)
Infogram::Client.new(key, secret)
end

def fixture(file)
File.read('spec/fixtures/' + file)
end

def with_fixture(file)
{ body: fixture(file), headers: { content_type: 'application/json; charset=utf-8' } }
{
body: fixture(file),
headers: { content_type: 'application/json; charset=utf-8' }
}
end

def stub_get(path)
stub_request(:get, /.*#{Infogram::API_URL + '/' + path}*/)
stub_request(:get, /.*#{Infogram::Client::API_URL + '/' + path}*/)
end

def stub_post(path)
stub_request(:post, /.*#{Infogram::API_URL + '/' + path}*/)
stub_request(:post, /.*#{Infogram::Client::API_URL + '/' + path}*/)
end
63 changes: 0 additions & 63 deletions spec/infogram-ruby_spec.rb

This file was deleted.

0 comments on commit 8a695d4

Please sign in to comment.